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

I have text files that I am parsing data out of and putting into an HTML format. On my page, I have columns set up for the different values. For example, June 3 would appear under Date and 4:05 CT would appear under time. Below is what the text file looks like.
4|Milwaukee Brewers|18|Houston Astros|6/3/2001|16:05 4|Milwaukee Brewers|18|Houston Astros|6/4/2001|18:05 4|Milwaukee Brewers|18|Houston Astros|6/5/2001|18:05
I am trying to change the way the date appears for my new pages. Something similar to this Sun. @ 4:05 for the game that will be played on the third. Any suggestions would be greatly appreciated.

Replies are listed 'Best First'.
Re: Changing a Date format
by arturo (Vicar) on Jun 21, 2001 at 00:41 UTC

    I take it what you want is something that gets you the day of the week and the hour and minute of the day. Judicious use and parsing of the output of scalar localtime($time), where $time is the value of a UNIX timestamp, should help you out here (see perldoc -f localtime for more on the output of scalar localtime).

    To get that UNIX timestamp, take a look at Time::Local : basically, you pass the timelocal function defined therein a year, month, and day, and it produces a timestamp for you.

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Changing a Date format
by larryk (Friar) on Jun 21, 2001 at 00:41 UTC
    Try Date::Manip for date formatting
    I would suggest the use of a templating system for outputting HTML -- I use CGI::FastTemplate and can vouch for its simplicity and extensibility.

    "Argument is futile - you will be ignorralated!"

Re: Changing a Date format
by Desdinova (Friar) on Jun 21, 2001 at 09:10 UTC
    I have found Time::ParseDate to be really good at working with dates. It will convert a date/time stamp text like you have into the std unix date format that you can use localtime on to pull out the pieces you need and put them into your text
Re: Changing a Date format
by davorg (Chancellor) on Jun 21, 2001 at 12:13 UTC

    The secret of dealing with dates in Perl is to convert your value into an "epoch seconds" value. You can do that using timelocal from Time::Local. In your case, it would be done like this (assuming your data line is in $_):

    use Time::Local; # get date and time my ($date, $time) = (split /\|/)[4, 5]; # get dmy my ($d,$m,$y) = split /\//, $date; # get hours & mins my ($hr, $min) = split /:/, $time; # convert year and month into 'correct' forms $y -= 1900; --$m; # get epoch (use 0 for seconds) my $epoch = timelocal(0, $min, $hr, $d, $m, $y);

    The easiest way to get formatted dates from epoch seconds is to use POSIX::strftime, like this:

    use POSIX 'strftime'; my $fmt_date = strftime('%Y-%m-%d', localtime($epoch));

    Please don't use Date::Manip unless you can't find any other way to do what you want. It's a huge inefficient module and using it will really slow your script down.

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

    Perl Training in the UK <http://www.iterative-software.com>