17. datetime Flashcards
covert second to readable format, or vice versa
system time is stored as timestamp (i.e. second)
- time.ctime -> str
- dtobj
datetime. fromtimestamp(dtobj)
to revert
3.dtobj.timestamp()
get current time
now = datetime.now()
timedelta object
year = timedelta(days = 365)
create datetime object
datetime(2020, 5, 17)\
datetime(2020, 5, 17,12,30,30)
must be integer
print time format
dtobj.strftime ** f for format
Convert object to a string according to a given format
from str to datetime
datetime.strptime(str)
Parse a string into a datetime object given a corresponding format
import datetime
if you don’t want to repeat too many datetime
use
from datetime import datetime, timedelta
count run time (timeit)
Your statements here
import timeit ** in second, and not in datetime module
start = timeit.default_timer()
stop = timeit.default_timer()
print(‘Time: ‘, stop - start)
timeit can actually run it over and over again
note stmt and setup are both block string
setup will only run one, i.e. defining the function
stmt is the stmt ran multiple times, i.e. calling the function
timeit.timeit(stmt, setup, number = 10000)
%%timeit (for jupyter magic keyword)
myfunction