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.


In reply to Re: Timeleft subroutine by Marshall
in thread Timeleft subroutine by robertw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.