4

I wrote a program using Python 3.0 that generated these formatted time values using the following line of code, and I recorded a lot of data with it.

I did, however, use %s (lowercase) instead of %S (uppercase). It doesn't seem to run on Windows and throws an error, but on my Linux devices it runs and generates an output like the one below. What does this "seconds" number represent?

I checked all the documentation for this and couldn't find what this does anywhere. I've recorded a lot of data with this and if there was a way to convert the number into seconds that would save a considerable amount of time.

Code: datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%s')

Output:

2019/08/06 10:34:1565051655
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan Olsen
  • 59
  • 3
  • Why are you not using `%S`, given that it's documented to do what you want? Still a legitimate question, but I'm just curious. – Mad Physicist Aug 10 '19 at 11:19
  • 1
    @donkopotamus. I don't think that's a dupe exactly – Mad Physicist Aug 10 '19 at 11:21
  • `%s` is not documented because it's not required by the 1989 C standard; read the paragraphs *before* the table in https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. – jonrsharpe Aug 10 '19 at 11:23
  • 1
    It was just an accident . I still find it strange that it's not documented anywhere though, even if it's not required by the standards – Ryan Olsen Aug 10 '19 at 11:40
  • 1
    *"To see the full set of format codes supported on your platform, consult the strftime(3) documentation."* <- it'll be documented there. – jonrsharpe Aug 10 '19 at 11:53
  • I also consider this as a legitimate question. I discovered this behaviour when accidentally putting lowercase 's' instead of uppercase one in format parameter and wondered what kind of result it produced and why it is not documented anywhere on Python's website. Thanks for asking! – Rocckk Aug 13 '20 at 09:44

1 Answers1

0

It represents seconds since the Unix epoch. You could use them to recover the original datetime (including seconds) using datetime.datetime.fromtimestamp

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
  • 2
    Well, [the first answer](https://stackoverflow.com/a/11743262/550094) to the suggested duplicate shows that it is not so simple, see in particular the warnings about timezones. – Thierry Lathuille Aug 10 '19 at 11:16
  • The question is ultimately about recovering the “second” information from the accidental use of `%s` ... that should be unaffected by the interplay of UTC to `localtime` – donkopotamus Aug 10 '19 at 11:22