Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl time function

Reply
Thread Tools

perl time function

 
 
lerameur
Guest
Posts: n/a
 
      10-17-2007
Hello,

I know there are lot of function in perl.
Can somebody tell me which function does the following:
I have the : year, month, day, hours I want to increment the hours
everytime the script is run. once the hours reaches 23 to 24 (actually
0), it will increment the days. My concerns are with leap years and
months

thanks
k

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      10-17-2007
lerameur wrote:
> Hello,
>
> I know there are lot of function in perl.
> Can somebody tell me which function does the following:
> I have the : year, month, day, hours I want to increment the hours
> everytime the script is run. once the hours reaches 23 to 24 (actually
> 0), it will increment the days. My concerns are with leap years and
> months


See Date::Calc (Add_Delta_YMDHMS).

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

 
Reply With Quote
 
 
 
 
lerameur
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 10:33 am, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> lerameur wrote:
> > Hello,

>
> > I know there are lot of function in perl.
> > Can somebody tell me which function does the following:
> > I have the : year, month, day, hours I want to increment the hours
> > everytime the script is run. once the hours reaches 23 to 24 (actually
> > 0), it will increment the days. My concerns are with leap years and
> > months

>
> See Date::Calc (Add_Delta_YMDHMS).
>
> --
> These are my personal views and not those of Fujitsu Siemens Computers!
> Josef Möllers (Pinguinpfleger bei FSC)
> If failure had no penalty success would not be a prize (T. Pratchett)
> Company Details:http://www.fujitsu-siemens.com/imprint.html


I used the same localtime I used, but I added hours+1 so that it
output to another time variable and use that time to do my
calculation.
thanks

 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 9:33 am, lerameur <(E-Mail Removed)> wrote:
> Hello,
>
> I know there are lot of function in perl.
> Can somebody tell me which function does the following:
> I have the : year, month, day, hours I want to increment the hours
> everytime the script is run. once the hours reaches 23 to 24 (actually
> 0), it will increment the days. My concerns are with leap years and
> months
>
> thanks
> k



Better to keep the time in seconds and use localtime to extract
year/month/day/hour. Then you can just keep adding 3600 each
hour. This way you don't have to reinvent what is probably the
most frequently reinvented wheel in application programming.

my $mytime = 1192633277;
use constant ONEHOUR => 3600;

$mytime += ONEHOUR;
@t = localtime($mytime);
($year, $month, $day, $hour) = @t[5,4,3,2];

--S




 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-17-2007

Quoth smallpond <(E-Mail Removed)>:
> On Oct 17, 9:33 am, lerameur <(E-Mail Removed)> wrote:
> >
> > I know there are lot of function in perl.
> > Can somebody tell me which function does the following:
> > I have the : year, month, day, hours I want to increment the hours
> > everytime the script is run. once the hours reaches 23 to 24 (actually
> > 0), it will increment the days. My concerns are with leap years and
> > months

>
> Better to keep the time in seconds and use localtime to extract
> year/month/day/hour. Then you can just keep adding 3600 each
> hour. This way you don't have to reinvent what is probably the
> most frequently reinvented wheel in application programming.


Hours (or, at any rate, 'calendar hours') are not necessarily 3600
seconds long, because of leap seconds. When doing calendar calculations,
it's best to use a module that does it right.

Ben

 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 11:53 am, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth smallpond <(E-Mail Removed)>:
>
> > On Oct 17, 9:33 am, lerameur <(E-Mail Removed)> wrote:

>
> > > I know there are lot of function in perl.
> > > Can somebody tell me which function does the following:
> > > I have the : year, month, day, hours I want to increment the hours
> > > everytime the script is run. once the hours reaches 23 to 24 (actually
> > > 0), it will increment the days. My concerns are with leap years and
> > > months

>
> > Better to keep the time in seconds and use localtime to extract
> > year/month/day/hour. Then you can just keep adding 3600 each
> > hour. This way you don't have to reinvent what is probably the
> > most frequently reinvented wheel in application programming.

>
> Hours (or, at any rate, 'calendar hours') are not necessarily 3600
> seconds long, because of leap seconds. When doing calendar calculations,
> it's best to use a module that does it right.
>
> Ben



True. You could be as much as 2 seconds off if the program had
been running for the last 10 years. By the way, how would a
module know when leap seconds have been added? Would it check
with IERS? Which modules take leap seconds into account?
--S

 
Reply With Quote
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 8:53 am, Ben Morrow <(E-Mail Removed)> wrote:
>
> Hours (or, at any rate, 'calendar hours') are not necessarily 3600
> seconds long, because of leap seconds. When doing calendar calculations,
> it's best to use a module that does it right.
>
> Ben
>


a) Who keeps track of these things so that the module works correct?
Just curious.

Let's say it is N seconds since 1-Jan-1970, 00:00:00 (or whatever the
base date-time), stored in variable t1, to 11:59:20 PM on date D, at
the end of which 2 leap seconds get added. Going by your post, it
looks like localtime($t1+45) would return 00:00:03 on date D+1 after
adjusting for 2 leap seconds. b) Is my deduction correct?

How many times have extra seconds been added or deducted since January
1970?

 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      10-18-2007
lerameur wrote:
> On Oct 17, 10:33 am, Josef Moellers <josef.moell...@fujitsu-
> siemens.com> wrote:
>
>>lerameur wrote:
>>
>>>Hello,

>>
>>>I know there are lot of function in perl.
>>>Can somebody tell me which function does the following:
>>>I have the : year, month, day, hours I want to increment the hours
>>>everytime the script is run. once the hours reaches 23 to 24 (actually
>>>0), it will increment the days. My concerns are with leap years and
>>>months

>>
>>See Date::Calc (Add_Delta_YMDHMS).

[...]
> I used the same localtime I used, but I added hours+1 so that it
> output to another time variable and use that time to do my
> calculation.


This was my first solution as well (use POSIX::mktime to convert from
DMYhms to seconds, then add the delta, and then use localtime to convert
back to DMYhms), but as others have pointed out, this doesn't take into
account leap seconds. If this is for a production system, you better
take care of this, or you may be called out of bed early one newyear's day.

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      10-27-2007
On 2007-10-17 15:53, Ben Morrow <(E-Mail Removed)> wrote:
>
> Quoth smallpond <(E-Mail Removed)>:
>> On Oct 17, 9:33 am, lerameur <(E-Mail Removed)> wrote:
>> >
>> > I know there are lot of function in perl.
>> > Can somebody tell me which function does the following:
>> > I have the : year, month, day, hours I want to increment the hours
>> > everytime the script is run. once the hours reaches 23 to 24 (actually
>> > 0), it will increment the days. My concerns are with leap years and
>> > months

>>
>> Better to keep the time in seconds and use localtime to extract
>> year/month/day/hour. Then you can just keep adding 3600 each
>> hour. This way you don't have to reinvent what is probably the
>> most frequently reinvented wheel in application programming.

>
> Hours (or, at any rate, 'calendar hours') are not necessarily 3600
> seconds long, because of leap seconds. When doing calendar calculations,
> it's best to use a module that does it right.


When doing calendar calculations which may possibly include future dates
you cannot use leap seconds. Leap seconds are determined by observation
every six months, so at this point of time it is impossible to know how
many seconds the year 2008 will have.

This is why POSIX timestamps don't use leap seconds. The POSIX timestamp
1199145600 always refers to 2008-01-01 00:00:00 UTC and the POSIX
timestamp 1230768000 always refers to 2009-01-01 00:00:00 UTC,
regardless of the number of actual seconds between these dates.

So, if you really need to know the number of seconds between two
timestamps, you can't just subtract them. OTOH, you can just add 3600 to
get to the next calendar hour, as smallpond suggested.

hp


--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-27-2007

Quoth "Peter J. Holzer" <(E-Mail Removed)>:
> On 2007-10-17 15:53, Ben Morrow <(E-Mail Removed)> wrote:
> >
> > Hours (or, at any rate, 'calendar hours') are not necessarily 3600
> > seconds long, because of leap seconds. When doing calendar calculations,
> > it's best to use a module that does it right.

>
> When doing calendar calculations which may possibly include future dates
> you cannot use leap seconds. Leap seconds are determined by observation
> every six months, so at this point of time it is impossible to know how
> many seconds the year 2008 will have.
>
> This is why POSIX timestamps don't use leap seconds. The POSIX timestamp
> 1199145600 always refers to 2008-01-01 00:00:00 UTC and the POSIX
> timestamp 1230768000 always refers to 2009-01-01 00:00:00 UTC,
> regardless of the number of actual seconds between these dates.
>
> So, if you really need to know the number of seconds between two
> timestamps, you can't just subtract them. OTOH, you can just add 3600 to
> get to the next calendar hour, as smallpond suggested.


OK, now I'm fascinated... what happens when a leap second occurs?
time(3) returns the same value for two seconds in a row? That sounds...
confusing, although I suppose it's similar to what adjtime(2) does...

[Spurious Perl connection: this is similar to the hobbit's system of
treating leap days as not part of the year... ]

Ben

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments