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

convert iso8601 to epoch time without using additional module

by swissknife (Sexton)
on Mar 13, 2014 at 11:20 UTC ( [id://1078165]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Experts,

I am not a hard code programmer but for a solution purpose i need to convert a date which is in iso8601 to epoch time.

this requirement is on HPUX server and our UNIX admin is not willing to install any module on it. So option is to use what comes by default (5.8.x).

Could someone provide me codes for the same? it will be much appriciated.

Thank You.

  • Comment on convert iso8601 to epoch time without using additional module

Replies are listed 'Best First'.
Re: convert iso8601 to epoch time without using additional module
by jethro (Monsignor) on Mar 13, 2014 at 12:02 UTC

    You could parse iso8601 and use Time::Local to convert to epoch. This module is part of core AFAIK so you should have it installed already.

    If this is too complicated there is always the ninja method of copying a module source like Date::ISO8601 and put it beside your script or insert the needed parts into your script.

Re: convert iso8601 to epoch time without using additional module
by hippo (Bishop) on Mar 13, 2014 at 13:49 UTC
    our UNIX admin is not willing to install any module

    The question then becomes: are you willing to install a module? Since administrative privileges are not required in order to install a module, anyone with a user account on the server can install one there. Yes, even you can use CPAN so there's no need to go reinventing any wheels.

    Good luck!

      BEGIN PASTE

      The first step was learning how to install modules without root. Though I generally have root authority on my boxes, I found it informative and it actually has become the basis for some of my other work. The key has been to set PERL5LIB to include the path you're using to install to, e.g., $ENV{PERL5LIB} = "$ENV{HOME}/lib/perl5:$ENV{HOME}/lib" (I think some of the modules install differently when it's ~/lib instead of, say, ~/myperllib, so I found I had to add both) and then pass in the correct parameters to the .PL script the module comes with, e.g.: $^X -I$ENV{HOME}/lib Build.PL --install_base $ENV{HOME}/lib or $^X -I$ENV{HOME}/lib Makefile.PL LIB=$ENV{HOME}/lib PREFIX=$ENV{HOME}. The -I flag may be optional - but I like being explicit. Once you have everything extracted and then the Makefile or Build files created, go ahead as normal, as if you had root. You just need to ensure PERL5LIB stays set, or that your scripts do a use lib ... properly before trying to use or require anything installed privately like this.

      END PASTE

      Does not work for me. I got the below error when running $ENV{PERL5LIB} = "$ENV{HOME}/lib/perl5:$ENV{HOME}/lib".

      /home/swissknife/ .kshrc{PERL5LIB}: not found

      also i think it will give error when i execute further $^X -I$ENV{HOME}/lib Build.PL --install_base $ENV{HOME}/lib. is ^X is valid here?

        Those are commands to run from inside perl, not directly at shell level. $^X is the name of the current perl executable, for example, as mentioned in perlvar. Don't run them at the shell prompt as they will make no sense there.

        HTH

Re: convert iso8601 to epoch time without using additional module
by kcott (Archbishop) on Mar 13, 2014 at 23:02 UTC

    G'day swissknife,

    Welcome to the monastery.

    Firstly, and this applies to any post you make here, please show the full version number (e.g. 5.8.8). Just type perl -v on the command line to get this information. You've told us that your version is "5.8.x" — there were 10 of those 5.8.0 - 5.8.9.

    [The following is based on the documentation for 5.8.8, which is the earliest available from http://perldoc.perl.org/.]

    This example script shows the basic code you'll need:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Local; my $iso_date = '1970-01-01T01:01:01'; my $expected_epoch = 1 * 60 * 60 + 1 * 60 + 1; my ($date, $time) = split /T/ => $iso_date; my ($year, $mon, $mday) = split /-/ => $date; $year -= 1900; $mon -= 1; my ($hour, $min, $sec) = split /:/ => $time; print "Expected Epoch: $expected_epoch"; print 'Calculated Epoch: ', timegm($sec, $min, $hour, $mday, $mon, $ye +ar);

    Output:

    Expected Epoch: 3661 Calculated Epoch: 3661

    See Time::Local (v5.8.8) and gmtime (v5.8.8) for an explanation of that code.

    If your ISO 8601 dates don't exactly match the format I've used (e.g. they could include time zone information), you'll need to tweak the code to deal with that. If you don't know how to do that, I'd recommend starting with the basics: "perlintro -- a brief introduction and overview of Perl (v5.8.8)".

    -- Ken

      Thanks Ken. Finally i was able to get what i was looking. format of time is different in my case and i was able to deal with that.

Re: convert iso8601 to epoch time without using additional module
by sundialsvc4 (Abbot) on Mar 13, 2014 at 14:52 UTC

    Well, Perl does come with Time::Piece built-in these days.   Does yours?

    If not, perhaps you could cabbage the source-code from the module that you would like to use, or at least the relevant parts of it, into a small class of your own making.   (Be sure to construct .t tests so that you can prove that it still works.   Cabbage tests from the module that you wanted to use, freely, but do run them.)   Even if you cannot, for whatever reason, invoke the very CPAN code-library that you want, at least you do not need to re-invent the algorithmic wheel.

      "Well, Perl does come with Time::Piece built-in these days. Does yours?"

      No it doesn't. OP says he's using "5.8.x"; Time::Piece was added in 5.10.0. See "perl5100delta: New modules".

      -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found