17. datetime Flashcards

1
Q

covert second to readable format, or vice versa

A

system time is stored as timestamp (i.e. second)

  1. time.ctime -> str
  2. dtobj
    datetime. fromtimestamp(dtobj)

to revert
3.dtobj.timestamp()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

get current time

A

now = datetime.now()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

timedelta object

A

year = timedelta(days = 365)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

create datetime object

A

datetime(2020, 5, 17)\
datetime(2020, 5, 17,12,30,30)
must be integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

print time format

A

dtobj.strftime ** f for format

Convert object to a string according to a given format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

from str to datetime

A

datetime.strptime(str)

Parse a string into a datetime object given a corresponding format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

import datetime

A

if you don’t want to repeat too many datetime
use
from datetime import datetime, timedelta

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

count run time (timeit)

A

Your statements here

import timeit ** in second, and not in datetime module

start = timeit.default_timer()

stop = timeit.default_timer()

print(‘Time: ‘, stop - start)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

timeit can actually run it over and over again

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly