Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Timeleft subroutine

by Marshall (Prior)
on Aug 30, 2012 at 13:01 UTC ( #990739=note: print w/ replies, xml ) Need Help??


in reply to Timeleft subroutine

For the interface, I would be thinking about seconds as a return value from timeleft(). Perhaps like something below or maybe not? Your idea has some problems, like what happens at midnight?

In general I would design the subs to work in seconds from the "epoch" date/time and have some conversion routines - Yes, there are exceptions, but I don't know enough about your app.

There are also some Perl modules: Date::Time and Date::Calc that work well.

#!/usr/bin/perl -w use strict; use Time::Local; # a CORE function (no install needed) # $time = timelocal($sec,$min,$hour,$mday,$mon,$year); print "current time is: ".localtime(),"\n"; print "seconds left until 8/30/2012 5:32 AM\n"; print seconds_left(2012,8,30,5,32,0), "\n"; sub seconds_left { my ($year, $month, $dayOfMonth, $hour, $minutes, $seconds) = @_; my $expiration_seconds = timelocal($seconds, $minutes, $hour, $dayOfMonth, $month-1, $year); my $current_seconds = time(); return ($expiration_seconds - $current_seconds); } __END__ current time is: Thu Aug 30 05:31:30 2012 seconds left until 8/30/2012 5:32 AM 30
This whole time/date stuff can get complicated. I mean like what happens when there is a leap year? What happens when there is a time switch from "normal time" to "daylight savings time"? I guess this year (2012) we had a "leap second".

In general, I would advise that your program work with GMT or what is now called UTC time - yes there is slight difference between these terms - but if you know enough to argue, then you already understand it well enough that we don't have to. Use UTC/GMT internally and convert to "local time" as needed.


Comment on Re: Timeleft subroutine
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990739]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2015-07-06 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (69 votes), past polls