The conversion between a Python datetime and a timestamp

Source: Internet
Author: User
Tags local time

Python processing is a big hole in the time zone when dealing with the conversion of datetime and timestamp in Python.

Because the python time is handled by the default localtime input, the script runs locally and runs on the server, resulting in a different result. If you don't pay attention, you'll be in the middle.

R also encounters time zone problems, but the R method provides the TZ option to specify the time zone of the date, simplifying the problem. The time.mktime in Python cannot specify the time zone, causing a lot of trouble. Looked for a long time, found that there is a Time.timezone property, according to the time of the machine running the script, to flexibly handle time zone problems.

The following is a method for datetime and timestamp conversions, and the input and output are GMT0.

Import timedef timestamp_datetime (ts,type= ' ms): "" "Input timestamp, convert timestamp to GMT0 datetime output:p aram ts:int integer or int string, default millisecond time:p Aram Type: The time type, the input data is MS or S. The default ms:return:GMT0 time_struct type (but time_struct actually does not contain TZ information) "" "ts = Int (ts) if type.lower () = = ' ms ': TS = t s/1000 elif type.lower () = = ' s ': Pass Else:print (' ERROR! Timestamp Please select S or MS ') return Str_format = '%y-%m-%d%h:%m:%s ' dt0 = Time.strftime (Str_format, Time.gmtime (ts)) #GMT0的日 Period return Dt0def Datetime_timestamp (dt0, type= ' Ms '): "" Enter the date (or date string) of the GMT0 time and output the int type timestamp.        The default millisecond level:p Aram dt0:gmt0 time:p Aram Type: The timestamp type of the output, the MS class or the S-Class: Return:int timestamp "" "If Isinstance (Dt0, str):  ts = Time.mktime (time.strptime (dt0, '%y-%m-%d%h:%m:%s ')) Else:ts = Time.mktime (dt0) delta = Time.timezone # mktime Default time is local time, need Ts+delta adjust time zone if type.lower () = = ' Ms ': TS0 = int (ts) * 1000-delta * elif Type.lo Wer () = = ' s ': ts0 = int (ts)-Delta Else:print (' ERROR! Time stampPlease select S or MS ') return return TS0 

  

The conversion between a Python datetime and a timestamp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.