How to use Python datetime and time
Time method often used
1.Get now time:
Use time method, get the now Timestamp
In [42]: time.time()
Out[42]: 1408066927.208922
Change timestamp to time tuple: struct_time
In [43]: time.localtime(time.time())
Out[43]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=9, tm_min=42, tm_sec=20, tm_wday=4, tm_yday=227, tm_isdst=0)
Formatted output desired time
In [44]: time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
Out[44]: '2014-08-15 09:43:04'
Connected to the text, when no parameters, the default is the output current time
In [48]: time.strftime('%Y-%m-%d %H:%M:%S')
Out[48]: '2014-08-15 09:46:53’
Of course, can be achieved through the datetime module, as follows:
In [68]: t = time.time()
In [69]: datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
Out[69]: '2014-08-15 10:04:51’
Also, you can only use the datetime module:
In [46]: datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Out[46]: '2014-08-15 09:45:27’
In [47]: datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
Out[47]: '2014-08-15 09:46:10'
2.Get time difference,Calculation program execution time etc.
Use time module: In [75]: def t(): ....: start = time.time() ....: time.sleep(10) ....: end = time.time() ....: print end - start ....: In [76]: t() 10.0014948845 Use datetime module: In [49]: starttime = datetime.datetime.now() In [50]: endtime = datetime.datetime.now() In [51]: print (endtime - starttime).seconds 6
3.Yesterday’s date calculations
In [52]: d1 = datetime.datetime.now() In [53]: d2 = d1 - datetime.timedelta(days=1) In [54]: d1 Out[54]: datetime.datetime(2014, 8, 15, 9, 54, 10, 68665) In [55]: d2 Out[55]: datetime.datetime(2014, 8, 14, 9, 54, 10, 68665)
4.Struct_time: time tuple into a timestamp
In [56]: datetime.datetime.now() Out[56]: datetime.datetime(2014, 8, 15, 9, 57, 52, 779893) In [57]: datetime.datetime.now().timetuple() Out[57]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=9, tm_min=58, tm_sec=12, tm_wday=4, tm_yday=227, tm_isdst=-1) In [58]: time.mktime(datetime.datetime.now().timetuple()) Out[58]: 1408067904.0
5.strptime: time to convert a string to a time tuple struct_time.
In [73]: time.strftime('%Y-%m-%d %H:%M:%S')
Out[73]: '2014-08-15 10:27:36'
In [74]: time.strptime('2014-08-15 10:27:36','%Y-%m-%d %H:%M:%S')
Out[74]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=10, tm_min=27, tm_sec=36, tm_wday=4, tm_yday=227, tm_isdst=-1)
time and datetime
Representing time in two ways
1. timestamp (relative to 1970.1.1 00:00:00 seconds offset calculation), the time stamp is the only
2. Time tuples that (struct_time), a total of nine elements, respectively, with a timestamp struct_time because of the different time zones and different
time module commonly used method
1.time.clock()
budong@budongdeMacBook-Pro:/tmp$ cat clock.py
#!/usr/bin/env python
import time
if __name__ == '__main__':
time.sleep(1)
print "clock1:%s" % time.clock()
time.sleep(1)
print "clock2:%s" % time.clock()
time.sleep(1)
print "clock3:%s" % time.clock()
Running:
budong@budongdeMacBook-Pro:/tmp$ ./clock.py
clock1:0.059173
clock2:0.059299
clock3:0.059416
2.time.sleep(secs)
Delayed the specified time to run a thread
Suitable for a script, a sleep timer will then continue doing
In [138]: while True:
.....: time.sleep(3)
.....: print time.strftime('%H:%M:%S')
.....:
17:21:35
17:21:38
17:21:41
17:21:44
……
3.time.localtime([secs])
A timestamp into a current struct_time time zone, if not seconds parameter input, places the current time is to convert standard Unavailable secs parameters, according to the current time, whichever In [141]: time.localtime() Out[141]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=14, tm_hour=17, tm_min=23, tm_sec=48, tm_wday=3, tm_yday=226, tm_isdst=0) Provide for the current timestamp when secs In [142]: time.time() Out[142]: 1408008232.217969 In [143]: time.localtime(time.time()) Out[143]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=14, tm_hour=17, tm_min=24, tm_sec=2, tm_wday=3, tm_yday=226, tm_isdst=0)
4.time.strftime(format[,t])
Specified struct_time (defaults to the current time), according to the specified format string output
"t" is not specified, the incoming time.localtime () as the default parameters:
In [156]: time.strftime('%Y-%m-%d %H:%M:%S')
Out[156]: '2014-08-14 17:28:16’
T specify when time.localtime (1407945600.0):
In [157]: time.localtime(1407945600.0)
Out[157]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=226, tm_isdst=0)
In [158]: time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(1407945600.0))
Out[158]: '2014-08-14 00:00:00’
5.time.time()
Returns the timestamp of the current time In [161]: time.time() Out[161]: 1408008711.730218
6.time.mktime(t)
Struct_time convert a timestamp, as time.localtime receives a timestamp returns a struct_time, while time.mktime receive a struct_time, returns a timestamp In [159]: time.localtime(1407945600.0) Out[159]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=226, tm_isdst=0) In [160]: time.mktime(time.localtime(1407945600.0)) Out[160]: 1407945600.0
datetime module commonly used method
datetime module commonly used are the following four categories:
1. datetime.date: The date refers to the date of composition (equivalent to the calendar)
2. datetime.time: refers to the day, 24 hours minutes and seconds microseconds constituted a specific time (equivalent to watch)
3. datetime.datetime: The above two together, contains both time and contain a date
4. datetime.timedelta: Interval object (timedelta). A point in time (datetime) plus an interval (timedelta) can get a new point in time (datetime). For example, today’s 3:00 plus five hours to get today’s 8:00. Similarly, the two time points will be subtracted from a time interval.
1.datetime.date
1.create a date object,the date is today, it can call datetime.date.today(), also can to datetime.date() by value, look the following:
In [4]: today = datetime.date.today()
In [5]: today
Out[5]: datetime.date(2014, 8, 15)
In [6]: t = datetime.date(2014,8,15)
In [7]: t
Out[7]: datetime.date(2014, 8, 15)
2.datetime.date.strftime(format) format datetime,like the format "Y-m-d H:M:S"
In [8]: today.strftime('%Y-%m-%d %H:%M:%S')
Out[8]: '2014-08-15 00:00:00’
date object in hours, minutes, seconds, the default is 0, that era in time.
3.datetime.date.timple() change to struct-time format,when transfer to tiem.mktime(t), it will change to timestamp format
In [9]: today.timetuple()
Out[9]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=227, tm_isdst=-1)
In [10]: time.mktime(today.timetuple())
Out[10]: 1408032000.0
4.datetime.date.replace(year, month, day) Returns a date object after replacement
In [11]: today.replace(year=2013)
Out[11]: datetime.date(2013, 8, 15)
5.datetime.date.fromtimestamp(timestamp) The timestamp change to a date object
In [12]: datetime.date.fromtimestamp(1408058729)
Out[12]: datetime.date(2014, 8, 15)
2.datetime.time
1.create a new time object
In [15]: t
Out[15]: datetime.time(8, 45, 20)
2.datetime.time.(format) format out
In [16]: t.strftime('%Y-%m-%d %H:%M:%S')
Out[16]: '1900-01-01 08:45:20’
time corresponding to the year, month, day 1900,01,01, in that time era.
3.datetime.time.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]]) Return time after replacement object.
In [17]: t.replace(hour=9)
Out[17]: datetime.time(9, 45, 20)
3.datetime.datetime
1.Create a "datetime"" object, dated today, either directly call "datetime.datetime.today()", or directly pass values to "datetime.datetime()", as follows:
In [21]: d1 = datetime.datetime.today()
In [22]: d1
Out[22]: datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
In [23]: d2 = datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
In [24]: d2
Out[24]: datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
2.datetime.datetime.now([tz]) when now select timezone, it's the same with datetime.datetime.today().
In [25]: datetime.datetime.now()
Out[25]: datetime.datetime(2014, 8, 15, 8, 14, 50, 738672)
3..datetime.datetime.strftime(format) format a datetime which we need, like "Y-m-d H:M:S".
In [27]: d1
Out[27]: datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
In [28]: d1.strftime('%Y-%m-%d %H:%M:%S')
Out[28]: '2014-08-15 08:12:34’
4.datetime.datetime.timple() Turn into struct_time format, so passed to time.mktime (t), and directly transferred into a timestamp format.
In [29]: d1
Out[29]: datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
In [30]: d1.timetuple()
Out[30]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=8, tm_min=12, tm_sec=34, tm_wday=4, tm_yday=227, tm_isdst=-1)
In [31]: time.mktime(d1.timetuple())
Out[31]: 1408061554.0
5.datetime.datetime.replace(year, month, day) Returns a date object after replacement.
In [32]: d1
Out[32]: datetime.datetime(2014, 8, 15, 8, 12, 34, 790945)
In [33]: d1.replace(year=2000)
Out[33]: datetime.datetime(2000, 8, 15, 8, 12, 34, 790945)
6.datetime.datetime.fromtimestamp(timestamp) The timestamp into a datetime object.
In [34]: time.time()
Out[34]: 1408061894.081552
In [35]: datetime.datetime.fromtimestamp(1408061894)
Out[35]: datetime.datetime(2014, 8, 15, 8, 18, 14)
4.datetime.timedelta
In [78]: today = datetime.datetime.today() In [79]: yesterday = today - datetime.timedelta(days=1) In [80]: yesterday Out[80]: datetime.datetime(2014, 8, 14, 15, 8, 25, 783471) In [81]: today Out[81]: datetime.datetime(2014, 8, 15, 15, 8, 25, 783471)