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

I have needed to put the date and time in a specefic format for the ARS helpdesk system in order to move the call status into a resolved state.

The format ARS accepts is mm/dd/yyyy hh:mm:ss AM/PM
The issue here is AM/PM.

I have done this but was wondering if there is a slicker way of doing it.

use strict; use warnings; my $d = &Date(); my $t = &Time(); print "$d - $t\n"; #Set Local Date sub Date() { (my $ss, my $mm, my $hh, my $d, my $mon, my $yr) = localtime(); my $date = sprintf("%04d/%02d/%02d", $yr+1900, $mon+1, $d); return($date); } #Set Local Time sub Time() { my ($ampm); (my $ss, my $mm, my $hh, my $d, my $mon, my $yr) = localtime(); if ( $hh > 12 ) { $hh = $hh - 12; $ampm = "PM"; } else { $ampm = "AM"; } my $time = sprintf("%02d:%02d:%02d %s", $hh, $mm, $ss, $ampm); return($time); }
This works, but I'm sure the gurus have this in two or three lines as opposed to any many as mine.

-----
Of all the things I've lost in my life, its my mind I miss the most.

Replies are listed 'Best First'.
Re: AM/PM in date and time format
by davorg (Chancellor) on Sep 09, 2002 at 13:57 UTC
    POSIX::strftime is your friend.
    use strict; use warnings; use POSIX 'strftime'; print strftime '%Y/%m/%d - %I:%M:%S %P', localtime; print "\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: AM/PM in date and time format
by cfreak (Chaplain) on Sep 09, 2002 at 13:57 UTC

    Actually your solution is quite good, if you just want the time and don't want to use outside modules (i.e. Date::Calc which doesn't do you much good anyway for this problem). You do have a small bug though ... what if it is 12 NOON? With your code

    if($hh > 12) { # hh = 12; $hh = $hh - 12; $ampm = "PM"; } else { $ampm = "AM"; # opps 12 PM just became 12 midnight. }

    Test to see if the hour is 12 and set the $ampm to "PM" in that case without subtracting anything.

    Hope that helps
    Chris

    Lobster Aliens Are attacking the world!
Re: AM/PM in date and time format
by davis (Vicar) on Sep 09, 2002 at 14:01 UTC
    perldoc POSIX and man strftime
    #!/usr/bin/perl use warnings; use strict; use POSIX qw(strftime); print strftime("%I:%M:%S %Y/%m/%d %p", localtime());

    Note I've used YYYY/MM/DD date format here, as you did in your example, but you state ARS wants the date in MM/DD/YYYY - so just change the appropriate letters.

    Update: After looking at davorg's example (Bah - Beaten again :-) ) for a sec, he uses the %P (uppercase) format string - a GNU extension to strftime which returns the am/pm string in lowercase. %p (lowercase) returns it in uppercase - there's consistency for you. Still, it made me read the appropriate manual pages, so thanks davorg and AcidHawk.

    Cheers


    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re: AM/PM in date and time format
by greenFox (Vicar) on Sep 09, 2002 at 14:05 UTC

    use the posix modules strftime() function-

    use POSIX; my $date = strftime("%D %H:%M:%S %p", localtime(time));

    man strftime for all the strftime formating options.

    --
    Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Re: AM/PM in date and time format
by AcidHawk (Vicar) on Sep 09, 2002 at 14:16 UTC
    Thank you everyone,

    Hmm.. why is it that I shy away from POSIX, even the name intimidates me.. oh well I guess this is my natural progression towards becomming a guru.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.