produce the same output as Unix's date command

Chris Rebert clp2 at rebertia.com
Thu Apr 5 17:51:32 EDT 2012


On Thu, Apr 5, 2012 at 1:52 PM, Jabba Laci <jabba.laci at gmail.com> wrote:
> Hi,
>
> Unix's date command produces this output (example):
>
> Thu Apr  5 22:49:42 CEST 2012
>
> I would like to produce the same output with Python, without calling
> "date" externally. Before doing it I'd like to ask the list if anyone
> has an existing solution for this.

>From POSIX (http://pubs.opengroup.org/onlinepubs/009695399/utilities/date.html
):
    When no formatting operand is specified, the output in the POSIX
locale shall be equivalent to specifying:
    date "+%a %b %e %H:%M:%S %Z %Y"

Therefore:
    from time import strftime
    print strftime("%a %b %e %H:%M:%S %Z %Y")

But note that `date` is locale-sensitive; imitating that would be a
bit more complicated.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list