获取一天开始的时间戳(当地时间)
Get the timestamp of the start of a day (local time)
我想知道是否有任何方法可以获取某一天开始时的 UNIX 时间戳,即给定时区的每一天的午夜时间戳。
假设您不关心夏令时并且您知道当时有效的 UTC 偏移量(相反到现在生效的 UTC 偏移量),你可以这样做:
import datetime as dt
return dt.datetime(year, month, day, tzinfo=dt.timezone(utc_offset)).timestamp()
小时、分钟和秒默认为零,因此您可以跳过它们。 timezone
class does not handle daylight savings time, historical changes in timezone definitions (e.g. British Double Summer Time), or any other temporal anomalies (e.g. there was no December 30, 2011 in Samoa); it is a "dumb" offset. It is equivalent (in this case) to adding or subtracting the offset directly onto the timestamp and then working in UTC. You must ensure this is correct for your use case. If you need better timekeeping, you should install and make use of pytz.
我想知道是否有任何方法可以获取某一天开始时的 UNIX 时间戳,即给定时区的每一天的午夜时间戳。
假设您不关心夏令时并且您知道当时有效的 UTC 偏移量(相反到现在生效的 UTC 偏移量),你可以这样做:
import datetime as dt
return dt.datetime(year, month, day, tzinfo=dt.timezone(utc_offset)).timestamp()
小时、分钟和秒默认为零,因此您可以跳过它们。 timezone
class does not handle daylight savings time, historical changes in timezone definitions (e.g. British Double Summer Time), or any other temporal anomalies (e.g. there was no December 30, 2011 in Samoa); it is a "dumb" offset. It is equivalent (in this case) to adding or subtracting the offset directly onto the timestamp and then working in UTC. You must ensure this is correct for your use case. If you need better timekeeping, you should install and make use of pytz.