jjhorner has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking, but I can't find a way to get perl to give me time since the epoch. Any hints? Thanks.

Replies are listed 'Best First'.
Re: Time since epoch
by vroom (His Eminence) on May 16, 2000 at 22:22 UTC
    time will do just that for the current time. If you want to convert another time to epoch seconds check out Time::Local ie
    use Time::Local; my $epoch_secs=timelocal( $sec, $min, $hrs, $dd, $mm-1, $yy);


    vroom | Tim Vroom | vroom@cs.hope.edu
Re: Time since epoch
by lhoward (Vicar) on May 16, 2000 at 22:26 UTC
    The Date::Calc module will let you do date computations back to the year 1 C.E.. The Delta_Days function should be just what you need.
    my $dd=Delta_Days(1,1,1,$cur_yr,$cur_mo,$cur_da);
    If you're interested in how to do the calculation by hand I highly recommend checking out Calenderical Calculations by Derschowitz and Reingold. Should be available at any bookstore w/ a reasonably complete techincal section.
    Looking at vrooms answer and mine reminds me that you need to be careful in how you define epoch. In general terms eopch is the start-date of your calendar. So when does your calendar start ???
    1. Jan 1, 1970 (unix Epoch)
    2. Jan 1, 1900 (another epoch used in computers)
    3. Jan 1, 1 (Gregorian epoch)
    4. etc..
    In my answer I had assumed the Gregorian epoch. If you want your "time from epoch" in seconds and your epoch is 1/1/1970 then the time() function returns that.
RE: Time since epoch
by Anonymous Monk on May 17, 2000 at 01:51 UTC
    The function/sub time() returns the sec. since the epoch. perl -e 'print time(), "\n";' For example: $var = time(); $var2 = ((($var / 365) / 24) / 60); print "The minutes since the epoch is: $var2 \n";
Re: Time since epoch
by turnstep (Parson) on May 16, 2000 at 22:51 UTC
    I know the Mac uses Jan 01, 1904 and VMS uses November 17, 1858 (!!) but what uses Jan 1, 1900?
      The unix localtime function returns years as # of years since 1970 (even though the dates are stored internally as seconds since 1970). Giving an apparent epoch of 1900 (even though its really 1970). Also, most databases represent dates as YYYY-MM-DD, but some don't allow dates before 1900. 1900 as an epoch is more common in application programs than at the OS level.
        I understand all that, I was just wondering if anyone knew of an *OS* that actually used Jan 1, 2000 as their epoch. You'd think it would be more common. Then again, if unix really *did* use 1900, we'd be in a lot of trouble. It would have been a lot easier if they had just used a 64-bit number and started at year "0". :)
RE: Time since epoch
by merlyn (Sage) on May 17, 2000 at 02:24 UTC