Python datetime和utc offset转换忽略时区/夏令时 [英] Python datetime and utc offset conversion ignoring timezone/daylight savings
问题描述
-
我在UTC上有一个UNIX时间戳,例如1425508527.从这里我想要获得年,月,天等。给定UTC偏移。例如。年份/月/日/时间(UTC -6小时)是多少?答案是2015年3月4日16:35:27。没有提供偏移量(或零点偏移量),答案应为2015年3月4日22:35:27。
-
现在我在某个位置有日期,以及UTC偏移量。例如2015年3月4日16:35:27和偏移量(UTC -6小时)。我应该得到的UNIX UTC时间戳应该是1425508527.
我能够几乎做2.(使用python datetime库)这样:
import datetime.datetime as datetime
import time
import dateutil.tz as tz
utc_offset = 6
time.mktime(datetime(2015,3,4,16,35,27,
tzinfo = tz.tzoffset (无,utc_offset * 60 * 60))。utctimetuple())
#=> 1425486927
上面的问题是utc_offset必须给错误的符号。根据此地图,utc_offset应设置为-6。我没有运气。我不需要/想要处理时区信息,如夏令时。如何在Python中实现?
如果您的系统使用 Unix时间,其中
不计算闰秒,则转换可以如下完成:
第1部分:时间戳和偏移到本地日期
导入datetime作为DT
导入日历
timestamp = 1425508527
offset = -6
date = DT.datetime(1970,1,1)+ DT.timedelta(seconds = timestamp)
print (日期)
#2015-03-04 22:35:27
localdate = date + DT.timedelta(hours = offset)
print(localdate)
#2015-03-04 16:35:27
第2部分:本地日期和偏移量到时间戳
utcdate = localdate - DT.timedelta(hours = offset)
assert date == utcdate
timetuple = utcdate.utctimetuple()
timestamp2 = calendar.timegm(timetuple)
print(timestamp2)
#1425508527
asser t timestamp == timestamp2
I have two operations I want to perform, one the inverse of the other.
I have a UNIX timestamp at UTC, say for instance, 1425508527. From this I want to get the year, month, day etc. given a UTC offset. EG. what is the year/month/day/time in (UTC -6 hours)? The answer is March 4, 2015 at 16:35:27. Without providing an offset (or offset zero) the answer should be March 4, 2015 at 22:35:27.
Now I have the date at some location, along with the UTC offset. For instance March 4, 2015 at 16:35:27 and the offset (UTC -6 hours). The UNIX UTC timestamp I should get should be 1425508527.
I am able to almost do 2. (using python datetime library) like this:
import datetime.datetime as datetime
import time
import dateutil.tz as tz
utc_offset = 6
time.mktime(datetime(2015,3,4,16,35,27,
tzinfo=tz.tzoffset(None, utc_offset*60*60)).utctimetuple())
# => 1425486927
The problem with the above is that utc_offset has to be given the wrong sign. According to this map, utc_offset should be set to -6. Number 1. I've had no luck with. I don't need/want to deal with timezone information like daylight savings time. How do I implement this in Python?
If your system uses Unix time, which does not count leap seconds, then the conversion can be done as follows:
Part 1: timestamp and offset to local date
import datetime as DT
import calendar
timestamp = 1425508527
offset = -6
date = DT.datetime(1970,1,1) + DT.timedelta(seconds=timestamp)
print(date)
# 2015-03-04 22:35:27
localdate = date + DT.timedelta(hours=offset)
print(localdate)
# 2015-03-04 16:35:27
Part 2: local date and offset to timestamp
utcdate = localdate - DT.timedelta(hours=offset)
assert date == utcdate
timetuple = utcdate.utctimetuple()
timestamp2 = calendar.timegm(timetuple)
print(timestamp2)
# 1425508527
assert timestamp == timestamp2
这篇关于Python datetime和utc offset转换忽略时区/夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!