Python normal time and unix timestamp conversion method, pythonunix

Source: Internet
Author: User

Python normal time and unix timestamp conversion method, pythonunix

This article describes how to convert the normal time and unix timestamp of python. Share it with you for your reference. The specific analysis is as follows:

This code can be used to convert the regular time format to a unix timestamp, or convert the unix timestamp back,
For example, 1332888820 format is converted to 06:53:40 format

#-*-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

I hope this article will help you with Python programming.

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.