Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Formatting Datetime objects nicely

by dragonchild (Archbishop)
on Nov 28, 2007 at 15:48 UTC ( #653560=perlquestion: print w/replies, xml ) Need Help??

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

Is there a formatter that does the following:
  • If $date is for today, returns 'Today'
  • If $date is for yesterday, returns 'Yesterday'
  • If $date is for tomorrow, returns 'Tomorrow'
  • Otherwise, returns "%B %e, %Y" (or some other strftime specifier)

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re: Formatting Datetime objects nicely
by brian_d_foy (Abbot) on Nov 28, 2007 at 17:31 UTC

    I don't know what your other requiremensts are, but here's a DateTime script to do it. Notice that you can plug in your own formatter class. :)

    #!/usr/bin/perl use DateTime; use DateTime::Format::Strptime; foreach my $time ( map { time - $_ * 86400 } -3 .. 3 ) { my $dt = DateTime->from_epoch( epoch => $time, formatter => DateTime::Format::Strptime->new( pattern => "%B % +e, %Y" ) ); my $string = do { my $duration = $dt - DateTime->now; my $D = $duration->years + $duration->months; my $d_days = $duration->delta_days; if( $D or $d_days < -1 or $d_days > 1 ) { "$dt" } elsif( $d_days < 0 ) { "Yesterday" } elsif( $d_days > 0 ) { "Tomorrow" } else { "Today" } }; print "$time: ", $dt->ymd, " $string\n"; }

    And here's the output:

    1196530263: 2007-12-01 December 1, 2007 1196443863: 2007-11-30 November 30, 2007 1196357463: 2007-11-29 Tomorrow 1196271063: 2007-11-28 Today 1196184663: 2007-11-27 Yesterday 1196098263: 2007-11-26 November 26, 2007 1196011863: 2007-11-25 November 25, 2007
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Formatting Datetime objects nicely
by tuxz0r (Pilgrim) on Nov 28, 2007 at 16:21 UTC
    Time::Fuzzy may do what you want. And, you can always alternate it's use with strftime to meet your fourth point.

    ---
    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Re: Formatting Datetime objects nicely
by hashED (Novice) on Nov 28, 2007 at 17:06 UTC
    You could whip one up yourself using Date::Manip. See http://search.cpan.org/~sbeck/Date-Manip-5.48/Manip.pod and look at ParseDate( ) in particular. You can use it to parse a wide range of date formats into something easy to crunch (you may also appreciate unixdate( ) from the same package). Then, use your favorite conditional logic tom-fooleration method to figure out whether the date is today's, yesterdays, tomorrows, or some other date (use localtime() with no arguments to get today's date and time... there is certainly another way, and probably a better one to do that..) and return whatevah you want. So fun! So easy! So custom! Now if I could just learn to make my own shirts...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-08-08 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?