Introduction to the time module of the Python module

Source: Internet
Author: User
Tags format definition local time month name
The following small series for you to bring a Python module of the time module (example explained). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Time

Three forms of time representation

Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.

Formatted time string (format string): ' 1999-12-06 '

Time Formatting symbols


The%y two-digit year represents (00-99)%y four-digit year (000-9999)%m Month (01-12) the Day of the month (0-31)%H 24-hour hours (0-23)%I 12-hour hours (01-12)%M minutes (00=59)%s seconds (00-59)%a Local simplified week name%a Local full week name%b Local simplified month name%b Local full month name%c local corresponding date representation and time representation%j One day of the year (001-366)%p local a.m. or p.m. the number of weeks in the year (00-53) Sunday for the beginning of the week%w Weeks (0-6), Sunday for the beginning of the week%w the week of the Year (00-53) Monday for the beginning of the week%x Local corresponding date indicates%x local corresponding time represents%z current time zone's name%% number itself "'

Tuples (struct_time): Struct_time tuples Total 9 elements Total nine elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)

Tuple element meaning


Index attribute (Attribute) value (values  )   tm_year (year)   Tm_mon (month)    1-12   tm_mday (day)   1-   Tm_hour (Hours)   0-23   tm_min (min)    0-59   tm_sec (sec)    0-61   Tm_wday (weekday)  0- 6 (0 = Sunday)   tm_yday (Day of the year) 1-366   tm_isdst (Daylight saving time) The default is-1 "" "

Several formats in Python that represent time


#导入时间模块 >>>import time# timestamp >>>time.time () 1500875844.800804# time string >>>time.strftime ("%Y-% m-%d%x ") ' 2017-07-24 13:54:37 ' >>>time.strftime ("%y-%m-%d%h-%m-%s ") ' 2017-07-24 13-55-04 ' #时间元组: LocalTime converts a timestamp to the current time zone Struct_timetime.gmtime () time.localtime () time.struct_time (tm_year=2017, tm_mon=7, tm_mday= Tm_hour=13, tm_min=59, tm_sec=37,      tm_wday=0, tm_yday=205, tm_isdst=0) # Summary: Time stamp is the time that the computer can recognize, time string is the time that people can understand; Tuples are used to manipulate the time

Conversion between various formatting times


#时间戳-to-structured time #time.gmtime (timestamp) #UTC时间, consistent #time.localtime (timestamp) #当地时间 with local time in London, UK. For example, we now implement this method in Beijing: 8 hours from UTC time, UTC time + 8 hours = Beijing Time >>>time.gmtime (1500000000) time.struct_time (tm_year=2017, tm_ Mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0) >>>time.localtime ( 1500000000) Time.struct_time (tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday= 195, tm_isdst=0) #结构化时间-time stamp #time. Mktime (structured time) >>>time_tuple = Time.localtime (1500000000) >>> Time.mktime (time_tuple) 1500000000.0


#结构化时间--String time #time.strftime ("format definition", "structured Time") if the structured time parameter does not pass, then the current time >>>time.strftime ("%y-%m-%d%x") 2017-07-24 14:55:36 ' >>>time.strftime ("%y-%m-%d", Time.localtime (1500000000)) ' 2017-07-14 ' #字符串时间-- Structured time #time.strptime (time string, string corresponding format) >>>time.strptime ("2017-03-16", "%y-%m-%d") Time.struct_time (tm_year= Tm_mon=3, Tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, Tm_isdst=-1) >>> Time.strptime ("07/24/2017", "%m/%d/%y") time.struct_time (tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_ Sec=0, Tm_wday=0, tm_yday=205, Tm_isdst=-1)


#结构化时间--%a%b%d%h:%m:%s%y string #time.asctime (structured time) if the parameter is not passed, the format string of the current time is returned directly >>>time.asctime (Time.localtime ( 1500000000) ' Fri Jul 10:40:00 ' >>>time.asctime () ' Mon Jul 15:18:33 ' #%a%d%d%h:%m:%s%y string---structure Time #time.ctime (timestamp) If the parameter is not passed, the format string of the current time is returned directly >>>time.ctime () ' Mon Jul 15:19:07 ' >>>time.ctime ( 1500000000) ' Fri Jul 14 10:40:00 2017 '

Calculate Time Difference


Import Timetrue_time=time.mktime (Time.strptime (' 2017-09-11 08:30:00 ', '%y-%m-%d%h:%m:%s ')) Time_now=time.mktime ( Time.strptime (' 2017-09-12 11:00:00 ', '%y-%m-%d%h:%m:%s ')) Dif_time=time_now-true_timestruct_time=time.gmtime (dif_ Time) print ("%d%d-%d-hour%d-minute%d-seconds '% (struct_time.tm_year-1970,struct_time.tm_mon-1,          struct_time.tm_mday-1 , Struct_time.tm_hour,          struct_time.tm_min,struct_time.tm_sec))

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.