python localtime 与utc时间差5分钟,Python-从DST调整的本地时间到UTC

博客围绕特定银行各地分行当地时间转UTC时间展开。指出最大挑战是检测当地时间是否处于夏令时调整期。给出了使用pytz库实现转换的函数to_utc,通过示例展示了不同时区、不同日期下本地时间转UTC时间的结果。

A specific bank has branches in all major cities in the world. They all open at 10:00 AM local time. If within a timezone that uses DST, then of course the local opening time also follows the DST-adjusted time. So how do I go from the local time to the utc time.

What I need is a function to_utc(localdt, tz) like this:

Arguments:

localdt: localtime, as naive datetime object, DST-adjusted

tz: timezone in the TZ-format, e.g. 'Europe/Berlin'

Returns:

datetime object, in UTC, timezone-aware

EDIT:

The biggest challenge is to detect whether the local time is in a period with DST, which also means that it is DST adjusted.

For 'Europe/Berlin' which has +1 DST in the summer:

Jan 1st 10:00 => Jan 1st 9:00 UTC

July 1st 10:00 => July 1st 8:00 UTC

For 'Africa/Lagos' which has no DST:

Jan 1st 10:00 => Jan 1st 9:00 UTC

July 1st 10:00 => July 1st 9:00 UTC

解决方案

Using pytz, and in particular its localize method:

import pytz

import datetime as dt

def to_utc(localdt,tz):

timezone=pytz.timezone(tz)

utc=pytz.utc

return timezone.localize(localdt).astimezone(utc)

if __name__=='__main__':

for tz in ('Europe/Berlin','Africa/Lagos'):

for date in (dt.datetime(2011,1,1,10,0,0),

dt.datetime(2011,7,1,10,0,0),

):

print('{tz:15} {l} --> {u}'.format(

tz=tz,

l=date.strftime('%b %d %H:%M'),

u=to_utc(date,tz).strftime('%b %d %H:%M %Z')))

yields

Europe/Berlin Jan 01 10:00 --> Jan 01 09:00 UTC

Europe/Berlin Jul 01 10:00 --> Jul 01 08:00 UTC

Africa/Lagos Jan 01 10:00 --> Jan 01 09:00 UTC

Africa/Lagos Jul 01 10:00 --> Jul 01 09:00 UTC

【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点探讨其系统建模控制策略,结合Matlab代码Simulink仿真实现。文章详细分析了无人机的动力学模型,特别是引入螺旋桨倾斜机构后带来的全驱动特性,使其在姿态位置控制上具备更强的机动性自由度。研究涵盖了非线性系统建模、控制器设计(如PID、MPC、非线性控制等)、仿真验证及动态响应分析,旨在提升无人机在复杂环境下的稳定性和控制精度。同时,文中提供的Matlab/Simulink资源便于读者复现实验并进一步优化控制算法。; 适合人群:具备一定控制理论基础和Matlab/Simulink仿真经验的研究生、科研人员及无人机控制系统开发工程师,尤其适合从事飞行器建模先进控制算法研究的专业人员。; 使用场景及目标:①用于全驱动四旋翼无人机的动力学建模仿真平台搭建;②研究先进控制算法(如模型预测控制、非线性控制)在无人机系统中的应用;③支持科研论文复现、课程设计或毕业课题开发,推动无人机高机动控制技术的研究进展。; 阅读建议:建议读者结合文档提供的Matlab代码Simulink模型,逐步实现建模控制算法,重点关注坐标系定义、力矩分配逻辑及控制闭环的设计细节,同时可通过修改参数和添加扰动来验证系统的鲁棒性适应性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值