In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The most common and commonly used time formats
1. Time stamp (timestamp), which indicates the offset in seconds from 00:00:00 on January 1, 1970.
2. Time tuple (struct_time), which has nine element groups.
3. Format time (format time), which can be formatted into a fixed or custom format to increase readability.
#! / usr/bin/env python#-*-coding:utf-8-*-import time# timestamp format. By default, get the current time 1500029143.7640195timestamp = time.time () print ("get current timestamp:", timestamp) # get the form of tuple (struct_time) Get the current time by default struct_time_UTC8= time.localtime () print ("get current East 8 (China) time:", struct_time_UTC8) struct_time_UTC= time.gmtime () print ("get current World time:", struct_time_UTC) Note: UTC (Coordinated Universal Time, Universal coordinated time), Greenwich mean Astronomical time, Universal Standard time. It is UTC+8 in China. DST (Daylight Saving Time) is the time after daylight saving time''# gets formatted The string format_time = time.strftime ("% Y-%m-%d% H:%M:%S", time.localtime ()) print formatted as% Y-%m-%d% H:%M:%S ("time after formatting according to the specified format:" Format_time) # Fri Jul 14 18:45:43 2017 (% a% b% d% H:%M:%S% Y) format # converts time in timestamp format to% a% b% d% H:%M:%S% Y format, default to format current time style_time1 = time.ctime () print ("convert timestamp to% a% b% d% H:%M:%S% Y format:" Style_time1) # convert time in the form of tuple (struct_time) to% a% b% d% H:%M:%S% Y format The default is also to format the current time style_time2 = time.asctime () print ("convert similar to tuple (struct_time) to% a% b% d% H:%M:%S% Y format:", style_time2)''run the result to get the current timestamp: 1500029371.4988112 get the current timestamp: time.struct_time (tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=18, tm_min=49, tm_sec=31, tm_wday=4) Tm_yday=195, tm_isdst=0) get the current world time: time.struct_time (tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=49, tm_sec=31, tm_wday=4, tm_yday=195) Tm_isdst=0) time after formatting according to the specified format: 2017-07-14 18:49:31 convert the timestamp to% a% b% d% H:%M:%S% Y format: Fri Jul 14 18:49:31 2017 convert similar tuples (struct_time) to% a% b% d% H:%M:%S% Y format: Fri Jul 14 18:49:31 2017 hours'
Some basic concepts
1. The form of a timestamp: generally speaking, a timestamp represents an offset in seconds starting at 00:00:00 on January 1, 1970. The main functions that return timestamp are time (), clock (), etc. The default is to return the timestamp of the current time.
two。 In the form of struct_time: the struct_ time tuple has nine elements. The main functions that return struct_time are gmtime (), localtime (), strptime (). By default, it returns the current time in the form of struct_time, but it can also convert other time periods or other forms of time format to struct_time form.
The 9 elements of struct_time tuple are described in detail:
'' 0 tm_year (year) such as 20111 tm_mon (month) 1-122 tm_mday (day) 1-313 tm_hour (hour) 0-234 tm_min (minute) 0595 tm_sec (weekday) 0-616 tm_wday (weekday) 0-6 (0 represents Sunday) 7 tm_yday (day of the year) 1-3668 tm_isdst (daylight saving time) defaults to-1 daylight saving time
Conversion of several time formats
#! / usr/bin/env python#-*-coding:utf-8-*-import time# converts a timestamp into a struct_time format: tuple_struct = time.localtime (1499703018.0) print ("convert a timestamp into a tuple-like time format:", tuple_struct) # converts a timestamp into a tuple-like time format: time.struct_time (tm_year=1973) Tm_mon=11, tm_mday=30, tm_hour=5, tm_min=33, tm_sec=9, tm_wday=4, tm_yday=334, tm_isdst=0) # converts a struct_time format into a timestamp: t = (2017, 7, 11, 0, 10, 18, 1,192, 0) timestamp1 = time.mktime (time.struct_time (t)) print ("convert a time format similar to a tuple to a timestamp:" Timestamp1) # converts the struct_time format to the specified format such as:% Y-%m-%d% H:%M:%Sformat_time1 = time.strftime ("% Y-%m-%d% H:%M:%S", t) # 2017-07-11 00:10:18print ("convert the time format similar to the element form to the specified format such as:% Y-%m-%d% H:%M:%S:" Format_time1) # convert formatted time into struct_time format tuple_struct2 = time.strptime ("2017-07-11 00:10:18", "% Y-%m-%d% H:%M:%S") print ("convert formatted time into tuple format:" Tuple_struct2)''the result of the run converts a timestamp into a time format similar to a tuple: time.struct_time (tm_year=2017, tm_mon=7, tm_mday=11, tm_hour=0, tm_min=10, tm_sec=18, tm_wday=1, tm_yday=192) Tm_isdst=0) converts a time format similar to a tuple to a timestamp: 1499703018.0 converts a time format similar to an element to a specified format such as:% Y-%m-%d% H:%M:%S: 2017-07-11 00:10:18 converts the formatted time into a tuple time format: time.struct_time (tm_year=2017, tm_mon=7, tm_mday=11, tm_hour=0, tm_min=10, tm_sec=18, tm_wday=1) Tm_yday=192, tm_isdst=-1)''
To make it easy to understand and remember the conversion of these common time formats, please take a look at the following figure:
Reference help information for time formatting
Reference help when formatting time:% a Local (locale) simplified week name% A Local full week name% b Local simplified month name% B Local full month name% c Local corresponding date and time represents% d the day ordinal (01-31)% H of the day (24-hour system) 00-23) hours of I (12 hours, 01-12) j days of the year (001-366) m months (01-12) M minutes (00-59)% p corresponding% S seconds (01-61)% U weeks of the year for local am or pm. Sunday is the beginning of the week. ) all days before the first Sunday are placed in week 0. % w the day of the week (0-6 is Sunday) 3% W and% U are basically the same, except that% W begins the week with Monday. % x local corresponding date% X local corresponding time% y minus century year (00-99)% Y complete year% Z time zone name (if empty character does not exist)%% 'character''
# # #
The datetime module re-encapsulates the time module and provides more classes such as date, time, datetime, timedelta and so on.
Examples of common uses of 1.datetim.data
#! / usr/bin/env python#-*-coding:utf-8-*-import datetime# gets a date object d = datetime.date (2017 datetime.date) # Parameter year, month, day # maximum date that data can represent d_max = d.maxprint ("maximum date that data can represent:", d_max) # minimum date that data can represent d_min = d.minprint ("maximum date that data can represent:" D.min) # get current date d_today = d.today () print ("get current date:", d.today ()) # get year, month, day year = d.yearmonth = d.monthday = d.dayprint ("get year% s, month% s, day% s"% (year,month,day)) # return the corresponding date d_date = d.fromtimestamp (12312432432) print ("return the corresponding date based on the given timestamp:" D_date) # returns weekday Return 0 on Monday and 1 on Tuesday By analogy, 2017-7-14 is Friday d_weekday = d.weekday () print ("return weekday is:", d_weekday) # returns the string formatted as (YYYY-MM-DD) d_format = d.isoformat () print ("returns the string formatted (YYYY-MM-DD):" D_format) # returns a formatted date string similar to the time module's strftimed_strftime = d.strftime ("% Y-%m-%d% H:%M:%S") print ("formatted string:", d_strftime) # returns the struct_time form of time d_tuple = d.timetuple () print ("struct_time form of return time:" D_tuple) the maximum date that data can represent: 9999-12-31data can represent the maximum date: 0001-01-01 get the current date: 2017-07-14 get year 2017, month 7, Date 14 returns the corresponding date according to the given timestamp: 2360-03-02 returns weekday: 4 returns YYYY-MM-DD string: 2017-07-14 formatted string: 2017-07-14 00:00:00 return time tuple (struct_time) form: time.struct_time (tm_year=2017) Tm_mon=7, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=-1)''
Examples of common uses of 2.datetime.time
#! / usr/bin/env python#-*-coding:utf-8-*-import datetimedt = datetime.time # Parameter hours, minutes, seconds # maximum time that time can express dt_max = dt.maxprint ("maximum time that can be expressed:", dt_max) # minimum time that time can represent dt_min = dt.minprint ("minimum time that can be expressed:" Dt_min) # get time, minute, second, subtle hour = dt.hourminute = dt.minutesecond = dt.secondmicrosecond = dt.microsecondprint ("get% s, minute% s, second% s, subtle% s"% (hour,minute,second,microsecond)) # format string (HH:MM:SS) dt_format = dt.isoformat () print ("format string (HH:MM:SS):", dt_format) # format the return time Strftimedt_strftime = dt.strftime ("% H:%M:%S") print ("format:", dt_strftime) 'maximum time that can be expressed by the running result of time module: the minimum time that can be expressed by the run result is: 1700 hours at 00:00:00, 30 minutes, 0 seconds, and subtle 0 seconds. Format string (HH:MM:SS): 17:30:00 format: 1730 sets, 00 seconds.
Examples of common uses of 3.datetime.datetime
#! / usr/bin/env python#-*-coding:utf-8-*-import datetime# get the current local time d_today = datetime.datetime.today () print ("get current local time:", d_today) # get the current time if the time zone parameter tz is provided Then d_now = datetime.datetime.now () print ("get current time:", d_now) # get the current world standard time (i.e. Greenwich mean astronomical time) d_utc = datetime.datetime.utcnow () print ("get the current world standard time (i.e. Greenwich mean astronomical time):", d_utc) # convert the timestamp into a formatted string You can convert the time zone parameter tz to the formatting time of the specified time zone d_fromtimestamp = datetime.datetime.fromtimestamp (123214324) print ("convert the timestamp into a formatted string:", d_fromtimestamp) # format the timestamp with the utc standard time zone time according to the timestamp d_utcfromtimestam = datetime.datetime.utcfromtimestamp (123214324) print ("convert the timestamp into a formatted string with the utc standard time zone time:" D_utcfromtimestam)''Note: through the comparison of the output above, we will find that the time obtained by converting the unified timestamp between fromtimestamp and utcfromtimestamp is exactly the same as 8h'''#dd = datetime.datetime (2017 L7 and 1400) # converts the specified time into a formatted string Time-like strftimedd_strftime = dd.strftime ("% Y-%m-%d% H:%M:%S") print ("format the specified time", dd_strftime) # returns weekday Return 0 on Monday and 1 on Tuesday By analogy, 2017-7-14 is Friday dd_weekday = dd.weekday () print ("return weekday is:", dd_weekday) # get the time format in the form of struct_time dd_timetuple = dd.timetuple () print ("get the time format in the form of struct_time:" Dd_timetuple) # dd_utctimetuple = dd.utctimetuple () # get by utc standard # returns a string dd_isoformat = dd.isoformat () print formatted as% Y-%m-%d% H:%M:%S ("returns a string formatted as% Y-%m-%d% H:%M:%S:" Dd_isoformat)''run the result to get the current local time: 2017-07-14 18V 57V 59.309859 get the current time: 2017-07-14 18V 57V 59.309858 get the current world standard time (Greenwich Astronomical time): 2017-07-14 10 57V 57V 59.309858 convert the timestamp into a formatted string: 1973-11-27 10:12:04 convert the timestamp to the utc standard Time zone time to convert to formatted string: 1973-11-27 02:12:04 format the specified time 2017-07-14 17:30:00 return weekday as: 4 get the time format in the form of struct_time: time.struct_time (tm_year=2017) Tm_mon=7, tm_mday=14, tm_hour=17, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=-1) returns a string formatted as% Y-%m-%d% H:%M:%S: 2017-07-14T17pur30purl 00room'
Examples of common uses of 4.datetime.timedelta
#! / usr/bin/env python#-*-coding:utf-8-*-# Author:Yaosheng Wangimport datetime# get current time now_date = datetime.datetime.now () print ("get current time:", now_date) # time after five days five_later = datetime.datetime.now () + datetime.timedelta (5) time ("time after five days:" Five_later) # time obtained five days ago five_ago = datetime.datetime.now ()-datetime.timedelta (5) print ("time five days ago:", five_ago) # time after three hours acquisition three_hour_later = datetime.datetime.now () + datetime.timedelta (hours=3) print ("time after three hours acquisition:" Three_hour_later)''run the result to get the current time: 2017-07-14 1900VIED 04.955596 five days after the acquisition: 2017-07-19 19VOBG 04.955596 get the time five days ago: 2017-07-09 19MAV 04.955596 three hours after the acquisition: 2017-07-14 2200Ranger 04.955596
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.