注意:由于对Unix平台没有任何影响,因此我不使用"unix-timestamp",而只是表示日期时间值的明确方式.当前,我正在执行下面的代码,但是除了我仍在解决时区混乱(我的时区为-3)之外,我认为转换为int并返回不是正确的方法,因为如果我没记错的话,datetime和datetime64是python/numpy中的本机类型.因此,datetime64将需要八个字节,而不是我用于(长)unix时间戳的四个字节.import datetimeimport calendarimport structnow = datetime.datetime.now()print nowstamp = calendar.timegm(now.utctimetuple())print stampbinarydatetime = struct.pack('<L', stamp)recoverstamp = struct.unpack('<L', binarydatetime)[0]print recoverstamprecovernow = datetime.datetime.fromtimestamp(recoverstamp)print recovernow所以主要问题是:这是将朴素的日期时间转换为二进制然后返回的有效方式吗?"另外一个问题是:如果这段代码中的所有内容都是幼稚的,为什么会有时区偏移?"感谢阅读!解决方案我找到了一种使用Unix时间戳并将其存储为整数的方法.这对我有用,因为我不需要亚秒级的分辨率,但是我认为长整数将允许微秒级的分辨率,并对代码进行一些修改.与原始版本相比,更改之处在于将calendar.timegm替换为time.mktime,还用utctimetuple替换了timetuple,以保持所有内容幼稚.此:import datetimeimport structimport timenow = datetime.datetime.now()print nowstamp = time.mktime(now.timetuple())print stamprecoverstamp = datetime.datetime.fromtimestamp(stamp)print recoverstampbinarydatetime = struct.pack('<L', stamp)recoverbinstamp = struct.unpack('<L', binarydatetime)[0]print recoverbinstamprecovernow = datetime.datetime.fromtimestamp(recoverbinstamp)print recovernow返回此:2013-09-02 11:06:28.0640001378130788.02013-09-02 11:06:2813781307882013-09-02 11:06:28由此,我可以轻松地将打包的binarydatetime写入文件,并稍后再读回.I want to store a list of datetimes in a binary file in Python.EDIT: by "binary" I mean the best digital representation for each datatype. The application for this is to save GPS trackpoints composed by (unix-timestamp, latitude, longitude, elevation), so the whole structure is little-endian "Long, float, float, float", with four bytes to each value.NOTE: I don't use "unix-timestamp" due to any affection to the Unix platform, but only as an unequivocal way to represent the value of a datetime.Currently, I am doing like the code below, but besides some timezone confusion that I'm still working out (my timezone is -3), I believe converting to int and back might not be the right way, since datetime and datetime64 are native types in python/numpy, if I'm not mistaken. Thus, a datetime64 would need eight bytes instead of the four I am using for the (long)unix-timestamp.import datetimeimport calendarimport structnow = datetime.datetime.now()print nowstamp = calendar.timegm(now.utctimetuple())print stampbinarydatetime = struct.pack('<L', stamp)recoverstamp = struct.unpack('<L', binarydatetime)[0]print recoverstamprecovernow = datetime.datetime.fromtimestamp(recoverstamp)print recovernowSo the main question is: "is this the pythonic way to converting naive datetime to binary and back?"And the aditional question is: "if everything in this code is supposed to be naive, why do I have a timezone offset?"Thanks for reading! 解决方案 I have found a way using the Unix timestamp and storing it as an integer. This works for me because I don't need a subsecond resolution, but I think long integers would allow for microsecond resolution with some modifications of the code.The changes from my original consist in replacing calendar.timegm by time.mktime and also utctimetuple by timetuple, to keep everything naive.This:import datetimeimport structimport timenow = datetime.datetime.now()print nowstamp = time.mktime(now.timetuple())print stamprecoverstamp = datetime.datetime.fromtimestamp(stamp)print recoverstampbinarydatetime = struct.pack('<L', stamp)recoverbinstamp = struct.unpack('<L', binarydatetime)[0]print recoverbinstamprecovernow = datetime.datetime.fromtimestamp(recoverbinstamp)print recovernowReturns this:2013-09-02 11:06:28.0640001378130788.02013-09-02 11:06:2813781307882013-09-02 11:06:28From this, I can easily write the packed binarydatetime to file, and read it back later. 这篇关于在Python中以二进制格式写入和读取Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:50
查看更多