23

I'm probably missing an obvious platform difference that I should be accommodating but I'm getting this when trying to do a time format (Python2.7)...

in Linux env:

>>> import time
>>> time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime())
'Tue, 29-May-2012 21:42:04 GMT'

in Windows:

>>> import time
>>> time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format string

The tuples returned from time.gmtime() appear to be the same, so I'm not completely sure what I need to change.

1
  • just to make sure, you are using the exact same versions of Python on both platforms? Commented May 29, 2012 at 21:49

3 Answers 3

21

In general, you'll find that python time.strftime() supports the same set of format specifiers as the platform (or that platform's libc to be more specific) it runs on. However, only a subset of these is portable. See http://docs.python.org/library/time.html for a list. To quote the docs:

Additional directives may be supported on certain platforms, but only the ones listed here have a meaning standardized by ANSI C.

In this case, %T can be replaced by %H:%M:%S.

Sign up to request clarification or add additional context in comments.

2 Comments

Here is a list of format characters that are supported by the Windows strftime: msdn.microsoft.com/en-us/library/fe06s4ak.aspx
A small gotcha: Linux/Posix/Unix strftime seems to silently ignore [some] invalid formatting, which cause Windows fatal exception.
0

I don't see the %T format directive listed in the Python documentation - must not be available/implemented for the Windows platform (I tried with both Python v2.7.3 and 3.2.3 just now)

Comments

0

I found that I had this problem because MacOS supports "%D" and I had to replace it with "%m/%d/%Y" to make it cross-platform. In general, combining 'simple' format strings like %m seems to be safer than using 'aggregate' strings like %T or %D.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.