获得“挂钟”中的秒数。时间

时间:2015-07-29 09:06:51

标签: python datetime dst

我正在编写一个计算某一天半小时交易期的模块。交易时段从半小时开始,连续编号从1(从00:00开始)到48(从23:30开始)。通常一天有48个交易周期,但在夏令时开始的那天有46个,在结束的那天有50个。

以下代码适用于所有'普通'天,但未能在夏令时开始或结束的日子给出正确的交易时段号码,因为下面代码中的datetime.replace()在一天的开始使用相同的UTC时间偏移。在夏令时发生变化的日子里,这种假设是不正确的。

datetime.replace()是否可以在00:00设置挂钟时间'时间,以便时间差与你在00:00设置秒表时获得的时间相匹配,然后计算它将在所有日期正确匹配的半小时间隔的数量?我还没有找到一种自动方式来做到这一点。

一个例子: 夏令时于2015年4月5日03:00(2015-05-04 14:00Z)在新西兰结束。因此,时间02:00-02:59(2015-05-04 14:00Z - 2015-05-04 14:59Z)在“挂钟”中重复播放。时间。因此,在新西兰,由于夏令时结束,2015年4月5日凌晨4点达到18000秒。由于夏令时开始,2014年9月28日花了10800秒。

@staticmethod
def half_hour_from_utc(time_utc = None):
    # Get the time as NZ civil time.
    time_nz = time_utc.astimezone(pytz.timezone('Pacific/Auckland'))



    # Get the time tuple for the start of the day. This is done by keeping
    # the date the same but setting the hours, minutes, seconds and
    # microseconds to zero.
    time_start_of_day = time_nz.replace(hour = 0, minute = 0, second = 0, microsecond = 0)

    # Get total number of seconds. The half hour period is the number of
    # 1800 second periods that have passed + 1
    total_secs = int((time_nz - time_start_of_day).total_seconds())
    half_hour = 1 + total_secs // 1800


    print('%s %s %s %s\n' % (time_nz, time_start_of_day, total_secs, half_hour))

2 个答案:

答案 0 :(得分:1)

问题是potttttttttttttttttter调用可能会返回非标准化的using UnityEngine; using System.Collections; public class PlayerMovement : MonoBehaviour { public float speed = 10f; public float rotationSpeed = 100f; private float distToGround; private Collider collider; void Start() { collider = GetComponent<Collider>(); distToGround = collider.bounds.extents.y; } void Update() { MovePlayer(); } void MovePlayer() { float translation = Input.GetAxis("Vertical"); float rotation = Input.GetAxis("Horizontal"); if (translation != null && rotation != null) { if (IsGrounded()) { transform.Translate(0, 0, translation * speed * Time.deltaTime); transform.Rotate(0, rotation * rotationSpeed * Time.deltaTime, 0); } } } bool IsGrounded() { return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f); } 值,即.replace()可能在午夜时分错误。见How do I get the UTC time of “midnight” for a given timezone?

datetime

在Python 2上模拟tzinfo

from datetime import datetime, time as datetime_time, timedelta
import pytz # $ pip install pytz

def half_hour_from_utc(time_utc, tz=pytz.timezone('Pacific/Auckland')):
    time_nz = time_utc.astimezone(tz) # no need to call normalize() here
    midnight = datetime.combine(time_nz, datetime_time(0, 0)) # naive
    time_start_of_day = tz.localize(midnight, is_dst=None) # aware

    return 1 + (time_nz - time_start_of_day) // timedelta(minutes=30) # Python 3

如果DST转换可能在给定时区的午夜发生,那么您可以在当天开始时使用最小值:

1 + td // timedelta(minutes=30)

注意:即使在给定时间内给定时区中00:00时间不存在,它仍然有效:找到差异只是相应的utc时间很重要。

答案 1 :(得分:0)

在处理挂钟算术时,您应该使用normalize()localize()

def half_hour_from_utc(time_utc = None):
    tz = pytz.timezone('Pacific/Auckland')
    time_nz = tz.normalize(time_utc)
    time_start_of_day = tz.localize(datetime.datetime(time_nz.year, time_nz.month, time_nz.day))
    total_secs = int((time_nz - time_start_of_day).total_seconds())
    half_hour = 1 + total_secs // 1800
    print('%s %s %s %s\n' % (time_nz, time_start_of_day, total_secs, half_hour))
  • normalize() =将带有时区的日期时间转换为带有其他时区的日期时间。
  • localize() =将没有时区的日期时间转换为带有时区的日期时间。

这些方法考虑了使用时区计算正确日期时间的必要逻辑。

相关问题