从时区和UTC时间,获取在该时间点的秒与本地时间的差异 [英] From a timezone and a UTC time, get the difference in seconds vs local time at that point in time

查看:331
本文介绍了从时区和UTC时间,获取在该时间点的秒与本地时间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是非常简单的,但我不太明白Python。
我想要一个函数需要两个参数,UTC时间(秒)和 zoneinfo name 像'Europe / Vienna',并返回当地时间和UTC时间点的秒数。



在C中将是:

  / * ...代码将本地时间设置为我要比较的时区,
未显示在此处。然后调用下面的函数来获得与本地时间的差异。
一个理想的解决方案,
,但只是为了展示我想要的通用语言(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;
}

我猜我的问题真的归结为:给一个 Olson时区(例如欧洲/斯德哥尔摩)和UTC时间,当地时间是什么?

解决方案

假设UTC时间(以秒为单位)表示POSIX时间戳,将其转换为斯德哥尔摩时间:

  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'))

<$如果源时区是UTC(如本例),则c $ c> tz.normalize()不必要。



一个更简单的选择是要使用 fromtimestamp() tz 参数,将从时代开始的秒转换为本地时间: p>

  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'))

两个示例都产生相同的结果。



如果本地机器使用正确的时区,然后将从外部源接收的POSIX时间戳转换为UTC,可以使用显式公式:

  from datetime import datetime,timedelta 
import pytz

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

最新的公式也可能支持较大的日期范围(20世纪70年代或以后,3000年以前的日期不太可能发生)。



如果时间戳来自本地的正确源,那么应该使用前两个示例(他们调用right time.gmtime())。


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?

解决方案

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()).

这篇关于从时区和UTC时间,获取在该时间点的秒与本地时间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆