UNIX timestamp conversion (Python)

Source: Internet
Author: User
UNIX timestamp conversion (Python)
#-*-Coding: UTF-8-*-import timedef timestamp_datetime (value): format = '% Y-% m-% d % H: % m: % s' # value indicates that the input value is a timestamp (integer), for example: 1332888820 value = time. localtime (value) # converted to # Time after localtime conversion. struct_time (maid = 2012, tm_mon = 3, tm_mday = 28, tm_hour = 6, tm_min = 53, tm_sec = 40, tm_wday = 2, tm_yday = 88, tm_isdst = 0) # Finally, convert the strftime function to the normal date format. Dt = time. strftime (format, value) return dtdef datetime_timestamp (DT): # dt is a string # intermediate process, which usually needs to be converted into a time array. strptime (DT, '% Y-% m-% d % H: % m: % s') # Time. struct_time (maid = 2012, tm_mon = 3, tm_mday = 28, tm_hour = 6, tm_min = 53, tm_sec = 40, tm_wday = 2, tm_yday = 88, tm_isdst =-1) # convert "06:53:40" to the timestamp S = time. mktime (time. strptime (DT, '% Y-% m-% d % H: % m: % s') return int (s) if _ name _ = '_ main _': D = datetime_timestamp ('2017-03-28 06:53:40 ') print d s = timestamp_datetime (2012) print s

 

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.