Skip to content
Advertisement

Tag: datetime

Convert datetime to Unix timestamp and convert it back in python

I have dt = datetime(2013,9,1,11), and I would like to get a Unix timestamp of this datetime object. When I do (dt – datetime(1970,1,1)).total_seconds() I got the timestamp 1378033200. When converting it back using datetime.fromtimestamp I got datetime.datetime(2013, 9, 1, 6, 0). The hour doesn’t match. What did I miss here? Answer What you missed here is timezones. Presumably you’ve

How to monkeypatch builtin function datetime.datetime.now?

I’d like to make sure that datetime.datetime.now() returns a specific datetime for testing purposes, How do I do this? I’ve tried with pytest’s monkeypatch But this gives me the error TypeError: can’t set attributes of built-in/extension type ‘datetime.datetime’ Answer As the error tells you, you can’t monkeypatch the attributes of many extension types implemented in C. (Other Python implementations may

A faster strptime?

I have code which reads vast numbers of dates in ‘YYYY-MM-DD’ format. Parsing all these dates, so that it can add one, two, or three days then write back in the same format is slowing things down quite considerably. Any suggestions how to speed it up a bit (or a lot)? Answer Python 3.7+: fromisoformat() Since Python 3.7, the datetime

How can I make a python numpy arange of datetime

I have some input data, with timestamps in the input file in the form of hours from the date time specified in the filename. This is a bit useless, so I need to convert it to python datetime.datetime objects, and then put it in a numpy array. I could write a for loop, but I’d like to do something like:

Weird timezone issue with pytz

A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue? In fact I’m getting different behavior between Answer Time zones and offsets change over the years. The default zone name and offset delivered when pytz creates a timezone object are the earliest ones available for that zone, and sometimes they can seem

Advertisement