Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Optimal Date conversion? (Too much information)

by McMahon (Chaplain)
on Dec 29, 2004 at 16:14 UTC ( [id://418010]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks...

I've been reading Date::Calc and Date::Manip and I've become more confused than when I started. I want to convert a localtime date like so:

Wed Dec 29 08:17:04 (localtime value)
to a different format like so:
 04-12-29_08:17:04
I would appreciate any suggestions as to the most efficient and readable way to do this.

Replies are listed 'Best First'.
Re: Optimal Date conversion? (Too much information)
by steves (Curate) on Dec 29, 2004 at 16:22 UTC

    Using Date::Manip:

    use strict; use Date::Manip; my $from = 'Wed Dec 29 08:17:04'; my $to = UnixDate($from, "%y-%m-%d_%H:%M:%S"); print "$to\n";

    That may not be the fastest method, since Date::Manip is pure Perl and is a bit slow. But it's pretty clear and allows both the input and output format to change as needed.

Re: Optimal Date conversion? (Too much information)
by borisz (Canon) on Dec 29, 2004 at 16:27 UTC
    use POSIX qw/strftime/; print strftime("%y-%m-%d_%H:%M:%S", localtime);
    Boris
Re: Optimal Date conversion? (Too much information)
by trammell (Priest) on Dec 29, 2004 at 16:28 UTC
    #!/usr/bin/perl -wl use strict; use Date::Parse; my $from = 'Wed Dec 29 08:17:04'; print str2time($from); __END__ 1104329824
Re: Optimal Date conversion? (Too much information)
by dragonchild (Archbishop) on Dec 29, 2004 at 16:30 UTC
    You actually don't need either one.
    use constant SECOND => 0; use constant MINUTE => 1; use constant HOUR => 2; use constant DAY => 3; use constant MONTH => 4; use constant YEAR => 5; use constant WEEKDAY => 6; my @time = localtime(time); my $string = $time[WEEKDAY] + 1 . '-' . $time[MONTH] + 1 . '-' . $time[DAY] . '_' . $time[HOUR] . ':' . $time[MINUTE] . ':' . $time[SECOND] ;
    Or, if you're comfortable with join(), you could replace the last line with:
    my $string = join('-', $time[WEEKDAY] + 1, $time[MONTH] + 1, $time[DAY]) . '_' . join(':', @time[HOUR, MINUTE, SECOND]) ;

    But, it's probably better to work with any of the DateTime modules on CPAN. You might want to look at DateTime, which is the up-and-coming set of modules that should do for date/time manipulation/calculation that DBI did for database access.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Optimal Date conversion? (Too much information)
by friedo (Prior) on Dec 29, 2004 at 16:28 UTC
    I would just use the list context of localtime for this.
    my @d = ( localtime )[5,4,3,2,1,0]; $d[0]-=100; # fix the year printf("%02.d-%02.d-%02.d_%02.d:%02.d:%02.d", @d);

    Output:
    04-11-29_11:29:37
Re: Optimal Date conversion? (Too much information)
by dws (Chancellor) on Dec 29, 2004 at 17:24 UTC

    I want to convert a localtime date like so... I would appreciate any suggestions as to the most efficient and readable way to do this.

    If by "efficient" you mean that you have a large number of these to translate, and that you really want to roll your own instead of using one of the several decent (but sometimes bloated) data manipulation packages, consider doing a hash lookup. Pre-generate a mapping like

    my %date_convert = ( ... 'Dec 29' => '04-12-29', ... );
    and then you can do something like
    my $day_re = '(?:Mon|Tue|...|Sun)'; my $mon_re = '(?:Jan|Feb|...|Dec)'; my $time_re = '\d\d:\d\d:\d\d'; $text =~ s/$day_re ($mon_re \d+) ($time_re)/$date_convert{$1} . "_" +. $2/ge;

    You didn't show us where the year comes in, so that's an open issue you'd need to deal with.

    Pregenerating the conversion hash is left as an exercise.

Re: Optimal Date conversion? (Too much information)
by TedPride (Priest) on Dec 29, 2004 at 22:08 UTC
    The following will do your conversion for you, assuming that you're trying to do something more complex than just output a localtime value in a different format:
    use strict; use warnings; my $date = 'Wed Dec 29 08:17:04'; $date = convert($date); print $date; BEGIN { my %c_mon = ('Jan',1,'Feb',2,'Mar',3,'Apr',4,'May',5,'Jun',6, 'Jul',7,'Aug',8,'Sep',9,'Oct',10,'Nov',11,'Dec',12); sub convert { my ($mon, $day, $rest) = $_[0] =~ /\w+ (\w+) (\d+) (.+)/; $mon + = $c_mon{$mon}; @_ = localtime(); $_[4]++; my $year = $_[5] % 100; $year-- if "$mon$day" gt "$_[4]$_[3]"; $year = 99 if $year == +-1; return sprintf('%02d', $year) . "-$mon-$day\_$rest"; } }
    As you can see, much of the effort is expended in determining the year, which isn't given in the original date. I've assumed that a date with a month / day after the current month / day is one in the previous year, and that all other dates are in the current year.

    If you just want a localtime value in the format given, the following works:

    use strict; use warnings; @_ = localtime(); my $date = sprintf('%02d-%02d-%02d_%02d:%02d:%02d', $_[5] % 100, $_[4] + 1, $_[3], $_[2], $_[1], $_[0] +); print $date;
    I'm assuming you want a 24-hour time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found