开发者

python convert date and time into linux timestmap

开发者 https://www.devze.com 2023-03-04 21:32 出处:网络
i have timestamps in the following format: 2011 February 2nd13h 27min 21s 110202132721 I want to convert 开发者_运维百科110202 132721 into the corresponding linux timestamp: 1296682041

i have timestamps in the following format:

2011 February 2nd  13h 27min 21s
110202             132721

I want to convert 开发者_运维百科110202 132721 into the corresponding linux timestamp: 1296682041

Is there any quick efficient way to achieve this?


Something like

>>> s =  "110202 132721"
>>> print time.mktime(time.strptime(s, "%y%m%d %H%M%S"))
1296653241.0

This interprets the time as a local time (your current time zone).


To create a Unix timestamp, use the time.mktime(t) function. It takes a time.struct_time object.

The objects definition can be viewed here. So you just have to parse the date and the time and put it into the object before handing it over to the mktime() function


Without your timezone information, this is not the 'corresponding' unix timestamp.

After a few attempts I have guessed you could be located in the Pacific coast of USA, so you have to define it explicitely in your script:

from datetime import datetime
import pytz
import calendar

a = "110202 132721"
yourTZ = 'America/Los_Angeles'

calendar.timegm(pytz.timezone(yourTZ).localize(datetime.strptime(a, '%y%m%d %H%M%S')).utctimetuple())

# returns 1296682041
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号