Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How can I convert epoch seconds to date format

by Anonymous Monk
on May 10, 2006 at 14:37 UTC ( [id://548471]=perlquestion: print w/replies, xml ) Need Help??

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

I have to convert local time to a time zone and then put it back to the database it the date format mm/dd/yyyy hr:min:sec. I am trying to do the date conversion using time() and getting the epoch seconds and subtracting the number of hours depending on the time zone. The result is in seconds so I am looking for way to convert it back to the original format. Thanks
  • Comment on How can I convert epoch seconds to date format

Replies are listed 'Best First'.
Re: How can I convert epoch seconds to date format
by gellyfish (Monsignor) on May 10, 2006 at 14:47 UTC

    The most flexible way to do this is to use the strftime() function from the POSIX module that comes with perl. You can do something like:

    use POSIX qw(strftime); + $time = time(); + print strftime("%m/%d/%Y %H:%M:%S",localtime($time));
    Obviously you will use your own value for $time

    /J\

Re: How can I convert epoch seconds to date format
by davorg (Chancellor) on May 10, 2006 at 14:50 UTC

    Use the strftime function from POSIX.pm.

    use POSIX 'strftime'; my $date = strftime '%Y/%m/%d %H:%M:%S', localtime $epoch;

    p.s. I corrected your illogical US date format to something more sensible :)

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Or you could use the user's time settings to decide the order of year, month and day:
      use POSIX 'strftime'; my $date = strftime '%c', localtime $epoch

        Some of the 'aggregate' specifiers don't work on all platforms (yes windows I'm loooking at you) unfortunately.

        /J\

      Thanks a lot....
Re: How can I convert epoch seconds to date format
by davorg (Chancellor) on May 10, 2006 at 15:11 UTC

    One other piece of advice from someone who has been badly burnt by this once or twice in the past...

    If you're storing dates and times in a datebase, then always store the UTC time. Convert from your local timezone to UTC as you put records into the data and convert UTC back to your local timezone as you fetch records from the database. This is guaranteed to make your live easier in the long run.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How can I convert epoch seconds to date format
by eXile (Priest) on May 10, 2006 at 14:51 UTC
    not sure on what you are exactly trying to do, but for conversions from epoch to localtime you can use 'localtime'
    perldoc -f localtime
    
    localtime EXPR
           localtime
                   Converts a time as returned by the time function to a 9-element
                   list with the time analyzed for the local time zone.  Typically
                   used as follows:
    
                       #  0    1    2     3     4    5     6     7     8
                       ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                                   localtime(time);
    
    Or you can use one of the many modules that does dateconversions, my current favorite is DateTime.

      This won't help the OP necessarily, but if you just want to display human-readable date and time without mucking with the data structure, remember that localtime() and gmtime(), when used in scalar context, return a string in the default format defined by ctime(3).

Re: How can I convert epoch seconds to date format
by jeanluca (Deacon) on May 10, 2006 at 15:41 UTC
    Here is a simple script that can convert dates to seconds and back. It also includes fraction of seconds and some other stuff. Maybe you can use it!
    # TimeFormat convert UTC dates to seconds (date2sec) and # seconds to UTC dates (sec2date) in a defined format. # Accepted input dates for &date2sec() are # 2005-03-28 12:00:00.001 # 2005-03-28 # 2005-102 12:00:00 # 2005-102 # 2005102 12:00:00 # 2005102 # 2005,100,12:10:00 # 2005,100 12:10:00.001 package TimeFormat ; use strict ; use Time::Local qw(timegm_nocheck); use POSIX 'strftime'; # convert seconds to a formatted date require Exporter ; #our $SECONDS ; # set when date2sec is called our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $Verbose, $FOR +MAT) ; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw( sec2date date2sec current2sec last_julian_day remov +e_time format ) ; %EXPORT_TAGS = ( Default => [qw(&date2sec)], Both => [qw(&date2sec &sec2date)], All => [qw(&date2sec &sec2date &current2sec &last +_julian_day &remove_time &format)]); $VERSION = 1.0 ; $Verbose = 0 ; # This variable is used for the conversion between dates $FORMAT = "%Y-%m-%d %H:%M:%S.%q" ; # should be set via format() # %Y - year including century or %y - year within century (00-99) # %m - month (1-12) # %d - day of month (1-31) # %j - julian day (000-366) # %H - hour (00-23) or %I - hour (01-12) # %M - minute (00-59) # %S - second (00-59) # %q - milli second (000-999) (not in the documentation) # %s - epoch time (xxxxxxxxxxx.xxx) sub remove_time { my $sec = shift ; &date2sec( sec2date($sec,"%Y%j") ) ; } sub last_julian_day { my $year = shift ; &sec2date(date2sec("$year-12-31"),"%j") ; } sub current2sec { return &date2sec( localtime() ) ; } # This function accepts epoch-seconds and returns a formatted date. sub sec2date { my ($sec, $fmt) = @_; $fmt = $FORMAT if ! $fmt ; # check input format if ( $sec !~ /^\d+(?:\.\d+)?$/ ) { $sec = &date2sec($sec) ; } # is input format correct ? return $sec if ($sec !~ /^\d+(?:\.\d+)?$/) ; ## fractional part of $sec, rounded to 3 digits my $milli = sprintf "%03.0f", 1000 * ($sec - int $sec); # add mili-seconds (%q) $fmt =~ s/%q/$milli/g; # change output-format for milli-seco +nds strftime $fmt, gmtime(int $sec); #strftime $fmt, localtime(int $sec); } # converts a date to epoch seconds sub date2sec { my $time = undef ; if ( @_ > 1 ) { $time = sprintf ("%d-%.3d %.2d:%.2d:%.2d",(1900 + $_[5]),($_[7]+1) +,$_[2],$_[1],$_[0]) ; } else { $time = shift ; } return undef if ! defined $time ; my $seconds ; # decl. of vars. my ( $year, $month, $day, $julian, $hour, $minute, $second, $mil +i) ; # convert year-only-date $time .= "-001" if ( length($time) == 4) ; # date is already in seconds if ( length($time) != 7 && $time =~ /^ \d+ # seconds (?: \.\d+ # milli-seconds )? $/xg ) { $seconds = $time ; } # split date ( with a julian day) # elsif ( $time =~ /\d{4}(?:[-,])?(\d{3})[\s{0,},](\d\d:\d\d:\d\ +d)?/ ) { elsif ( $time =~ /\d{4}(?:[-,])?(\d{3})\b/ ) { ( $year, $julian, $hour, $minute, $second, $mili ) = ( $time =~ m/ (\d{4}) # year (?: # group the day - time portions (?: [-,] # year and julian day sep. with a , or - )? (\d{1,3}) # julian day (?: # group the time portions [\s+,] # date and time sep. by whitespaces or , (\d\d) # hour : (\d\d) # minute : (\d\d) # second + fractions (?: (\.\d+) )? )? # time is optional )? # day - time is optional /xg ); $seconds = &timegm_nocheck($second,$minute,$hour,$julian,0,$year- +1900) + $mili ; } # (date contains month and day) elsif ( $time =~ /\d{4}-\d{2}-\d{2}\s{0,}?(\d\d:\d\d:\d\d)?(?: +\.\d+)?/ ) { ( $year, $month, $day, $hour, $minute, $second, $mili ) = ( $time +=~ m/ (\d{4}) # year - # (\d{2}) # month - # (\d{2}) # day (?: # group the time portions \s+ # one or more whitespace (\d\d) # hour : (\d\d) # minute : (\d\d) # second (?: (\.\d+) )? )? # time is optional /xg ); print STDERR __PACKAGE__." Error: Month out of range: $month\n" if + $month > 12; return undef unless $month < 13 && defined $year ; # only chec +k month $seconds = &timegm_nocheck($second,$minute,$hour,$day,$month +-1,$year-1900) + $mili ; } else { return undef ; } # input date could not be parsed return $seconds ; } # predefine the output format sub format { $FORMAT = shift ; return 1 ; # OK } 1; __END__
    If there are any comments on this module, please let me know!!

    Luca
      One more question on the time.... Is there a way I can get the timezone from localtime? Thanks
        never mind... I got it....
      wow... cheers for this. I develop primariliy in Java JS and python but have had to work with Perl quite a bit. As helpful as this is, I think its quite a shame that you have to hand-crank all this in Perl so you can read milliseconds from a timestamp..

        Well, this thread is 9 years old, and no, you don't have to code your own millisecond handling in Perl - for example DateTime handles everything down to nanoseconds. Dates can be parsed via DateTime::Format::Strptime or any of the other DateTime::Format::* modules.

        FWIW, you also did not have to do any of this in 2006 when jeanluca wrote that code, DateTime/DateTime::Format::Strptime were both available at that time

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://548471]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found