热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Pythonunix时间戳转换和时区-Pythonunixtimestampconversionandtimezone

Heyall!Ivegottimezonetroubles.大家好!我遇到了时区问题。Ihaveatimestampof2010-07-2623:35:03我的时间戳

Hey all! Ive got timezone troubles.

大家好!我遇到了时区问题。

I have a time stamp of 2010-07-26 23:35:03

我的时间戳是2010-07-26 23:35:03

What I really want to do is subtract 15 minutes from that time.

我真正想做的是从那时起减去15分钟。

My method was going to be a simple conversion to unix time, subtract the seconds and convert back. Simple right?

我的方法是简单转换为unix时间,减去秒数并转换回来。简单吧?

My problem is that python adjusts the returned unix time using my local timezone, currently eastern daylight savings time which I believe is GMT -4.

我的问题是python使用我的本地时区调整返回的unix时间,目前东部夏令时我认为是GMT -4。

So when I do this:

所以当我这样做时:

 # packet[20] holds the time stamp

 unix_time_value = (mktime(packet[20].timetuple())) 

I get 1280201703 which is Tue, 27 Jul 2010 03:35:03. I can do this:

我得到1280201703,这是星期二,2010年7月27日03:35:03。我可以做这个:

 unix_time_value = (mktime(packet[20].timetuple())) - (4 * 3600)

but now I have to check for eastern standard time which is -5 GMT and adjust the (4 * 3600) to (5 * 3600). Is there any way to tell python to not use my local timezone and just convert the darn timestamp OR is there an easy way to take packet[20] and subtract 15 minutes?

但现在我必须检查东部标准时间-5 GMT并将(4 * 3600)调整为(5 * 3600)。有没有办法告诉python不使用我的本地时区只是转换darn时间戳或有一个简单的方法来采取数据包[20]并减去15分钟?

3 个解决方案

#1


6  

Subtract datetime.timedelta(secOnds=15*60).

减去datetime.timedelta(秒= 15 * 60)。

#2


6  

The online docs have a handy table (what you call "unix time" is more properly called "UTC", for "Universal Time Coordinate", and "seconds since the epoch" is a "timestamp" as a float...):

在线文档有一个方便的表(你称之为“unix时间”更恰当地称为“UTC”,“世界时间坐标”,“自纪元以来的秒”是“时间戳”作为浮点数...):

Use the following functions to convert between time representations:

使用以下函数在时间表示之间进行转换:

From                        To                           Use

seconds since the epoch     struct_time in UTC           gmtime()

seconds since the epoch     struct_time in local time    localtime()

struct_time in UTC          seconds since the epoch      calendar.timegm()

struct_time in local time   seconds since the epoch      mktime()

where the unqualified function names come from the time module (since that's where the docs are;-). So, since you apparently start with a struct_time in UTC, use calendar.timegm() to get the timestamp (AKA "seconds since the epoch"), subtract 15 * 60 = 900 (since the units of measure are seconds), and put the resulting "seconds since the epoch" back into a struct_time in UTC with time.gmtime. Or, use time.mktime and time.localtime if you prefer to work in local times (but that might give problem if the 15 minutes can straddle the instant in which it switches to DST or back -- always working in UTC is much sounder).

其中不合格的函数名称来自时间模块(因为那是文档的位置;-)。因此,由于您显然以UTC中的struct_time开头,因此使用calendar.timegm()来获取时间戳(AKA“自纪元以来的秒数”),减去15 * 60 = 900(因为度量单位是秒),然后放入生成的“自纪元以来的秒数”返回UTC中的struct_time,时间为time.gmtime。或者,如果你喜欢在当地时间工作,请使用time.mktime和time.localtime(但如果15分钟可以跨越它切换到DST或返回的瞬间,那么这可能会产生问题 - 总是在UTC工作时更加健全) 。

Of course, to use calendar.timegm, you'll need an import calendar in your code (imports are usually best placed at the top of the script or module).

当然,要使用calendar.timegm,您需要在代码中使用导入日历(导入通常最好放在脚本或模块的顶部)。

#3


1  

Scroll down and read about subtracting from a date: http://pleac.sourceforge.net/pleac_python/datesandtimes.html

向下滚动并阅读从日期中减去的内容:http://pleac.sourceforge.net/pleac_python/datesandtimes.html


推荐阅读
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
  • 获取时间的函数js代码,js获取时区代码
    本文目录一览:1、js获取服务器时间(动态)2 ... [详细]
author-avatar
大头莎LALA
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有