Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

how to look back past hour
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


Report this thread to moderator Post Follow-up to this message
Old Post
Richard Lee
04-02-08 09:28 AM


Re: how to look back past hour
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));
}

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Pang
04-02-08 09:28 AM


Re: how to look back past hour
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chas. Owens
04-02-08 09:28 AM


Re: how to look back past hour
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-02-08 09:28 AM


Re: how to look back past hour
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?

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Pang
04-02-08 09:29 AM


Re: how to look back past hour
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chas. Owens
04-02-08 09:30 AM


RE: how to look back past hour
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

Report this thread to moderator Post Follow-up to this message
Old Post
T Baetzler
04-02-08 09:30 AM


Re: how to look back past hour
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";


Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-03-08 12:18 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:48 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.