From a timezone and a UTC time, get the difference in seconds vs local time at that point in time

試著忘記壹切 提交于 2019-12-29 01:26:06

问题


This should be very simple, but I can't quite figure it out in Python. I want to have a function which takes two arguments, a UTC time in seconds and a zoneinfo name like 'Europe/Vienna' and returns the offset in seconds from local time and UTC for that point in time.

In C it would be:

/* ... code to to set local time to the time zone I want to compare against,
   not shown here. Then call function below to get difference vs localtime.
   Hardly an ideal solution,
   but just to demonstrate what I want in a "lingua franca" (C): */


int get_diff_vs_localtime(const time_t original_utc_time)
{
    struct tm* ts;

    ts = localtime(&original_utc_time);

    return mktime(ts) - original_utc_time;
}

I guess my question really boils down to: "given an Olson timezone (example 'Europe/Stockholm') and a UTC time, what is the local time?


回答1:


Assuming "UTC time in seconds" means POSIX timestamp. To convert it to Stockholm time:

from datetime import datetime
import pytz

tz = pytz.timezone('Europe/Stockholm')

utc_dt = datetime.utcfromtimestamp(posix_timestamp).replace(tzinfo=pytz.utc)
dt = tz.normalize(utc_dt.astimezone(tz))
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))

tz.normalize() is unnecessary if the source timezone is UTC (like in this case).

A simpler alternative is to use fromtimestamp()'s tz parameter, to convert "seconds since the epoch" to local time:

from datetime import datetime
import pytz

tz = pytz.timezone('Europe/Stockholm')

dt = datetime.fromtimestamp(posix_timestamp, tz)
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))

Both examples produce the same result.

If local machine uses "right" timezones then to convert POSIX timestamp received from an external source to UTC, an explicit formula could be used:

from datetime import datetime, timedelta
import pytz

utc_dt = datetime(1970, 1, 1, tzinfo=pytz.utc) + timedelta(seconds=posix_timestamp)

The latest formula may also support a larger date range (less likely issues with dates before 1970, after 2038 or 3000 years).

If the timestamp comes from the local "right" source then the first two examples should be used instead (they call "right" time.gmtime()).




回答2:


You could use pytz and datetime to do something in the manner of:

from datetime import datetime
from pytz import timezone

def get_diff(now, tzname):
    tz = timezone(tzname)
    utc = timezone('UTC')
    utc.localize(datetime.now())
    delta =  utc.localize(now) - tz.localize(now)
    return delta

Which for the following example...

now = datetime.utcnow()
print(now)
tzname = 'Europe/Stockholm'
delta = get_diff(now, tzname)
print(delta)
now_in_stockholm = now + delta
print(now_in_stockholm)

... outputs:

2012-10-02 14:38:56.547475
2:00:00
2012-10-02 16:38:56.547475



回答3:


I guess my question really boils down to: "given an Olson timezone (example 'Europe/Stockholm') and a UTC time, what is the local time?

If I understand your problem correctly:

from pytz import timezone
import datetime, time

tz = timezone('Asia/Kuwait')
utc_dt = datetime.datetime.utcfromtimestamp(time.time())
utc_dt + tz.utcoffset(utc_dt)

>>> tz.utcoffset(utc_dt).seconds
10800
>>> tz
<DstTzInfo 'Asia/Kuwait' LMT+3:12:00 STD>
>>> utc_dt + tz.utcoffset(utc_dt)
datetime.datetime(2012, 10, 2, 17, 13, 53, 504322)
>>> utc_dt
datetime.datetime(2012, 10, 2, 14, 13, 53, 504322)



回答4:


This is pretty old, but I couldn't find a great answer, so here's what I came up with:

from datetime import datetime
local = datetime.now()
utc = datetime.utcnow()
int((local - utc).days * 86400 + round((local - utc).seconds, -1))

Returns:

-21600

because I am (currently) 21600 seconds (6 hours) behind UTC.

Note: the second date calculated (in this case UTC) needs to be rounded since there is a super small difference in time at each calculation.



来源:https://stackoverflow.com/questions/12691081/from-a-timezone-and-a-utc-time-get-the-difference-in-seconds-vs-local-time-at-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!