Convert Python unix timestamp and timezone

Hey there! I have problems with the time zone.

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

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

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

My problem is that python is adjusting the returned unix time using my local timezone, currently eastern daylight saving time, which I think is 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, Jul 27, 2010 03:35:03 AM. I can do it:

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

, -5 GMT, (4 * 3600) (5 * 3600). python darn [20] 15 ?

+3
3

datetime.timedelta(seconds=15*60).

+6

- (, "unix time", "UTC", "Universal Time Coordinate" ", " "" " ...):

:

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()

time ( , ;-). , , -, struct_time in UTC, calendar.timegm() (AKA " " ), 15 * 60 = 900 ( - ) "" " struct_time in UTC time.gmtime. time.mktime time.localtime, ( , 15 , DST - UTC, ).

, calendar.timegm import calendar ( script ).

+6

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

+1
source

Source: https://habr.com/ru/post/1761537/


All Articles