1

I have timestamp which is a time object and trying to convert it to a datetime object because datetime has stonger capabilities and I need to use some function that only datetime has.
The reason I'm starting with time is because datetime doesn't support milliseconds which the original string contains.

What is the easiest way to do it?

kaki gadol
  • 1,116
  • 1
  • 14
  • 34
  • "*because datetime doesn't support milliseconds* - what makes you think that? Just because there is no *attribute* "milliseconds" doesn't mean you cannot work with them - just multiply by 10^3 and use the microseconds attribute ;-) – FObersteiner Mar 02 '22 at 13:33
  • https://docs.python.org/3/library/time.html#time.strftime `time.strftime` doesn't support printing milli or microsecond. unlike `datetime.strftime` https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior . (and the same about `strptime`..) – kaki gadol Mar 02 '22 at 13:40
  • your question says nothing about `strftime`. please clarify if this should be helpful for others. – FObersteiner Mar 02 '22 at 13:41
  • btw. there also seems to be confusion about datetime and time objects vs. the modules with the same name. There is e.g. a datetime.time class, where you can use strftime without problems to create a string with milliseconds precision: `datetime.time(1,2,3,444000).strftime('%H:%M:%S.%f')[:-3]` – FObersteiner Mar 02 '22 at 13:44
  • They question talks about the broad issue of converting objects between the two common packages used when working with time and dates, I looked the simple way to convert the object in order to apply it in my specific use case (which uses `strftime` and `strptime` for different reasons..). This is the reason I posted the question and answer this way, for someone looking for the broader question be able to solve it easily – kaki gadol Mar 02 '22 at 13:49
  • The broad answer is: preferably use the datetime module exclusively. And again, the term `time object` is confusing as described above. You seem to mean `time.struct_time object`. And I think that has been answered [here](https://stackoverflow.com/q/1697815/10197418). – FObersteiner Mar 02 '22 at 13:51
  • 1
    I referred it as time object because it's the return value of `time.strptime`.. BUT, you are right.. I'll flag the question as duplicate. Thanks for the interesting discussion – kaki gadol Mar 02 '22 at 14:05

1 Answers1

0

assuming timestamp is an time object and datetimestamp will be the datetime object:

from datetime import datetime
import time

timestamp = time.strptime('2022-03-02 02:45:12.123', '%Y-%m-%d %H:%M:%S.%f')
datetimestamp = datetime(*timestamp[:5])

It works because time is actually a tuple with the following structure (year, month, day, hour, minute, seconds....), and the datetime __init__ expects the same sequence of variables

kaki gadol
  • 1,116
  • 1
  • 14
  • 34
  • `timestamp` is a [time.struct_time object](https://docs.python.org/3/library/time.html#time.struct_time) for clarification. But why not simply `datetimestamp = datetime.fromisoformat('2022-03-02 02:45:12.123')` ? – FObersteiner Mar 02 '22 at 13:47