Python study, time module, example source code

import time

print( time.time() ) #the timestamp after 1970.1.1
#1393854889.330645
print( time.gmtime() ) #UTC
#time.struct_time(tm_year=2014, tm_mon=3, tm_mday=3, tm_hour=13, tm_min=54, tm_sec=49, tm_wday=0, tm_yday=62, tm_isdst=0)
print( time.localtime() ) #current time based on system
#time.struct_time(tm_year=2014, tm_mon=3, tm_mday=3, tm_hour=22, tm_min=54, tm_sec=49, tm_wday=0, tm_yday=62, tm_isdst=0)
t = time.gmtime(1234567890)
print( t )
#time.struct_time(tm_year=2009, tm_mon=2, tm_mday=13, tm_hour=23, tm_min=31, tm_sec=30, tm_wday=4, tm_yday=44, tm_isdst=0)

print( t.tm_mon )
print( t.tm_hour )
#2
#23

print( time.asctime(t) )
print( time.mktime(t) )
#Fri Feb 13 23:31:30 2009
#1234535490.0


#sleep
t = time.time()
time.sleep(1) #10 second sleep
t2 = time.time()

spendtime = t2 - t
print( "before timestamp: ", t)
print( "after timestamp: ", t2)
print( "wait {0} second".format(spendtime))
#before timestamp:  1393855412.738972
#after timestamp:  1393855413.740086
#wait 1.0011141300201416 second

from time import localtime, strftime
print( strftime( "%B %dth %A %I:%M", localtime() ) )
print( strftime( "%Y-%m-%d %I:%M", localtime() ) )
print( strftime( "%y/%m/%d %H:%M:%S", localtime() ) )
print( strftime("%y/%m/%d %H:%M:%S", localtime() ) )
print( strftime("%x %X", localtime()) )

#March 03th Monday 11:03
#2014-03-03 11:03
#14/03/03 23:03:33
#14/03/03 23:03:33
#03/03/14 23:03:33

import datetime
print( datetime.date(2009, 5, 5) )
#2009-05-05
print( datetime.date.fromtimestamp( time.time() ) )
#2014-03-03
print( datetime.date.today() )
#2014-03-03


d = datetime.date.today()
print( d.year )
print( d.month )
print( d.day )
print( d.max )
print( d.min )
#2014
#3
#3
#9999-12-31
#0001-01-01

d = datetime.date.today()
d2 = d.replace(day=25)
print(d, d2)
#2014-03-03 2014-03-25

d.timetuple()
print( d.toordinal() )
print( d.weekday() )
#735295
#0 -> It means monday.

d = datetime.date.today()
print( d.isoformat() )
print( d.ctime() )
#2014-03-03
#Mon Mar  3 00:00:00 2014

from datetime import time
print( time(7) )
#07:00:00
print( time(8, 14, 20, 3000 ) )
#08:14:20.003000

print( time(hour=3, second=3) )
#03:00:03

print("-----")

from datetime import datetime, date, time
print( datetime.now() )
print( datetime.today() )
print( datetime.utcnow() )
print( datetime.fromtimestamp(1234567890))
print( datetime.utcfromtimestamp(1234567890))
print( datetime.fromordinal(732421))
d = date(2009, 3, 10)
t = time(12, 23, 53)
print( datetime.combine(d, t) )
#014-03-03 23:22:08.580259
#2014-03-03 23:22:08.580277
#2014-03-03 14:22:08.580292
#2009-02-14 08:31:30
#2009-02-13 23:31:30
#2006-04-20 00:00:00
#2009-03-10 12:23:53

print("----")

from datetime import datetime
dt = datetime.now()
print( dt.date() )
print( dt.time() )
print( dt.replace(hour=20, second=30 ) )
print( dt.timetuple() )
#2014-03-03
#23:25:15.304432
#2014-03-03 20:25:30.304432
#time.struct_time(tm_year=2014, tm_mon=3, tm_mday=3, tm_hour=23, tm_min=25, tm_sec=15, tm_wday=0, tm_yday=62, tm_isdst=-1)


print("----")
dt = datetime.now()
print( dt.weekday() )
print( dt.isoweekday() )
#0
#1

print(dt.isoweekday())
print(dt.ctime())
print( str(dt) )
#1
#Mon Mar  3 23:27:26 2014
#2014-03-03 23:27:26.117400

print(" timedelta class ");
from datetime import timedelta
print( timedelta(days=-3 ) )
print( timedelta( hours=7) )
#-3 days, 0:00:00
#7:00:00

print( timedelta(weeks=2, days=3, hours=-3, minutes=30) )
print( timedelta(minutes=3, milliseconds=-20, microseconds=400))
#16 days, 21:30:00
#0:02:59.980400

print("---")

from datetime import timedelta

td_1 = timedelta(hours = 7 )
td_2 = timedelta(days=-3)
print( td_1 + td_2 )
#-3 days, 7:00:00

print( td_1-td_2, td_1+td_2, td_1*4, td_1//3, abs(td_2))
#3 days, 7:00:00 -3 days, 7:00:00 1 day, 4:00:00 2:20:00 3 days, 0:00:00

td_1 = timedelta(hours = 7 )
td_2 = timedelta(days=-3)
print( td_1 > td_2 )
print( td_1 < td_2 )
#True
#False

td_1 = timedelta(hours=24)
td_2 = timedelta(seconds=86400)
print( td_1 == td_2 )
#True

Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)