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

What is your preferred platform independent alternative to POSIX->strftime?

I often recommend POSIX->strftime on forums since it's part of CORE, and therefore won't require any module installation. However, it does fail in a few ways:

  1. 1) It's functionality differs across platforms as reported in previous threads on perlmonks:

    1. [Explained] Platform-dependent behavior observed in POSIX::strftime
    2. strftime reference for Win32
  2. 2) The documentation therefore also differs across platforms:

    1. unix: "man strftime"
    2. windows: MSDN strftime

    I've often used the "I'm feeling lucky" reference for strftime in a pinch, but that doesn't match any OS I use completely.

  3. 3) It fails transparently.

    Whenever an unrecognized format is used, it simply doesn't parse the string. To debug this you must attempt to use each specifier individually to determine which one failed.

I even attempted to limit myself to the "safer" specifiers reported by POSIX, aAbBcdHIjmMpSUwWxXyYZ%, but they aren't platform independent either.

I'm therefore looking for suggestions of CPAN alternatives that will work the same regardless of platform. Ideally it would be able to be a drop in replacement for previous uses of my $date = strftime $fmt, localtime;. The two that I'm currently examining are:

  1. Alternative 1) Date::Format: has a strftime function but annoyingly is prototyped to require an array as the 2nd argument, my $date = strftime $fmt, @{[localtime]};

  2. Alternative 2) DateTime: Does work as my $date = DateTime->now->strftime($fmt);, but doesn't have a constructor that accepts the 'struct tm' data structure returned by localtime

Do you have a preferred module for formatting dates that you recommend to programmers on forums? Is the platform dependent nature of POSIX a non-issue, or is it good that I want a module that will work the same regardless of the local environment?

Replies are listed 'Best First'.
Re: CPAN alternative to POSIX strftime
by Khen1950fx (Canon) on May 23, 2011 at 22:31 UTC
    I would use Time::localtime because it's core and uses the struct tm data structure of localtime.
Re: CPAN alternative to POSIX strftime
by Anonymous Monk on May 23, 2011 at 22:35 UTC
    but doesn't have a constructor that accepts the 'struct tm' data structure returned by localtime

    Why is that important?

    Do you have a preferred module for formatting dates that you recommend to programmers on forums?

    DateTime, always, because all the alternatives are full of caveats, and that kind of PITA interest me less and less.

Re: CPAN alternative to POSIX strftime
by DrHyde (Prior) on May 25, 2011 at 09:42 UTC

    The only module that I even consider for anything to do with dates and times these days is DateTime, because I never need to just print the current time with a fancy format. I always need to do at least some manipulation of it, or can envisage having to do that in the future. And if you use anything else for date/time arithmetic you are in a state of sin.

    I've not looked in depth at its implementation of strftime, but from a quick glance at the source it appears to be its own pure-perl implementation, so even if it has bugs I've not noticed at least they should be portable bugs :-)