Python
Python
Python time calendar
197011
Python time time.time(), :
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time # time
ticks = time.time()
print ":", ticks
: 1459994552.51
1970UNIXWindows2038
Python9:
| 0 | 4 | 2008 |
| 1 | 1 12 | |
| 2 | 131 | |
| 3 | 023 | |
| 4 | 059 | |
| 5 | 061 (6061 ) | |
| 6 | 06 (0) | |
| 7 | 1366 () | |
| 8 | -1, 0, 1, -1 |
struct_time
| 0 | tm_year | 2008 |
| 1 | tm_mon | 1 12 |
| 2 | tm_mday | 1 31 |
| 3 | tm_hour | 0 23 |
| 4 | tm_min | 0 59 |
| 5 | tm_sec | 0 61 (6061 ) |
| 6 | tm_wday | 06 (0) |
| 7 | tm_yday | 1 366() |
| 8 | tm_isdst | -1, 0, 1, -1 |
localtime
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
localtime = time.localtime(time.time())
print " :", localtime
: time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)
asctime():
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
localtime = time.asctime( time.localtime(time.time()) )
print " :", localtime
: Thu Apr 7 10:05:21 2016
time strftime
time.strftime(format[, t])
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
# 2016-03-20 11:45:39
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# Sat Mar 28 22:24:24 2016
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
#
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
2016-04-07 10:25:09 Thu Apr 07 10:25:09 2016 1459175064.0
python
- %y 00-99
- %Y 000-9999
- %m 01-12
- %d 0-31
- %H 240-23
- %I 1201-12
- %M 00-59
- %S 00-59
- %a
- %A
- %b
- %B
- %c
- %j 001-366
- %p A.M.P.M.
- %U 00-53
- %w 0-6
- %W 00-53
- %x
- %X
- %Z
- %% %
Calendar
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import calendar
cal = calendar.month(2016, 1)
print "20161:"
print cal
20161:
January 2016
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Time
Time
| 1 | time.altzone |
| 2 | time.asctime([tupletime]) "Tue Dec 11 18:07:14 2008"2008121118071424 |
| 3 | time.clock( ) CPUtime.time() |
| 4 | time.ctime([secs]) asctime(localtime(secs))asctime() |
| 5 | time.gmtime([secs]) 1970tt.tm_isdst0 |
| 6 | time.localtime([secs]) 1970tt.tm_isdst01 |
| 7 | time.mktime(tupletime) 1970 |
| 8 | time.sleep(secs) secs |
| 9 | time.strftime(fmt[,tupletime]) fmt |
| 10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') fmt |
| 11 | time.time( ) 1970 |
| 12 | time.tzset() TZ |
Time2
| 1 | time.timezone time.timezone >0<=0 |
| 2 | time.tzname time.tzname |
Calendar
calendar.setfirstweekday()
| 1 | calendar.calendar(year,w=2,l=1,c=6) year3c w21* W+18+2* Cl |
| 2 | calendar.firstweekday( ) calendar 0 |
| 3 | calendar.isleap(year) True False >>> import calendar >>> print(calendar.isleap(2000)) True >>> print(calendar.isleap(1900)) False |
| 4 | calendar.leapdays(y1,y2) Y1Y2 |
| 5 | calendar.month(year,month,w=2,l=1) yearmonthw7* w+6l |
| 6 | calendar.monthcalendar(year,month) Yearmonth0;1 |
| 7 | calendar.monthrange(year,month) 06;112 |
| 8 | calendar.prcal(year,w=2,l=1,c=6) print calendar.calendar(year,w=2,l=1,c=6) |
| 9 | calendar.prmonth(year,month,w=2,l=1) print calendar.month(year,month,w=2,l=1) |
| 10 | calendar.setfirstweekday(weekday) 06 |
| 11 | calendar.timegm(tupletime) time.gmtime1970 |
| 12 | calendar.weekday(year,month,day) 06 1 1212 |
Python
353***[email protected]
datetime
#!/usr/bin/python # -*- coding: UTF-8 -*- import datetime i = datetime.datetime.now() print (" %s" % i) print ("ISO %s" % i.isoformat() ) print (" %s" %i.year) print (" %s" %i.month) print (" %s" %i.day) print ("dd/mm/yyyy %s/%s/%s" % (i.day, i.month, i.year) ) print (" %s" %i.hour) print (" %s" %i.minute) print (" %s" %i.second)353***[email protected]
894***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- import time import calendar """ : 0-6 : 1-366 : -1, 0, 1 """ """ python ------------------------------------ %y 00-99 %Y 000-9999 %m 01-12 %d 0-31 %H 240-23 %I 1201-12 %M 00=59 %S 00-59 %a %A %b %B %c %j 001-366 %p A.M.P.M. %U 00-53 %w 0-6 %W 00-53 %x %X %Z # %% % """ # 1 # 1538271871.226226 time.time() # 2 → # time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=9, tm_min=4, tm_sec=1, tm_wday=6, tm_yday=246, tm_isdst=0) time.localtime() time.localtime(1538271871.226226) # 3 → # time.ctime() time.ctime(1538271871.226226) # 4 → # 1538271871 time.mktime((2018, 9, 30, 9, 44, 31, 6, 273, 0)) # 5 → # time.asctime() time.asctime() time.asctime((2018, 9, 30, 9, 44, 31, 6, 273, 0)) time.asctime(time.localtime(1538271871.226226)) # 6 → # time.strftime() time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 7 → # time.strptime() print(time.strptime('2018-9-30 11:32:23', '%Y-%m-%d %H:%M:%S')) # 8 time.clock()894***[email protected]
____
221***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- from datetime import datetime from datetime import date from datetime import time as time_datetime import time from datetime import timedelta # ------------------1-------------# # print(time.time()) # print(time.localtime(time.time())) print(time.gmtime(time.time())) # print(time.strftime("%Y-%m-%d", time.localtime(time.time()))) print(time.strftime("%Y-%m-%d", time.gmtime(time.time()))) # print(time.strftime("%Y-%m-%d")) # datetime print(datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d")) print(datetime.now().strftime("%Y-%m-%d")) print(datetime.today().strftime("%Y-%m-%d")) # ------------------2-------------# # time start = time.time() time.sleep(1) print(time.time() - start) # datetime start = datetime.now() time.sleep(1) print((datetime.now() - start).seconds) # print(datetime.now() - timedelta(days=1)) # print(time.mktime(time.localtime())) # localtime print(time.mktime(time.gmtime())) # gmtime print(time.mktime(datetime.now().timetuple())) # datetime # print(time.strptime("2019-07-14 11:23:33", "%Y-%m-%d %H:%M:%S")) # # 1. (1970.1.1 00:00:00), # 2. (struct_time),struct_time # ------------------3time-------------# # time.clock # UNIX“” # WINDOWS # WIN32 QueryPerformanceCounter() # start = time.clock() time.sleep(1) print(time.clock() - start) # print(time.localtime()) print(time.localtime(time.time())) # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # print(time.mktime(time.localtime())) print(time.mktime(time.gmtime())) print(time.mktime(datetime.now().timetuple())) # ------------------4datetime-------------# # 1.datetime.date: () # 2.datetime.time: 24() # 3.datetime.datetime: # 4.datetime.timedelta: (timedelta)(datetime)(timedelta) # (datetime)358 # print(date.today()) # print(date(2018, 1, 1)) # print(date.today().strftime("%Y%m%d")) # 0 print(date.today().timetuple()) # print(date.fromtimestamp(time.time())) # datetimetime t = time_datetime(8, 11, 11) print(t) # print(t.strftime("%H-%M-%S")) # datetimedatetime.datetime.today() # datetime.datetime() print(datetime.today()) print(datetime.now()) print(datetime(2014, 8, 15, 8, 12, 34, 790945)) # datetime.datetime.now([tz]) datetime.datetime.today() # print(datetime.now().strftime("%H-%M-%S")) # print(datetime.now().timetuple()) print(time.mktime(datetime.now().timetuple())) # d1 = datetime(2014, 8, 15, 8, 12, 34, 790945) print(d1.replace(year=2000)) # ------------------5timedelta-------------# print(datetime.today()) print(datetime.today() - timedelta(days=1))____
221***[email protected]