130

I want to compare UTC timestamps from a log file with local timestamps. When creating the local datetime object, I use something like:

>>> local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, 0, 
                                 tzinfo=pytz.timezone('Israel'))

I want to find an automatic tool that would replace thetzinfo=pytz.timezone('Israel') with the current local time zone.

Any ideas?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 1
    Your problem is questionable to begin with. Depending on what you're doing, there's a good chance of producing a race condition close to the daylight savings time changeover (or any other situation where the local time zone changes). – Kevin Oct 21 '15 at 13:35
  • 2
    if by *local time* you mean your OS setting, you can simply get it as tz aware datetime object like `local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, 0).astimezone()` (Python >= 3.6). [[docs]](https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone) – FObersteiner Jun 12 '20 at 10:41

19 Answers19

185

In Python 3.x, local timezone can be figured out like this:

>>> import datetime
>>> print(datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo)
AEST

It's a tricky use of datetime's code .

For python < 3.6, you'll need

>>> import datetime
>>> print(datetime.datetime.now(datetime.timezone(datetime.timedelta(0))).astimezone().tzinfo)
AEST
Flimm
  • 136,138
  • 45
  • 251
  • 267
vbem
  • 2,115
  • 2
  • 12
  • 9
  • 57
    Simply `datetime.datetime.now().astimezone().tzinfo` should already be OK. – Polv Jul 29 '18 at 10:23
  • 5
    @Polv `datetime.datetime.utcnow().astimezone().tzinfo` also seems to be correct and to me is easier to read as it explicitly states it'd use UTC to begin with. Since no `datetime` is initially bound to any timezone it shouldn't make a difference internally. – sjngm Aug 17 '18 at 20:59
  • 13
    @Polv @sjngm not quite as that only works for python >= 3.6. You cannot call `astimezone()` on naive datetimes prior to 3.6. – Fynn Becker Aug 22 '18 at 09:28
  • 4
    Clever, but will it work if your program runs long-term over for example a DST change? – Wilmer Sep 03 '19 at 22:01
  • 1
    This line of code returns UTC on my python terminal (inside a docker container?) but I'm in GMT+4 – Jose Georges Apr 09 '20 at 16:35
  • Brilliant. This should be the answer. – Travis Griggs Jul 23 '20 at 19:25
  • @Jose docker image is an operating system on its own right, so if it starts as utc of course you will get utc. – Drachenfels Aug 21 '20 at 15:30
  • The local time zone may have had a different UTC offset in the past and therefore the same code as in your answer but for past dates may return a wrong result silently unless Python has access to the tz database. That is why [my answer uses tzlocal module to get a pytz timezone](https://stackoverflow.com/a/17363006/4279) which provides access to the tz database in a portable manner. – jfs Oct 29 '20 at 20:00
  • There is no reason not to use `timezone.utc` on Python 3.6+ (i.e., the 2nd example in the answer (with `timedelta(0)`) may be removed. – jfs Oct 29 '20 at 20:08
  • 1
    i believe that example simply had the 3.6 comparator reversed. in other words, it was saying you can't use timezone.utc in 3.6+, but it's actually *available* starting with 3.6, so i left the second example, but marked it as `< 3.6` instead of `>= 3.6`. phew. – anarcat May 17 '21 at 18:43
  • 1
    @sjngm you can use the code to get the local timezone from the OS but the code is semantically wrong `datetime.datetime.utcnow()` returns UTC time without timezone information (naive datetime). `.astimezone()` adds the local timezone information to it but it treats the naive datetime as local time, not UTC! – pabouk - Ukraine stay strong Jun 04 '21 at 08:01
  • `datetime.now(datetime.now().astimezone().tzinfo).isoformat()` to include your local time zone in the ISO string. – hobs Sep 06 '21 at 21:51
  • I wonder why `datetime.datetime.now()` doesn't already return you a `datetime.datetime` with time zone information. Why do we have to use `.astimezone()`? – Ray May 20 '22 at 13:57
  • 10
    This doesn't look correct. If I use `TZ=Australia/Sydney`, it gives `datetime.timezone(datetime.timedelta(seconds=36000), 'AEST')`. That's a fixed offset time zone, which will give the wrong answer when applied to dates 6 months out when daylight saving time is in effect. – James Henstridge Aug 10 '22 at 13:04
49

Try dateutil, which has a tzlocal type that does what you need.

Steven
  • 28,002
  • 5
  • 61
  • 51
  • 34
    This isn't a very standard package... are there more canonical solutions? – gatoatigrado Nov 22 '12 at 20:42
  • 2
    `dateutil` fails for some timezones with dates in the past. And for cases when it does work, you could use [pure stdlib solution](http://stackoverflow.com/a/17363006/4279) – jfs Jun 28 '13 at 10:55
  • 1
    @gatoatigrado: see [How to convert a python utc datetime to a local datetime using only python standard library?](http://stackoverflow.com/a/13287083/4279) – jfs Jun 28 '13 at 10:56
  • 10
    `from dateutil.tz import tzlocal` – Andy Hayden Oct 20 '16 at 08:55
  • @Jthorpe: `tzlocal` and `dateutil.tz.tzlocal` are different things. `tzlocal` is a `pytz`-based *module* that supports more cases than [`dateutil.tz.tzlocal()` *function* which can return a wrong result](https://stackoverflow.com/a/17365806/4279) – jfs Oct 22 '17 at 13:14
  • local timezone comes from the time module: `import time; time.tzname` – Karsten Feb 10 '18 at 17:24
  • 2
    @Jthorpe `dateutil` is not defuct and is under active development. – tacaswell Feb 15 '18 at 14:22
  • @Jthorpe as @ tacaswell said, dateutil doesn't seem defunct as it is recommended by the latest docs: https://docs.python.org/3.8/library/datetime.html (search for dateutil) - as of 3.8-dev – bernard paulus Apr 19 '19 at 09:39
  • Regarding my defunct comment, it was valid at the time (2016) as the posted answer did not work, and it's still valid for people who don't use bleading edge python (3.8-dev), as some times adding on to an old system doesn't allow for upgrading your python. – Jthorpe Apr 19 '19 at 17:21
42

to compare UTC timestamps from a log file with local timestamps.

It is hard to find out Olson TZ name for a local timezone in a portable manner. Fortunately, you don't need it to perform the comparison.

tzlocal module returns a pytz timezone corresponding to the local timezone:

from datetime import datetime

import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal

tz = get_localzone()
local_dt = tz.localize(datetime(2010, 4, 27, 12, 0, 0, 0), is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc) #NOTE: utc.normalize() is unnecessary here

Unlike other solutions presented so far the above code avoids the following issues:

  • local time can be ambiguous i.e., a precise comparison might be impossible for some local times
  • utc offset can be different for the same local timezone name for dates in the past. Some libraries that support timezone-aware datetime objects (e.g., dateutil) fail to take that into account

Note: to get timezone-aware datetime object from a naive datetime object, you should use*:

local_dt = tz.localize(datetime(2010, 4, 27, 12, 0, 0, 0), is_dst=None)

instead of:

#XXX fails for some timezones
local_dt = datetime(2010, 4, 27, 12, 0, 0, 0, tzinfo=tz)

*is_dst=None forces an exception if given local time is ambiguous or non-existent.

If you are certain that all local timestamps use the same (current) utc offset for the local timezone then you could perform the comparison using only stdlib:

# convert a naive datetime object that represents time in local timezone to epoch time
timestamp1 = (datetime(2010, 4, 27, 12, 0, 0, 0) - datetime.fromtimestamp(0)).total_seconds()

# convert a naive datetime object that represents time in UTC to epoch time
timestamp2 = (datetime(2010, 4, 27, 9, 0) - datetime.utcfromtimestamp(0)).total_seconds()

timestamp1 and timestamp2 can be compared directly.

Note:

  • timestamp1 formula works only if the UTC offset at epoch (datetime.fromtimestamp(0)) is the same as now
  • fromtimestamp() creates a naive datetime object in the current local timezone
  • utcfromtimestamp() creates a naive datetime object in UTC.
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    Just what I needed! – nagylzs Oct 22 '17 at 13:00
  • 4
    I don't understand why getting local timezone it isn't included in one of the Python core modules... – rfmoz Mar 30 '20 at 18:57
  • 3
    @Rfraile: If we exclude corner cases where you would need a `pytz` timezone, Python had local timezone in stdlib since forever. 2- [PEP 615 -- Support for the IANA Time Zone Database in the Standard Library](https://www.python.org/dev/peps/pep-0615/) is accepted in Python 3.9 (it provides access to the same tzdata as `pytz`) – jfs Mar 31 '20 at 17:05
  • 2
    @jfs I wanted to say support for Olson timezone, sorry. Good to know this new PEP 615, it will add a great value, thanks for sharing – rfmoz Mar 31 '20 at 18:34
24

I was asking the same to myself, and I found the answer in 1:

Take a look at section 8.1.7: the format "%z" (lowercase, the Z uppercase returns also the time zone, but not in the 4-digit format, but in the form of timezone abbreviations, like in [3]) of strftime returns the form "+/- 4DIGIT" that is standard in email headers (see section 3.3 of RFC 2822, see [2], which obsoletes the other ways of specifying the timezone for email headers).

So, if you want your timezone in this format, use:

time.strftime("%z")

[1] http://docs.python.org/2/library/datetime.html

[2] https://www.rfc-editor.org/rfc/rfc2822#section-3.3

[3] Timezone abbreviations: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations , only for reference.

Community
  • 1
  • 1
David L
  • 1,058
  • 12
  • 9
  • 3
    the question is about finding `tzinfo` object corresponding to local timezone, not current utc offset as a string. [`time.timezone`,`.altzone`](http://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone#comment25200533_13218990) give you current utc offset. Timezone offset or abbreviations are ambiguous. It is not that easy to get local timezone that you could use for dates in the far past, present and near future. Look at [`tzlocal` module's source code](https://github.com/regebro/tzlocal) to see an example how it can be done. – jfs Dec 11 '13 at 19:28
  • Simple and effective – Mohammad Banisaeid Apr 18 '17 at 10:11
12

The following appears to work for 3.7+, using standard libs:

from datetime import timedelta
from datetime import timezone
import time

def currenttz():
    if time.daylight:
        return timezone(timedelta(seconds=-time.altzone),time.tzname[1])
    else:
        return timezone(timedelta(seconds=-time.timezone),time.tzname[0])
Robbie Matthews
  • 1,404
  • 14
  • 22
9

First get pytz and tzlocal modules

pip install pytz tzlocal

then

from tzlocal import get_localzone
local = get_localzone()

then you can do things like

from datetime import datetime
print(datetime.now(local))
Delgan
  • 18,571
  • 11
  • 90
  • 141
aminalid
  • 1,156
  • 10
  • 7
6

Here's a way to get the local timezone using only the standard library, (only works in a *nix environment):

>>> '/'.join(os.path.realpath('/etc/localtime').split('/')[-2:])
'Australia/Sydney'

You can use this to create a pytz timezone:

>>> import pytz
>>> my_tz_name = '/'.join(os.path.realpath('/etc/localtime').split('/')[-2:])
>>> my_tz = pytz.timezone(my_tz_name)
>>> my_tz
<DstTzInfo 'Australia/Sydney' LMT+10:05:00 STD>

...which you can then apply to a datetime:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2014, 9, 3, 9, 23, 24, 139059)

>>> now.replace(tzinfo=my_tz)
>>> now
datetime.datetime(2014, 9, 3, 9, 23, 24, 139059, tzinfo=<DstTzInfo 'Australia/Sydney' LMT+10:05:00 STD>)
amorphic
  • 722
  • 8
  • 12
6

Here's a slightly more concise version of @vbem's solution:

from datetime import datetime as dt

dt.utcnow().astimezone().tzinfo

The only substantive difference is that I replaced datetime.datetime.now(datetime.timezone.utc) with datetime.datetime.utcnow(). For brevity, I also aliased datetime.datetime as dt.

For my purposes, I want the UTC offset in seconds. Here's what that looks like:

dt.utcnow().astimezone().utcoffset().total_seconds()
David Marx
  • 8,172
  • 3
  • 45
  • 66
  • 2
    ValueError: astimezone() cannot be applied to a naive datetime (python 3.4) – pbarill Jun 16 '18 at 15:10
  • 1
    Yeah, after I published some code with this solution, users who were using linux reported this same bug. If I remember correctly, the root cause is the OS time isn't configured to a timezone (i.e. it's "naive" UTC) and this breaks the code. Haven't looked into a solution beyond wrapping that code in a try/except block. – David Marx Jun 20 '18 at 08:14
  • Ok thanks. Since then I gave up the idea of a pure stdlib solution and went with `pytz` and `tzlocal`. I am wary of adding third party dependencies when python already ships with so much, but when dealing with time zones, clearly you need external help. – pbarill Jun 20 '18 at 13:23
6

To create an ISO formatted string that includes the ISO representation of your local time zone in Israel (+04:00) :

on a server in Israel:

>>> datetime.now(datetime.now().astimezone().tzinfo).isoformat()
'2021-09-07T01:02.030042+04:00'

This will create a "timezone aware" date object that will compare to any other datetime object in UTC or local time appropriately. But the time zone ISO representation (and the date/time string itself) will change if you ran this on a server in San Francisco at the exact same time, as I did:

on a server in San Francisco, CA, USA (Pacific):

>>> datetime.now(datetime.now().astimezone().tzinfo).isoformat()
'2021-09-06T14:01:02.030042-07:00'

The datetime objects in in both cases would be compatible with each other. So if you subtracted them you'd get a time delta of 0:

On a server anywhere in Python3.6+:

>>> (datetime.fromisoformat('2021-09-06T14:01:02.030042-07:00') -
...  datetime.fromisoformat('2021-09-07T01:01:02.030042+04:00'))
datetime.timedelta(0)
hobs
  • 18,473
  • 10
  • 83
  • 106
4

Avoiding non-standard module (seems to be a missing method of datetime module):

from datetime import datetime
utcOffset_min = int(round((datetime.now() - datetime.utcnow()).total_seconds())) / 60   # round for taking time twice
utcOffset_h = utcOffset_min / 60
assert(utcOffset_min == utcOffset_h * 60)   # we do not handle 1/2 h timezone offsets

print 'Local time offset is %i h to UTC.' % (utcOffset_h)
thoku
  • 1,120
  • 9
  • 27
  • 2
    If you don't care about DST or utc offsets in the past (as your solution shows); you could just use `-time.timezone`. – jfs Jun 28 '13 at 11:07
3

Based on Thoku's answer above, here's an answer that resolves the time zone to the nearest half hour (which is relevant for some timezones eg South Australia's) :

from datetime import datetime
round((round((datetime.now()-datetime.utcnow()).total_seconds())/1800)/2)
  • Python 3 version: `round((round((datetime.datetime.now()-datetime.datetime.utcnow()).total_seconds())/1800)/2)` – Dmitry Sep 23 '15 at 12:59
  • (1) it is wrong if the local utc offset is different in the past (it is in many timezones) (2) `round()` may produce a wrong utc offset here (for some timezones) and it is unnecessary. To find out how to get the precise utc offset, see [Getting computer's UTC offset in Python](http://stackoverflow.com/q/3168096/4279) – jfs Oct 05 '15 at 01:41
3

Based on J. F. Sebastian's answer, you can do this with the standard library:

import time, datetime
local_timezone = datetime.timezone(datetime.timedelta(seconds=-time.timezone))

Tested in 3.4, should work on 3.4+

Gilly
  • 1,739
  • 22
  • 30
3

You may be happy with pendulum

>>> pendulum.datetime(2015, 2, 5, tz='local').timezone.name
'Israel'

Pendulum has a well designed API for manipulating dates. Everything is TZ-aware.

Étienne Bersac
  • 580
  • 6
  • 11
3

I have also been looking for a simple way to read the local host configuration and get timezone aware local_time based on it. As of python 3.6+ the simplest approach is use dateutil.tz which will read /etc/localtime and assist in getting timezone aware datetime object.

Here is more info on it: https://dateutil.readthedocs.io/en/stable/tz.html

The implementation to accomplish what you're looking for is as follows:

from datetime import datetime
from dateutil import tz
local_time = datetime.now(tz.gettz())

This will provide you the following local_time:

2019-10-18 13:41:06.624536-05:00

Additional Resources I used in researching this topic: Paul Ganssle Presentation about time zones: https://www.youtube.com/watch?v=l4UCKCo9FWY

pytz: The Fastest Footgun in the West https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html

darekm101
  • 321
  • 1
  • 6
2

I want to compare UTC timestamps from a log file with local timestamps

If this is your intent, then I wouldn't worry about specifying specific tzinfo parameters or any additional external libraries. Since Python 3.5, the built in datetime module is all you need to create a UTC and a local timestamp automatically.

import datetime
f = "%a %b %d %H:%M:%S %Z %Y"         # Full format with timezone

# tzinfo=None
cdatetime = datetime.datetime(2010, 4, 27, 12, 0, 0, 0)  # 1. Your example from log
cdatetime = datetime.datetime.now()   # 2. Basic date creation (default: local time)
print(cdatetime.strftime(f))          # no timezone printed
# Tue Apr 27 12:00:00  2010

utctimestamp = cdatetime.astimezone(tz=datetime.timezone.utc)  # 1. convert to UTC
utctimestamp = datetime.datetime.now(tz=datetime.timezone.utc) # 2. create in UTC
print(utctimestamp.strftime(f))
# Tue Apr 27 17:00:00 UTC 2010

localtimestamp = cdatetime.astimezone()               # 1. convert to local [default]
localtimestamp = datetime.datetime.now().astimezone()  # 2. create with local timezone
print(localtimestamp.strftime(f))
# Tue Apr 27 12:00:00 CDT 2010

The '%Z' parameter of datetime.strftime() prints the timezone acronym into the timestamp for humans to read.

codejedi365
  • 1,074
  • 9
  • 13
1

For simple things, the following tzinfo implementation can be used, which queries the OS for time zone offsets:

import datetime
import time

class LocalTZ(datetime.tzinfo):
    _unixEpochOrdinal = datetime.datetime.utcfromtimestamp(0).toordinal()

    def dst(self, dt):
        return datetime.timedelta(0)

    def utcoffset(self, dt):
        t = (dt.toordinal() - self._unixEpochOrdinal)*86400 + dt.hour*3600 + dt.minute*60 + dt.second + time.timezone
        utc = datetime.datetime(*time.gmtime(t)[:6])
        local = datetime.datetime(*time.localtime(t)[:6])
        return local - utc


print datetime.datetime.now(LocalTZ())
print datetime.datetime(2010, 4, 27, 12, 0, 0, tzinfo=LocalTZ())

# If you're in the EU, the following datetimes are right on the DST change.
print datetime.datetime(2013, 3, 31, 0, 59, 59, tzinfo=LocalTZ())
print datetime.datetime(2013, 3, 31, 1, 0, 0, tzinfo=LocalTZ())
print datetime.datetime(2013, 3, 31, 1, 59, 59, tzinfo=LocalTZ())

# The following datetime is invalid, as the clock moves directly from
# 01:59:59 standard time to 03:00:00 daylight savings time.
print datetime.datetime(2013, 3, 31, 2, 0, 0, tzinfo=LocalTZ())

print datetime.datetime(2013, 10, 27, 0, 59, 59, tzinfo=LocalTZ())
print datetime.datetime(2013, 10, 27, 1, 0, 0, tzinfo=LocalTZ())
print datetime.datetime(2013, 10, 27, 1, 59, 59, tzinfo=LocalTZ())

# The following datetime is ambigous, as 02:00 can be either DST or standard
# time. (It is interpreted as standard time.)
print datetime.datetime(2013, 10, 27, 2, 0, 0, tzinfo=LocalTZ())
Søren Løvborg
  • 8,354
  • 2
  • 47
  • 40
  • (1) it can't work if `time` module has no access to the tz database on a given platform (note: `pytz` in the question provides such access in a portable way) (2) `t` formula is incorrect if the utc offset that corresponds to `dt` is not `-time.timezone` and therefore even if `time` module knows the correct `localtime(t)` -- your code may ask a wrong value (consider time around a DST transition). – jfs Oct 05 '15 at 01:36
1

tzlocal from dateutil.

Code example follows. Last string suitable for use in filenames.

>>> from datetime import datetime
>>> from dateutil.tz import tzlocal
>>> str(datetime.now(tzlocal()))
'2015-04-01 11:19:47.980883-07:00'
>>> str(datetime.now(tzlocal())).replace(' ','-').replace(':','').replace('.','-')
'2015-04-01-111947-981879-0700'
>>> 
Johnny Utahh
  • 2,389
  • 3
  • 25
  • 41
1

First, note that the question presents an incorrect initialization of an aware datetime object:

>>> local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, 0,
...                                  tzinfo=pytz.timezone('Israel'))

creates an invalid instance. One can see the problem by computing the UTC offset of the resulting object:

>>> print(local_time.utcoffset())
2:21:00

(Note the result which is an odd fraction of an hour.)

To initialize an aware datetime properly using pytz one should use the localize() method as follows:

>>> local_time=pytz.timezone('Israel').localize(datetime.datetime(2010, 4, 27, 12))
>>> print(local_time.utcoffset())
3:00:00

Now, if you require a local pytz timezone as the new tzinfo, you should use the tzlocal package as others have explained, but if all you need is an instance with a correct local time zone offset and abbreviation then tarting with Python 3.3, you can call the astimezone() method with no arguments to convert an aware datetime instance to your local timezone:

>>> local_time.astimezone().strftime('%Y-%m-%d %H:%M %Z %z')
'2010-04-27 05:00 EDT -0400'
Alexander Belopolsky
  • 2,228
  • 10
  • 26
  • (1) don't pass `pytz` timezone to `datetime()` constructor directly -- if you know that you shouldn't do it; why do it? (2) If you have `pytz` installed; why do you use non-portable `astimezone()` that might not have access to the tz database on a given platform? You could get pure Python [`tzlocal` module that can find `pytz` tzinfo object for the local timezone on Windows and Unix (`/etc/localtime` or `/usr/local/etc/localtime`) instead.](http://stackoverflow.com/a/17363006/4279) – jfs Oct 04 '15 at 07:18
  • 1
    (1) because this is how OP presented his question; (2) astimezone() is as portable as it gets and can be used without having to install an additional package. – Alexander Belopolsky Oct 04 '15 at 13:14
  • (1) the question may contain a broken code; the answer should not (without a good reason and clearly indication in the code block itself, [example](http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array#comment35813598_3277503)). (2) the code already uses pytz. Correct answer should use the tz database. – jfs Oct 04 '15 at 13:50
  • 1
    tzlocal is not part of pytz. If it is available on the user's system - using it is a fine solution. If not, astimezone() is the recommended solution for Python 3.3 and higher. Whether or not the "correct" solution should use the tz database depends on user needs. Hopefully for most users the result will be the same regardless. Note that while mixing pytz timezones with say dateutil ones is not recommended, datetime instances using standard library datetime.timezone are fully interoperable with those using pytz. – Alexander Belopolsky Oct 04 '15 at 14:13
  • 1
    _(1) the question may contain a broken code; the answer should not ..._ That's a good advise. I rewrote my answer. Thanks. – Alexander Belopolsky Oct 04 '15 at 17:57
  • if your definition of "correct" is different from what the tz database says then you should specify it explicitly. – jfs Oct 05 '15 at 00:59
  • 2
    The question is what is the "correct" value of the `tzinfo` attribute in the resulting datetime instance. I put "correct" in quotes because for most practical purposes, the exact type of `tzinfo` does not matter as long as the values of `utcoffset()` and `tzname()` are the same. A `datetime.timezone` object is a lightweight `tzinfo` that has just enough data to return the correct `utcoffset()` and `tzname()` for a specific time. In contrast, `pytz.timezone` gives access to all history which may not be needed. – Alexander Belopolsky Oct 05 '15 at 01:20
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91333/discussion-between-alexander-belopolsky-and-j-f-sebastian). – Alexander Belopolsky Oct 05 '15 at 01:56
1
now_dt = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
now_ts, utc_ts = map(time.mktime, map(datetime.datetime.timetuple, (now_dt, utc_now)))
offset = int((now_ts - utc_ts) / 3600)

hope this will help you.

宝城 p
  • 11
  • 2