1

When running dateutil.parser.parse("2017-09-19T04:31:43Z").strftime('%s') in Python, I obtain the following error:

ValueError: Invalid format string

What is the problem?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
xha
  • 39
  • 1
  • 8

2 Answers2

1

The same code works for me in Python 2.

from dateutil.parser import parse
print(parse("2017-09-19T04:31:43Z").strftime('%s'))
# 1505813503

or

import dateutil.parser
print(dateutil.parser.parse("2017-09-19T04:31:43Z").strftime('%s'))
# 1505813503

The OP claims that this does NOT work, giving the same ValueError: Invalid format string error.

One explanation is that, according to this post, your option %s doesn't exist. See valid options of time.strftime here.

Why did %s work on my system?

Great answer by Darren Stone from this post

In the Python source for datetime and time, the string STRFTIME_FORMAT_CODES tells us:

"Other codes may be available on your platform.
See documentation for the C library strftime function." 

So now if we man strftime (on BSD systems such as Mac OS X), you'll find support for %s:

%s is replaced by the number of seconds since the Epoch, UTC (see mktime(3))." 

What can you do?

It seems you want the Unix timestamp. As suggested by @jleahy here,

If you want to convert a python datetime to seconds since epoch you should do it explicitly:

datetime.datetime(2012,04,01,0,0).strftime('%s') # '1333234800'
(datetime.datetime(2012,04,01,0,0) - 
 datetime.datetime(1970,1,1)).total_seconds() # 1333238400.0 

In Python 3.3+ you can use timestamp() instead:

datetime.datetime(2012,4,1,0,0).timestamp() # 1333234800.0
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
  • So it seems that it doesn't work out in Python 3.5. I just don't know what goes wrong. – xha Oct 24 '17 at 08:45
  • According to [this post](https://stackoverflow.com/questions/26140502/python-invalid-format-string), your option `%s` doesn't exist. I don't understand why it works for me, which prints the Unix time stamp. That's a good question I should ask. Anyway, check available options [here](https://docs.python.org/2/library/time.html#time.strftime) – kgf3JfUtW Oct 24 '17 at 13:01
  • Very insightful! Let me check it out. – xha Oct 25 '17 at 13:29
0

It may be that you simply typed it wrong. Maybe you meant to use uppercase S, which provides formatting for the seconds. If not, %s as a format string is probably platform-specific.

If you look at this page, you'll see the comment:

Platform specific directives: The full set of format codes supported varies across platforms, because Python calls the platform C library's strftime() function, and platform variations are common.

For example, on Windows, %s (lowercase) is not accepted at all in strftime. Windows, however, will recognize %S (uppercase) and treat the seconds as a zero-padded decimal number.

Another example of an incompatibility across platforms: Windows doesn't recognize hyphens in the format string, while it works on linux. eg:

print(time.strftime("%-I:%M %p")) #this fails on windows

Probably it's best for your code if you stick to the platform independent formatting string options mentioned here: http://strftime.org/

mxmoss
  • 449
  • 4
  • 8