Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

strftime reference for Win32

by holli (Abbot)
on Feb 24, 2005 at 18:30 UTC ( [id://434177]=perlmeditation: print w/replies, xml ) Need Help??

The Posix-module has a very nice function called strftime() that is capable of formatting dates and times to a human readable format.

But ... the manpage is not very descriptive. It tells you nothing about the meanings of the parameters in the format string.

Unix/Linux users can read about them by typing
$ date --help
(thanks blokhead for advising me on that), but Windows users are lost.

Therefore, in the hope it will help somebody*, i post this little script, that can serve both as a reference and as an example.
use strict; use POSIX qw(strftime); print strftime qq{ a: Day of Week (short, text) %a A: Day of Week (long, text) %A b: Monthname (short, text) %b B: Monthname (long, text) %B c: Full datetime (number, long) %c d: Day (number) %d H: Hour (24 hour) (number) %H I: Hour (12 hour) (number) %I j: Day of year (number) %j m: Month (number) %m M: Minutes (number) %M p: am/pm/empty (text) %p S: Seconds (number) %S U: Week of Year (number) %U w: Day of week (number) %w W: Week of Year (number) %W x: date (number) %x X: time (number) %X y: year (short, number) %y Y: year (long, number) %Y Z: timezone (text) %Z }, localtime;

Notes:
The parameters %U and %W, are not identical. %U starts counting the Week Of Year at sundays, %W starts counting on mondays.
The %p parameter can be empty, depending on the locale of the system.

*and for my own convenience

Update: corrected typo for %M
Update: corrected typo for %I (thanks bmann)
Update: retitled node from "strftime reference" to "strftime reference for win32"


holli, /regexed monk/

Replies are listed 'Best First'.
Re: strftime reference
by bmann (Priest) on Feb 24, 2005 at 18:40 UTC
    holli++ - very useful

    For the sake of completeness, here's sample output:

    a: Day of Week (short, text) Thu A: Day of Week (long, text) Thursday b: Monthname (short, text) Feb B: Monthname (long, text) February c: Full datetime (number, long) 02/24/2005 12:53:26 PM d: Day (number) 24 H: Hour (24 hour) (number) 12 I: Hour (12 hour) (number) 12 j: Day of year (number) 055 m: Month (number) 02 M: Minutes (number) 53 p: am/pm/empty (text) PM S: Seconds (number) 26 U: Week of Year (number) 08 w: Day of week (number) 4 W: Week of Year (number) 08 x: date (number) 02/24/2005 X: time (number) 12:53:26 PM y: year (short, number) 05 Y: year (long, number) 2005 Z: timezone (text) Pacific Standard Time

    Update: This is a Win2K system, Locale set to English(US)

      Yes, I did not think of that. Here is the output for a german system.
      a: Day of Week (short, text) Do A: Day of Week (long, text) Donnerstag b: Monthname (short, text) Feb B: Monthname (long, text) Februar c: Full datetime (number, long) 24.02.2005 19:42:04 d: Day (number) 24 H: Hour (24 hour) (number) 19 I: Hour (12 hour) (number) 07 j: Day of year (number) 055 m: Month (number) 02 M: Minutes (number) 42 p: am/pm/empty S: Seconds (number) 04 U: Week of Year (number) 08 w: Day of week (number) 4 W: Week of Year (number) 08 x: date (number) 24.02.2005 X: time (number) 19:42:04 y: year (short, number) 05 Y: year (long, number) 2005 Z: timezone (text) Westeuropäische Normalzeit

      As one can see, %p is empty here, because the locale is set to 24 hours. %x, %X and %c are different too.


      holli, /regexed monk/
      Check out http://strfti.me for a very readable online reference and sandbox where you can experiment with different strftime format strings.
Re: strftime reference
by Joost (Canon) on Feb 24, 2005 at 23:47 UTC
Re: strftime reference
by lachoy (Parson) on Feb 24, 2005 at 21:17 UTC
    FWIW, the DateTime can translate an object to a string using strftime formats as well. (It's also got a guide to strftime formatting keys in the pod.) Plus with DateTime::Format::Strptime you can use the same format to parse a string into a DateTime object. It also does tons of other hugely useful date stuff.

    Chris
    M-x auto-bs-mode

      Alas, there appears no ppd for this module, so Windows users are probably out of luck. The PPM::Repositories module returns

      datetime http://datetime.perl.org/download Get your DateTime modules here

      but, alas, it didn't work for me...

        Check out the theoryx repository (which you can add with the URL: 'http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer58') -- the versions they have seem to be a little old, but it's still better than POSIX...

        Chris
        M-x auto-bs-mode

Re: strftime reference
by trammell (Priest) on Feb 24, 2005 at 22:52 UTC
    When I do a man strftime, I get:
      The following conversion specifications are supported:
        %a     Replaced by the locale's abbreviated weekday
               name.
        %A     Replaced by the locale's full weekday name.
        %b     Replaced by the locale's abbreviated month
               name.
      ...
    
    Maybe you just need to install better man pages? If you were doing a perldoc POSIX, I sympathize...

    Update: To be fair, the POSIX perldoc contains the text:

      Consult your system's "strftime()" manpage for details
      about these and the other arguments.
    
Re: strftime reference
by blahblahblah (Priest) on Feb 24, 2005 at 19:30 UTC
Re: strftime reference
by demerphq (Chancellor) on Feb 25, 2005 at 17:34 UTC

    Note that strftime format strings are system dependent. You normally should use your systems C documentation to find out what the possible formats are. On Win32 this means using MSDN.

    ---
    demerphq

Re: strftime reference
by Anonymous Monk on Feb 28, 2005 at 11:03 UTC
    The reason that POSIX manual doesn't show the list of escape sequences is that the list depends on your OS. Perhaps you should make it more clear that your program is only of use for Windows users - on my system, I also have:
    • %C: Century number (2 digits)
    • %D: equivalent to %m/%d/%y
    • %e: like %e, leading zero of month replaced with space
    • %E: Modifier alternative format
    • %F: Equivalent to %Y-%m-%d
    • %g: Like %G, but use 2-digit year
    • %G: 4 digit year, but if ISO week belongs to previous year, use previous year.
    • %g: Like %G, but use 2-digit year
    • %h: Equivalent to %b
    • %k: Hours, 0 - 23, single digits preceeded by blank
    • %l: Hours, 1 - 12, single digits preceeded by blank
    • %n: Newline
    • %O: Modifier alternative format
    • %P: As %p, but in lowercase
    • %r: Equivalent to '%I:%M:%S %p'
    • %R: Equivalent to '%H:%M'
    • %s: Number of seconds since epoch
    • %t: Tab character
    • %T: Equivalent to '%H:%M:%S'.
    • %u: Day of week, 1 (Monday) - 7 (Sunday)
    • %V: ISO week number of the year (01 - 53)
    • %z: Time zone as offset of GMT.
    • %%: A % character
    • %+: Time in 'date' format
    Here's a program to see what's available on your OS:
    #!/usr/bin/perl use strict; use warnings; use POSIX; foreach my $ch ('a' .. 'z', '%', '+') { my $lc = strftime "%\l$ch", localtime; my $uc = strftime "%\u$ch", localtime; for ($lc, $uc) { s/\n/\\n/g; s/\t/\\t/g; } printf "%%%s %25s %25s\n", lc $ch, $lc, $uc; } __END__
      I was not aware of that. Thanks for the info, i retitled the node.


      holli, /regexed monk/
Re: strftime reference
by bart (Canon) on Feb 25, 2005 at 15:13 UTC
    For these your legend doesn't look quite right:
    c: Full datetime (number, long) 25/02/05 16:10:05 x: date (number) 25/02/05 X: time (number) 16:10:05

    These aren't what I'd call "numbers"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found