Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Epoch time conversion in CSV

by Marshall (Abbot)
on Sep 25, 2012 at 16:12 UTC ( #995584=note: print w/replies, xml ) Need Help??

in reply to Epoch time conversion in CSV

Might also want to look at Time::Local. This module provides functions that are the inverse of built-in perl functions localtime() and gmtime(). There a number of fancy modules that do time calculations, including Date::Calc, but that is probably more than you need.

I usually do all logging and database entries in GMT and convert to local time for presentation to users. However in your case, sounds like local time is exactly what you want for some kind of a report and you already have something that is basically GMT based. I would mention that epoch is not an ideal format for data enterchange - there are some O/S'es that use a different "epoch time" than what standard Unix uses, but with Windows Perl ports or standard Unix, you will be ok - that's a detail. I like having some kind of readable format in the logs, etc. rather than some kind of integer (like epoch time) anyway.

One suggestion to consider is a date/time format like: 2012-02-23 23:05 (YYYY-MM-DD) (HH-MM) with leading zero's and time in 24 hour format. The beauty of a format like 2012-02-23 23:05 is that you can use an alpha compare (lt,gt,eq) to order or sort dates. Sometimes useful for reports. That way you don't have to go back to epoch time for the comparison.

To force leading zero's, use sprintf or printf with a %02d format spec (adds a leading zero if only one digit). If you are using the format above, you need the leading zero or the sort order won't work out right.

#!/usr/bin/perl -w use strict; printf "%02d\n", $_ foreach ( 2, 23); __END__ 02 23
See: Wiki Epoch

Using a date format like I described about works very well. The boss or the users can import this modified date/time formatted .CSV file into an Excel or other spreadsheet and sort it easily using simple spreadsheet procedures. This is a good thing!

Replies are listed 'Best First'.
Re^2: Epoch time conversion in CSV
by hokey (Initiate) on Sep 26, 2012 at 02:36 UTC
    You should consider using an ISO8601 time format, for all the reasons Marshall suggests. This is the code I usually use:
    use POSIX qw/strftime/; print strftime('%Y-%m-%d %H:%M:%S',gmtime);
    If you want local time use this:
    print strftime('%Y-%m-%d %H:%M:%S',localtime);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995584]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-01-21 07:01 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.