9

I have a csv file with the datetime in unixtimestamp format with milliseconds and timezone information in milliseconds as well. I want to convert this into a more usable datetime format for further processing.

For example, the time is 1437323953822 and timezone is -14400000.

I can convert the timestamp into a datetime by using

datetime.datetime.fromtimestamp(1437323953822/1000)

But how do I now incorporate the timezone which is -4 UTC time from what I know.

(-14400000 / 1000 / 60 / 60) = -4

How do I use this timezone to get the actual time?

sfactor
  • 12,592
  • 32
  • 102
  • 152

4 Answers4

6

fromtimestamp can also take another parameter for the timezone, a subclass of tzinfo:

classmethod datetime.fromtimestamp(timestamp[, tz])

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

Else tz must be an instance of a class tzinfo subclass, and the timestamp is converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
4

fromtimestamp() already returns your local time i.e., you don't need to attach the utc offset if fromtimestamp() determines it correctly automatically:

#!/usr/bin/env python
from datetime import datetime

local_time = datetime.fromtimestamp(1437323953822 * 1e-3)
# -> datetime.datetime(2015, 7, 19, 12, 39, 13, 822000)

fromtimestamp() may fail in some cases e.g., if the local timezone had a different utc offset in the past and fromtimestamp() does not use a historical timezone database on a given platform (notably, Windows). In that case, construct the local time explicitly from utc time and the given utc offset:

#!/usr/bin/env python
from datetime import datetime, timedelta

utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=1437323953822)
utc_offset = timedelta(milliseconds=-14400000)
local_time = utc_time + utc_offset
# -> datetime.datetime(2015, 7, 19, 12, 39, 13, 822000)

Python always expects POSIX Epoch and therefore it is ok to hardcode it. The explicit formula may be more precise (no rounding error) and it may accept a wider range of input timestamps (fromtimestamp() range depends on platform and may be narrower than the corresponding datetime range).

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • what is wrong with the answer that it deserves a downvote? How could it be improved? – jfs Aug 06 '19 at 14:04
3

This question is old but I want to give a slightly more comprehensive answer.

  1. About the unix timestamp:
    The timestamp is the number of seconds (or milliseconds) elapsed since an absolute point in time, midnight of Jan 1 1970 in UTC time. (UTC is Greenwich Mean Time without Daylight Savings time adjustments.)

  2. fromtimestamp does convert the unix timestamp to your platform's time. If you are working across different platforms, it is important to set the platform's timezone correctly. If you want it to be in UTC instead, then utcfromtimestamp should be used instead.

  3. To answer OP's question directly, the following code will create a timezone based on the offset.

from datetime import datetime, timezone, timedelta

ts = int('1604750712')
tz = timezone(-timedelta(hours=4))

print(datetime.fromtimestamp(ts, tz).strftime('%Y-%m-%d %H:%M:%S'))

timezone object is an concrete class of tzinfo, I have initiated it with a negative offset of 4 hours from UTC.

Ernest Han
  • 395
  • 1
  • 3
  • 10
1
from datetime import datetime
import pytz # pip install pytz

tz = pytz.timezone('Asia/Dubai')
ts = int('1604750712')
print(datetime.fromtimestamp(ts,tz).strftime('%d-%m-%Y %H:%M:%S'))