Introduction
01 import time
02 import datetime
03
04 print "Today is day", time.localtime()[7], "of the current year"
05 # Today is day 315 of the current year
06
07 today = datetime.date.today()
08 print "Today is day", today.timetuple()[7], "of ", today.year
09 # Today is day 315 of 2009
10
11 print "Today is day", today.strftime("%j"), "of the current year"
12 # Today is day 315 of the current year
Finding Today's Date
01 #-----------------------------
02 # Finding todays date
03
04 today = datetime.date.today()
05 print "The date is", today
06 #=> The date is 2009-11-11
07
08 # the function strftime() (string-format time) produces nice formatting
09 # All codes are detailed at http://www.python.org/doc/current/lib/module-time.html
10 print today.strftime("four-digit year: %Y, two-digit year: %y, month: %m, day: %d")
11 #=> four-digit year: 2009, two-digit year: 09, month: 11, day: 11
Converting DMYHMS to Epoch Seconds
01 #-----------------------------
02 # Converting DMYHMS to Epoch Seconds
03 # To work with Epoch Seconds, you need to use the time module
04
05 # For the local timezone
06 t = datetime.datetime.now()
07 print "Epoch Seconds:", time.mktime(t.timetuple())
08 #=> Epoch Seconds: 1257918741.0
09
10 # For UTC
11 t = datetime.datetime.utcnow()
12 print "Epoch Seconds:", time.mktime(t.timetuple())
13 #=> Epoch Seconds: 1257889941.0
Converting Epoch Seconds to DMYHMS
01 #-----------------------------
02 # Converting Epoch Seconds to DMYHMS
03
04 EpochSeconds = time.mktime(datetime.datetime.utcnow().timetuple())
05 now = datetime.datetime.fromtimestamp(EpochSeconds)
06 #or use datetime.datetime.utcfromtimestamp()
07 print now
08 #=> 2009-11-11 05:58:58
09 print now.ctime()
10 #=> Wed Nov 11 05:58:58 2009
11
12 # or with the time module
13 oldtimetuple = time.localtime(EpochSeconds)
14 # oldtimetuple contains (year, month, day, hour, minute, second, weekday, yearday, daylightSavingAdjustment)
15 print oldtimetuple
16 #=> (2009, 11, 11, 5, 58, 58, 2, 315, 0)
Adding to or Subtracting from a Date
01 #-----------------------------
02 # Adding to or Subtracting from a Date
03 # Use the rather nice datetime.timedelta objects
04
05 now = datetime.date(2009, 11, 1)
06 difference1 = datetime.timedelta(days=1)
07 difference2 = datetime.timedelta(weeks=-2)
08
09 print "One day in the future is:", now + difference1
10 #=> One day in the future is: 2009-11-02
11
12 print "Two weeks in the past is:", now + difference2
13 #=> Two weeks in the past is: 2009-10-18
14
15 print datetime.date(2003, 8, 6) - datetime.date(2000, 8, 6)
16 #=> 1095 days, 0:00:00
17
18 #-----------------------------
19 birthtime = datetime.datetime(1987, 01, 8, 3, 45, 50) # 1987-01-08 03:45:50
20
21 interval = datetime.timedelta(seconds=5, minutes=17, hours=2, days=55)
22 then = birthtime + interval
23
24 print "Then is", then.ctime()
25 #=> Then is Wed Mar 4 06:02:55 1987
26
27 print "Then is", then.strftime("%A %B %d %I:%M:%S %p %Y")
28 #=> Then is Wednesday March 04 06:02:55 AM 1987
29
30 #-----------------------------
31 when = datetime.datetime(2009, 11, 11) + datetime.timedelta(days=55)
32 print "Nat was 55 days old on:", when.strftime("%m/%d/%Y").lstrip("0")
33 #=> Nat was 55 days old on: 1/05/2010
Difference of Two Dates
01 #-----------------------------
02 # Dates produce timedeltas when subtracted.
03
04 # diff = date2 - date1
05 # diff = datetime.date(year1, month1, day1) - datetime.date(year2, month2, day2)
06 #-----------------------------
07
08 bree = datetime.datetime(1981, 6, 16, 4, 35, 25)
09 nat = datetime.datetime(1973, 1, 18, 3, 45, 50)
10
11 difference = bree - nat
12 print "There were", difference, "minutes between Nat and Bree"
13 #=> There were 3071 days, 0:49:35 minutes between Nat and Bree
14
15 weeks, days = divmod(difference.days, 7)
16
17 minutes, seconds = divmod(difference.seconds, 60)
18 hours, minutes = divmod(minutes, 60)
19
20 print "%d weeks, %d days, %d:%d:%d" % (weeks, days, hours, minutes, seconds)
21 #=> 438 weeks, 5 days, 0:49:35
22
23 #-----------------------------
24 print "There were", difference.days, "days between Bree and Nat."
25 #=> There were 3071 days between bree and nat
Day in a Week/Month/Year or Week Number
01 #-----------------------------
02 # Day in a Week/Month/Year or Week Number
03
04 when = datetime.date(1981, 6, 16)
05
06 print "16/6/1981 was:"
07 print when.strftime("Day %w of the week (a %A). Day %d of the month (%B).")
08 print when.strftime("Day %j of the year (%Y), in week %W of the year.")
09
10 #=> 16/6/1981 was:
11 #=> Day 2 of the week (a Tuesday). Day 16 of the month (June).
12 #=> Day 167 of the year (1981), in week 24 of the year.
Parsing Dates and Times from Strings
01 #-----------------------------
02 # Parsing Dates and Times from Strings
03
04 time.strptime("Tue Jun 16 20:18:03 1981")
05 # (1981, 6, 16, 20, 18, 3, 1, 167, -1)
06
07 time.strptime("16/6/1981", "%d/%m/%Y")
08 # (1981, 6, 16, 0, 0, 0, 1, 167, -1)
09 # strptime() can use any of the formatting codes from time.strftime()
10
11 # The easiest way to convert this to a datetime seems to be;
12 now = datetime.datetime(*time.strptime("16/6/1981", "%d/%m/%Y")[0:5])
13 # the '*' operator unpacks the tuple, producing the argument list.
Printing a Date
1 #-----------------------------
2 # Printing a Date
3 # Use datetime.strftime() - see helpfiles in distro or at python.org
4
5 print datetime.datetime.now().strftime("The date is %A (%a) %d/%m/%Y")
6 #=> The date is Wednesday (Wed) 11/11/2009
High-Resolution Timers
01 #-----------------------------
02 # High Resolution Timers
03
04 t1 = time.clock()
05 # Do Stuff Here
06 t2 = time.clock()
07 print t2 - t1
08
09 # 2.27236813618
10 # Accuracy will depend on platform and OS,
11 # but time.clock() uses the most accurate timer it can
12
13 time.clock(); time.clock()
14 # 174485.51365466841
15 # 174485.55702610247
16
17 #-----------------------------
18 # Also useful;
19 import timeit
20 code = '[x for x in range(10) if x % 2 == 0]'
21 eval(code)
22 # [0, 2, 4, 6, 8]
23
24 t = timeit.Timer(code)
25 print "10,000 repeats of that code takes:", t.timeit(10000), "seconds"
26 print "1,000,000 repeats of that code takes:", t.timeit(), "seconds"
27
28 # 10,000 repeats of that code takes: 0.128238644856 seconds
29 # 1,000,000 repeats of that code takes: 12.5396490336 seconds
30
31 #-----------------------------
32 import timeit
33 code = 'import random; l = random.sample(xrange(10000000), 1000); l.sort()'
34 t = timeit.Timer(code)
35
36 print "Create a list of a thousand random numbers. Sort the list. Repeated a thousand times."
37 print "Average Time:", t.timeit(1000) / 1000
38 # Time taken: 5.24391507859
Short Sleeps
1 #-----------------------------
2 # Short Sleeps
3
4 seconds = 3.1
5 time.sleep(seconds)
6 print "boo"
转自:http://hello-math.appspot.com/2009/11/11/python-datetime-handing.html
固定链接: Python中的日期和时间的处理 | 丕子
+复制链接





最新评论
今天在电台上听到了,女主持人
学校有专门的tex模板, 本
喜欢,分享了!
新年快乐!
一般发国外期刊采用得到tex
我在的学校有一个博士维护的t
画图工具用matlab或者e
人工编号后期修改好麻烦;自动
真厉害。觉得这种Graphi
什么公司啊,丕子? 好久没