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

Dear Monks,

I'm trying to convert a date to seconds (epoch).
Basically my problem is that I might have a year and a julian day only ..... how to use timegm which seems to want seconds, minutes, hours, day of the week etc ?

Thanks in advance
Luca

Replies are listed 'Best First'.
Re: From a date to epoch seconds
by Aristotle (Chancellor) on Nov 18, 2005 at 14:35 UTC

    It’s probably easiest to use POSIX::strptime in your case.

    Makeshifts last the longest.

      Looks good, although I cannot find any good documentation on POSIX::strptime. Any suggestions for a place to look ?
      I think I need some info on the "Format" and the "string" too!!

      Thanks a lot
      Luca
Re: From a date to epoch seconds
by davorg (Chancellor) on Nov 18, 2005 at 14:51 UTC

    You want DateTime::Format::Strptime.

    #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $date = '2005 322'; # 322nd day of 2005 my $p = DateTime::Format::Strptime->new(pattern => '%Y %j'); my $dt = $p->parse_datetime($date); print $dt->epoch, "\n", $dt->dmy, "\n";

    Oh, and by the way, what you have there is not really a Julian Day.

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

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

Re: From a date to epoch seconds
by Mutant (Priest) on Nov 18, 2005 at 14:40 UTC
    I'd use DateTime. It does a lot more than you need, but if you're working with dates, you might need some of the extra functionality later on.

      Well, the easiest way to use DateTime for what he needs is… DateTime::Format::Strptime. In other words, he doesn’t lose anything by migrating up from POSIX::strptime. To be sure, DateTime is a fine suite of modules and I use it whenever I need to manipulate dates and times, but the only thing most code needs to do with dates and times is to accurately convert between representations, in which case DateTime is more hassle than help, and strptime/strftime are entirely sufficient.

      Makeshifts last the longest.

Re: From a date to epoch seconds
by ikegami (Patriarch) on Nov 18, 2005 at 14:59 UTC