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

I have a timestamp for an upcoming event stored in a berkely database. I need to convert the YYYYMMDD into Day (string), Month (string) Month (int), Year (int) - ex: 20000604 into Friday, May 4, 2000 any ideas? Thanks in advance, Sean

Replies are listed 'Best First'.
Re: converting timestamps
by lhoward (Vicar) on Jun 01, 2000 at 04:01 UTC
    Time::ParseDate can be used to parse many date formats into unix timestamps. If your format is not directly it would not be hard to do a little string manipulation to convert YYYYMMDD to one of the supported formats (like YYYY/MM/DD) Then you can use Time::CTime to format the timestamp anyway you want.

    If the dates you are interested are out of the valid range for UNIX timestamps (1970-2034) then Date::Manip should fit the bill with similar parsing and formatting features.

Re: converting timestamps
by takshaka (Friar) on Jun 01, 2000 at 03:57 UTC
  • Roll your own code using the standard TimeLocal module to convert to epoch seconds and localtime to get the weekday; or,
  • Use one of the date modules (like Date::Calc, Date::Manip) from CPAN
Re: converting timestamps
by cei (Monk) on Jun 01, 2000 at 04:02 UTC
    If on a *nix box, the following may work:

    my $event = "20000504"; #or the value of your BDA hash open(DT,"date -d $event '+%A, %B %e, %Y' |"); my $date = <DT>; chop($date); close DT;
RE: converting timestamps
by Adam (Vicar) on Jun 01, 2000 at 04:49 UTC
    Really the only pain here is the day of the week. The rest is easy. Just regex the stamp into its three components and use the month as an index into an array. Alas, the day of the week involves lots of math. Here is my sample (as yet un-optimized) code:
    sub TimeStamp($) # Pass the stamp in as YYYYMMDD { $_=$_[0]; if( /(\d{4})(\d{2})(\d{2})/ ) { my $month = (Null, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep +, Oct, Nov, Dec)[$2]; my $day = int($3); print "$month $day, $1" } else { warn "Invalid input to TimeStamp()" } }
Re: converting timestamps
by Anonymous Monk on Jun 01, 2000 at 17:16 UTC
    Thanks to all of you - I got it working but all of you have have given me different insights into date/time manipulation. Will Time::Calc work to do date comparisons? Obviously one can't say if $timeA =< $timeB.... Again, Thanks! this is one of the most supportive Perl sites I have ever seen!