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

Greetings, esteemed monks!

I'm trying to figure out how to best (you can use your own definition of that--mine tends to be loosely aligned with what many people call "elegant.") way to print out the curent date/time in the following format:

ddmmmyy:hh:mm:ss (as in 06jan06:12:23:05)

I have Date::Calc installed but alas no DateTime modules. Even with Date::Calc, as far as I can tell, I'd need to do a substr() on the result of Month_to_Text to get the three-letter abbreviation of the month.

TIA,

Terrence

_________________________________________________________________________________

I like computer programming because it's like Legos for the mind.

  • Comment on Best way to print out a particular date-time format (very specific)

Replies are listed 'Best First'.
Re: Best way to print out a particular date-time format (very specific)
by japhy (Canon) on Jun 08, 2006 at 20:44 UTC
    I'd use:
    use POSIX 'strftime'; my $date = lc strftime("%d%b%y:%H:%M:%S", localtime); # %b returns Mm +m, not mmm

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      perl -MDate::Manip -e'print lc UnixDate("NOW","%d%b%y:%H:%M:%S")."\n"'

      :)

      EDIT: Forgot the lc

      -=( Graq )=-
        • Date::Manip is not core, POSIX is.
        • Date::Manip provides everything plus the bathroom sink, POSIX is lightweight.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Ka-ka click, click, booyah!

      ++japhy!

      _________________________________________________________________________________

      I like computer programming because it's like Legos for the mind.

Re: Best way to print out a particular date-time format (very specific)
by davidrw (Prior) on Jun 08, 2006 at 21:02 UTC
    Based on Recipe #13 in the Date::Calc docs, you can do:
    use Date::Calc qw( Today_and_Now Month_to_Text ); my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now(); printf "%02d%.3s%02d:%02d:%02d:%02d", $day, lc Month_to_Text($month), $year % 100, $hour, $min, $sec ;
      # Rough AEST Time. (Australian Eastern Standard Time) # Get GMTIME, then add zone incrament. # Best to define all zones in a hash or array or something, # and use the appropriate zone incrament depending on what # time zone you want to use/show. # # At least this is how I have always done it. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time); # GMTIME+AEST_TIME(10) my $zone_standard_hour=($hour+10);
Re: Best way to print out a particular date-time format (very specific)
by badaiaqrandista (Pilgrim) on Jun 08, 2006 at 23:17 UTC

    I usually use Time::Piece, which will override CORE::localtime to return an object of Time::Piece class, like this:

    use Time::Piece; my $tp = localtime; print $tp->strftime("%d%b%y:%H:%M:%S");

    Beside that, you can get the year, month, hour, etc by just calling its methods

    -cheepy-
Re: Best way to print out a particular date-time format (very specific)
by ambrus (Abbot) on Jun 09, 2006 at 11:56 UTC

    Since no-one has mentioned it, there's also

    system("date +%d%b%y:%H:%M:%S");
    Of course, I like the Date::Manip solution more.
      Of course, I like the Date::Manip solution more.

      Well, I don't know about favourites, but japhy's use of POSIX would fall into a more 'fit for purpose' answer IMHO.

      There is a section in Date::Manip's documentation labelled should I use Date::Manip - which I think is quite a frank section for an author to include.

      -=( Graq )=-