Home > Archive > PERL Beginners > April 2008 > how to look back past hour
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to look back past hour
|
|
| Richard Lee 2008-04-02, 4:28 am |
| What is the best way to indicate past hour from current time without
using a module?
so if it's 12:00, then 11:00-12:00
if it's 17:30 then 16:30 to 17:30
if it's 00:30 then 23:30 to 00:30
I wrote a below program and works but there has to be a better and
easier(not sure if I want easier as I am trying to learn) but I want to
do it the right perl way(despite
perl's motto)..
let me know please.
#!/usr/bin/perl -w
use strict;
use diagnostics;
my $time = localtime;
my @time_1 = split / /, $time;
my $start_time;
for ($time_1[4]) {
if ($_ =~ /00:([0-5][0-9]:[^ ])/) {
$start_time = join(':', 23,$1);
} else {
/(.*):(.*):(.*)/;
$start_time = join(':', $1 - 1, $2,$3);
}
}
print "$start_time to $time \n";
__END__
Wed Apr 2 01:04:07 2008
| |
| Jeff Pang 2008-04-02, 4:28 am |
| On 4/2/08, Richard Lee <rich.japh@gmail.com> wrote:
> What is the best way to indicate past hour from current time without
> using a module?
>
> so if it's 12:00, then 11:00-12:00
>
I'd prefer using POSIX module since it's a built-in perl module.
The code would be simple,
use strict;
use POSIX 'strftime';
print get_time(time - 3600),"-",get_time(),"\n";
sub get_time {
my $timestamp = shift || time;
return strftime("%H:%M",localtime($timestamp));
}
| |
| Chas. Owens 2008-04-02, 4:28 am |
| On Wed, Apr 2, 2008 at 2:02 AM, Jeff Pang <pangj@earthlink.net> wrote:
> On 4/2/08, Richard Lee <rich.japh@gmail.com> wrote:
>
> I'd prefer using POSIX module since it's a built-in perl module.
> The code would be simple,
>
> use strict;
> use POSIX 'strftime';
>
> print get_time(time - 3600),"-",get_time(),"\n";
>
> sub get_time {
> my $timestamp = shift || time;
> return strftime("%H:%M",localtime($timestamp));
> }
This is one of those seemingly simple questions that turns out to be
much more complicated than you originally think. For instance, what
is one hour less than 2008-03-09 03:00:00? If you answered 2008-03-09
02:00:00 you would be wrong in the USA as that hour does not exist.
Daylight Saving Time kicks in on the second Sunday of March (for now
at least), so the right answer is 2008-03-09 01:00:00. These sort of
caveats make working with dates and times a pain.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| John W. Krahn 2008-04-02, 4:28 am |
| Richard Lee wrote:
> What is the best way to indicate past hour from current time without
> using a module?
>
> so if it's 12:00, then 11:00-12:00
>
> if it's 17:30 then 16:30 to 17:30
>
> if it's 00:30 then 23:30 to 00:30
>
> I wrote a below program and works but there has to be a better and
> easier(not sure if I want easier as I am trying to learn) but I want to
> do it the right perl way(despite
> perl's motto)..
>
> let me know please.
>
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
>
> my $time = localtime;
> my @time_1 = split / /, $time;
> my $start_time;
>
> for ($time_1[4]) {
> if ($_ =~ /00:([0-5][0-9]:[^ ])/) {
> $start_time = join(':', 23,$1);
> } else {
> /(.*):(.*):(.*)/;
> $start_time = join(':', $1 - 1, $2,$3);
> }
> }
>
> print "$start_time to $time \n";
my $seconds_per_hour = 3600;
print scalar localtime, "\n"; # now
print scalar localtime time - $seconds_per_hour, "\n"; # one hour ago
Or if you just want to print the time only:
my $seconds_per_hour = 3600;
my ( $second, $minute, $hour ) = localtime;
printf "%02d:%02d:%02d\n", $hour, $minute, $second; # now
my ( $second, $minute, $hour ) = localtime time - $seconds_per_hour;
printf "%02d:%02d:%02d\n", $hour, $minute, $second; # one hour ago
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Jeff Pang 2008-04-02, 4:29 am |
| On 4/2/08, Chas. Owens <chas.owens@gmail.com> wrote:
> On Wed, Apr 2, 2008 at 2:02 AM, Jeff Pang <pangj@earthlink.net> wrote:
>
> This is one of those seemingly simple questions that turns out to be
> much more complicated than you originally think. For instance, what
> is one hour less than 2008-03-09 03:00:00? If you answered 2008-03-09
> 02:00:00 you would be wrong in the USA as that hour does not exist.
It sounds strange to me.
Why 2008-03-09 02:00:00 doesn't exist for USA people?
| |
| Chas. Owens 2008-04-02, 4:30 am |
| On Wed, Apr 2, 2008 at 2:24 AM, Jeff Pang <pangj@earthlink.net> wrote:
> On 4/2/08, Chas. Owens <chas.owens@gmail.com> wrote:
>
> It sounds strange to me.
> Why 2008-03-09 02:00:00 doesn't exist for USA people?
>
Because of Daylight Saving Time. Clocks are set from 01:59:59 to
03:00:00 on the second Sunday in March and from 01:59:59 to 01:00:00
on the first Sunday in November.
* http://en.wikipedia.org/wiki/Daylight_saving_time
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| T Baetzler 2008-04-02, 4:30 am |
| Richard Lee <rich.japh@gmail.com> asked:
> What is the best way to indicate past hour from current time=20
> without using a module?
[...]
#!/usr/bin/perl -w
use strict;
printf "%d:%02d to %d:%02d\n", (localtime time - 3600 )[2,1], (localtime =
time)[2,1];
__END__
> my $time =3D localtime;
> my @time_1 =3D split / /, $time;
Don't use a variable name like $time, it'll introduce hard to
find bugs in your code should you ever forget the sigil $.
Also splitting scalar localtime for hours and minutes is=20
redundant. If you want individual values instead of a time
stamp, use localtime in scalar context, i.e.
my @time_1 =3D localtime;
Once you know that
my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst) =3D localtime;
you can pick out individual values like this:
my( $min, $hour ) =3D ( localtime )[1,2];
or even
my( $hour, $min ) =3D ( localtime )[2,1];
HTH,
Thomas
| |
| Rob Dixon 2008-04-02, 7:18 pm |
| Richard Lee wrote:
> What is the best way to indicate past hour from current time without
> using a module?
>
> so if it's 12:00, then 11:00-12:00
>
> if it's 17:30 then 16:30 to 17:30
>
> if it's 00:30 then 23:30 to 00:30
>
> I wrote a below program and works but there has to be a better and
> easier(not sure if I want easier as I am trying to learn) but I want to
> do it the right perl way(despite
> perl's motto)..
>
> let me know please.
>
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
>
> my $time = localtime;
> my @time_1 = split / /, $time;
> my $start_time;
>
> for ($time_1[4]) {
> if ($_ =~ /00:([0-5][0-9]:[^ ])/) {
> $start_time = join(':', 23,$1);
> } else {
> /(.*):(.*):(.*)/;
> $start_time = join(':', $1 - 1, $2,$3);
> }
> }
>
> print "$start_time to $time \n";
>
> __END__
> Wed Apr 2 01:04:07 2008
Hi Richard.
How about the program below.
HTH,
Rob
use strict;
use warnings;
my $time = localtime;
my $start_time;
for ($time) {
my $hms = (split)[3];
($start_time = $hms) =~ s/(\d+)/($1 - 1) % 24/e;
}
print "$start_time to $time \n";
|
|
|
|
|