Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Converting POSIX format date/times to epoch seconds

by Anonymous Monk
on Feb 23, 2005 at 04:07 UTC ( [id://433565]=perlquestion: print w/replies, xml ) Need Help??

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

Say I have the users' preferred date time format in a POSIX format string. It's easy to display the local time in a format that they like:
use POSIX; my $format = '%d-%m-%Y %H:%M:%S'; print POSIX::strftime($format, localtime());
However, how do I go the other way? Say I want to record all time in epoch seconds. I know the users' preferred POSIX date formats, so how do I take any format and convert it?

Replies are listed 'Best First'.
Re: Converting POSIX format date/times to epoch seconds
by Corion (Patriarch) on Feb 23, 2005 at 07:28 UTC
      That's exactly what I was after... Thanks!
Re: Converting POSIX format date/times to epoch seconds
by graff (Chancellor) on Feb 23, 2005 at 06:45 UTC
    Given that you know how to parse the users' preferred date format -- i.e. you can easily split it up into components (sec, min, hr, date, month year), another thing you can try is Time::Local -- it's part of the core Perl distro, so everybody has it.

    It exports a function called "timelocal", which just converts those components to seconds since the epoch (i.e. it's the "opposite" of localtime).

Re: Converting POSIX format date/times to epoch seconds
by sgifford (Prior) on Feb 23, 2005 at 04:41 UTC
      I'm not sure how that helps me. Say I have a date - 02/01/2005. Is that the 2nd Jan or the 1st Feb? If I know the POSIX format then I know which assumption to make. As far as I can tell, Date::Parse makes assumptions on the Time Zone, but with an internet app, my users are distributed. It looks like DateTime does the trick, but alas, there is no Windows ppm, and I had trouible building it from source. (No the app is not on Windows, but development is...)
        ...but alas, there is no Windows ppm, and I had trouible building it from source.
        Date::Parse is a member of the TimeDate-distribution, as you can see here.
        There are ppms for TimeDate available at the ActiveState-repositories.

        You can take that as a general rule. If you cannot find a module vie ppm, search at CPAN to see if it is hidden in a distribution, then use ppm to search for that distribution.


        holli, /regexed monk/

        If the only problem is telling apart day-of-month from month, just parse it, format it with strftime, and see if it matches what you started off with. If it's wrong, swith the day-of-month and month and try again.

        Kinda hackish, but seems like it would work. AFAIK, your only other option is to implement the opposite of strftime, which sounds like a lot of work.

Re: Converting POSIX format date/times to epoch seconds
by zentara (Archbishop) on Feb 23, 2005 at 12:48 UTC
    #!/usr/bin/perl use Date::Manip; my $secs = UnixDate('03-DEC-02', '%s'); print "$secs\n"; #see perldoc Date::Manip for details on #formats for seconds, fractional seconds, and timezones

    I'm not really a human, but I play one on earth. flash japh
      While there are other date modules, Date::Manip is one of, if not the most versatile.
      One thing to be aware of though, is that it is very heavy to use in an environment where performance matters.
      I use it when I can, and replace it with small, custom routines in places where it's just too slow.
Re: Converting POSIX format date/times to epoch seconds
by Aristotle (Chancellor) on Feb 23, 2005 at 14:22 UTC

    See HTTP::Date. Yes, the name and choice of distribution for this piece of code if horrible, but it has a refreshingly minimal-frills API if your needs are very simple (ie no date math, just conversions).

    Makeshifts last the longest.

      seems to fit the KISS method but the HTTP distribution is probably not the best. small enough to copy and customize if needed.

        I needed to convert the date from the WMIC command output so I added a block to the code (using a copy anyway for other reasons). it is small enough to customize and here is a small customization (which can probably be improved). I wanted to have a "better" way of determining up time - converting the boot time then taking the delta seemed to be a simple solution. FYI - Win32::GetTickCount was returning 13 days (in ms) on my machine which has been up for 150something days. using this change and time() function to get a delta produced a correct result.

        # WMIC (Windows command line) Date output format from `wmic os get + lastbootuptime` (($yr, $mon, $day, $hr, $min, $sec) = /^ (\d{4}) # year (\d\d) # numerical month (\d\d) # day (\d\d) # Hour (\d\d) # Min (\d\d) # Sec /x) ||

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found