파이썬 Datetime 이해하기

100
PYTHON 날날 Moon Yong Joon

Transcript of 파이썬 Datetime 이해하기

PowerPoint

Python Moon Yong Joon

CalendarModule

calendar

: calendar

pytzModule

Time zone

Time classDescriptionTime Stampepoch (1970 1 1 ) Unix UTC(Universal Time Coordinated)1972 ( ) GMT(Greenwich Mean Time) LST(Local Standard Time) UTC 15 1 135 UTC 9 .DST(Daylight Saving Time)

Pytz : timezone

pytz : all_timezones variableDescriptionall_timezonespytz.lazy.LazyList list

pytz : all_timezones_set variableDescriptionall_timezones_setpytz.lazy.LazySet set

pytz : timezone variableDescriptioncommon_timezones pytz.lazy.LazyList common_timezones_setpytz.lazy.LazySet set

pytz : country_names

pytz : country_timezones

pytz : timezone timezone

Pytz.UTC

Pytz.UTC : UTC pytz.UTC pytz.UTC

@classvariable zone : strtzname:str

@classmethodfromutc(dt) : str

@instancemethoddst(self, dt): strfromutc(self,dt) : strlocalize(self,dt) : strnormalize(self,dt) : strutcoffset(self,dt) : str

Pytz UTC class UTC class datetime.tzinfo subclass pytz.UTC

@classvariable zone : strtzname:str

@classmethodfromutc(dt) : str

@instancemethoddst(self, dt): strfromutc(self,dt) : strlocalize(self,dt) : strnormalize(self,dt) : strutcoffset(self,dt) : str

Pytz UTC instance UTC utc pytz.UTC

@classvariable zone : strtzname:str

@classmethodfromutc(dt) : str

@instancemethoddst(self, dt): strfromutc() : strlocalize(self,dt) : strnormalize() : strutcoffset() : str

datetime

Datetime Timezone Pytz Pytz all_timezones .

Timezone pytz.timezone(string)

datatime.datetime.now(tz=pytz.timezone())

offset +8:30 +9:00 . +8:30( 8;28)

pytz :offset

Timezone:localize

timezone : localize timezone localize datetime

Pytz localize() , datetime pytz.tzfile.Asia/Seoul

@classvariable zone : strtzname:str

@classmethodfromutc(dt) : str

@instancemethoddst(self, dt): strfromutc() : strlocalize(self,dt) : strnormalize() : strutcoffset() : str

Asia/Tokyo

Timezone datatime localize datetime datetime.astimezone(pytz.timezone)

Timezone:normalize

timezone : normalize timezone normalize datetime

localize normalize tzinfo timezone localize

: timezone Localize ()

: timedelta timedelta (30)

: pytz.utc Asia/Pyongyang normalize 30

timeModule

time.struct_time class

time.struct_time gmtime(), localtime(), strptime() IndexAttributeValues0tm_year(for example, 1993)1tm_monrange [1, 12]2tm_mdayrange [1, 31]3tm_hourrange [0, 23]4tm_minrange [0, 59]5tm_secrange [0, 61] 6tm_wdayrange [0, 6], Monday is 07tm_ydayrange [1, 366]8tm_isdst0, 1 or -1; see below

Time Stamp classDescriptionTime Stampepoch (1970 1 1 ) Unix

Function : time methodDescriptiontime.time()epoch float time.asctime([t]) gmtime()/localtime() -struct_time :: time.ctime([sec]) epoch ::

UTC(Universal TimeCoordinated) classDescriptionUTC(Universal Time Coordinated)1972 ( )

Function : gmtime methodDescriptiontime.gmtime([sec]) UTC struct_time time.time() struct_time

time.struct_time(tm_year=2017, tm_mon=1, tm_mday=2, tm_hour=0, tm_min=53, tm_sec=9, tm_wday=0, tm_yday=2, tm_isdst=0) Mon Jan 2 00:53:09 2017 1483285989.0

LST(Local Standard Time) classDescriptionLST(Local Standard Time) UTC 15 1 135 UTC 9 .

Function : localtime methodDescriptiontime.localtime([secs]) struct_time , time.time() struct_time

time.struct_time(tm_year=2017, tm_mon=1, tm_mday=2, tm_hour=9, tm_min=50, tm_sec=25, tm_wday=0, tm_yday=2, tm_isdst=0)

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=9, tm_min=6, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Function : mktime methodDescriptiontime.mktime(t) struct_time time()

Function : sleep methodDescriptiontime.sleep(secs)

formatting

Function : strftime struct_time

Function : strptime format

time.struct_time(tm_year=2000, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1)time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)

Time format - 1DirectiveMeaningExample%a Sun, Mon, ..., Sat (en_US);So, Mo, ..., Sa (de_DE)%A Sunday, Monday, ..., Saturday (en_US);Sonntag, Montag, ..., Samstag (de_DE)%b Jan, Feb, ..., Dec (en_US);Jan, Feb, ..., Dez (de_DE)%B January, February, ..., December (en_US);Januar, Februar, ..., Dezember (de_DE)%cLocales appropriate date and time representation.Tue Aug 16 21:30:00 1988 (en_US);Di 16 Aug 21:30:00 1988 (de_DE)%dday as a zero-padded decimal number.01, 02, ..., 31%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12%jDay of the year as a zero-padded decimal number.001, 002, ..., 366%mMonth as a zero-padded decimal number.01, 02, ..., 12%MMinute as a zero-padded decimal number.00, 01, ..., 59%pLocales equivalent of either AM or PM.AM, PM (en_US);am, pm (de_DE)%SSecond as a zero-padded decimal number.00, 01, ..., 59

Time format - 2 DirectiveMeaningExample%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, ..., 53%w 0 is Sunday and 6 is Saturday.0, 1, ..., 6%WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.00, 01, ..., 53%xLocales appropriate date representation.08/16/88 (None);08/16/1988 (en_US);16.08.1988 (de_DE)%XLocales appropriate time representation.21:30:00 (en_US);21:30:00 (de_DE)%y as a zero-padded decimal number.00, 01, ..., 99%YYear with century as a decimal number.0001, 0002, ..., 2013, 2014, ..., 9998, 9999%zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).(empty), +0000, -0400, +1030%ZTime zone name (empty string if the object is naive).(empty), UTC, EST, CST%%A literal'%'character.%

datetimeModule

datetime Module class

datetime Module class classDescriptiondatetime.date (Gregorian calendar ) year, month, day datetime.time hour, minute, second, microsecond, and tzinfo datetime.datetime date time year, month, day, hour, minute, second, microsecond, and tzinfo datetime.timedelta date, time, or datetime days, seconds datetime.tzinfo time datetime

5 Datetime.pydatetime

time

date

timedelta

tzinfo

datetime .date class

datetime.date

3 / date@classvariable min : datetime.datemax: datetime.dateResolution : datetime.timedelta

@classmethodtoday() : datetime.datefromtimestamp(timestamp:float) : datetime.datefromordinal(ordinal:int) : datetime.date

@instancemethodweekday() : intisoweekday() : intstrftime(format: str) : strisoformat() : str@instancevariable year : int month : int day : int

Class/instance variable variableDescriptiondate.min1 1 1 date(1,1,1).date.max99991231 date(9999,12,31).date.resolution Days: 1 day, seconds : 0:00:00 date.yearBetween MINYEAR and MAXYEAR inclusive.date.monthBetween 1 and 12 inclusive.date.day Between 1 and the number of days in the given month of the given year.

today MethodDescriptiondate.today() date , date.fromtimestamp(time.time())

MethodDescriptiondate.weekday()(0) (6) date.isoweekday()(1) (7) date.replace(year,month,day) date date.timetuple() time.struct_time(time.localtime())

MethodDescriptiondate.toordinal()1 1 1 date date.fromordinal(ordinal) 1 1 1 ordinal

Format MethodDescriptiondate.strftime(format) format %y %m %d %Y %m %d , YYYY[] yy[] date.isoformat()ISO 8601 format, YYYY-MM-DD. ,date(2002,12,4).isoformat()=='2002-12-04'.

Timestamp variableDescriptiondate.fromtimestamp(timestamp)POSIX timestamp date time.time() posix timestamp date

Operator(+,-) datatime.date - datetime.date =timedeltadatatime.date+timedelta=datatime.date,

Julian odianal

datetime . time class

Datetime.time

time@classvariable min : datetime.timemax: datetime.timeresolution : datetime.timedelta

@instancemethodreplace([hour[,minute[,second[,microsecond[,tzinfo]]]]])isoformat()strftime(format)utcoffset()tdst()tzname()

@instancevariablehour :intmunite : intsecond : int

microsecond : int tzinfo ; dateime.tzinfo

Class/instance variable variableDescriptiontime.mintime(0,0,0,0).time.maxtime(23,59,59,999999)time.resolutiontimedelta(microseconds=1)time.hourrange(24).time.minuterange(60)

time.secondrange(60)time.microsecondrange(1000000).

time.tzinfodatetime.tzinfo

instance method MethodDescriptiontime.replace([hour[,minute[,second[,microsecond[,tzinfo]]]]]) datetime datetime time.isoformat()HH:MM:SS.mmmmmm, if self.microsecond is 0, HH:MM:SStime.strftime(format) format time.utcoffset()If tzinfo is None, returns None, else returns self.tzinfo.utcoffset(None), and raises an exception if the latter doesnt return None or a timedelta object representing a whole number of minutes with magnitude less than one day.time.dst()If tzinfo is None, returns None, else returns self.tzinfo.dst(None), and raises an exception if the latter doesnt return None, or a timedelta object representing a whole number of minutes with magnitude less than one day.time.tzname()If tzinfo is None, returns None, else returns self.tzinfo.tzname(None), or raises an exception if the latter doesnt return None or a string object.

instance method Time tzinfo subclass

datetime . datetime class

datetime.datetime

datetime@classvariable min : datetime.datemax: datetime.dateresolution : datetime.timedelta

@classmethodtoday() : datetime.datetimenow(tz) : datetime.datetimeutcnow() : datetime.datetimefromtimestamp(timestamp[,tz]) : datetime.datetimefromordinal(ordinal) : datetime.datetimecombine(date,time) : datetime.datetimestrptime(date_string,format) : datetime.datetime

@instancemethoddate()time() timetz()

strftime(format: str) : strisoformat() : strastimezone(tz:str) : tzinfo

@instancevariable year : int month : int day : int hour :int

munite : intsecond : intmicrosecond : int tzinfo ; dateime.tzinfo

Class variable variableDescriptiondatetime.min1 1 1 date(1,1,1).datetime.max99991231 date(9999,12,31).datetime.resolution Days: 1 day, seconds : 0:00:00

instance variable variableDescriptiondatetime.yearBetween MINYEAR and MAXYEAR inclusive.datetime.monthBetween 1 and 12 inclusive.datetime.day Between 1 and the number of days in the given month of the given year.datetime.hourrange(24).datetime.minuterange(60)

datetime.secondrange(60)datetime.microsecondrange(1000000).

datetime.tzinfodatetime.tzinfo

Now/today MethodDescriptiondatetime.today() date time datetime.datetime(,,,,,,)datetime.now([tz]) date time . (tzinfo) datetime.today()

datetime.utcnow()UTC date time

Julian date MethodDescriptiondatetime.fromordinal(ordinal)1 1 1 ordinal() datetime.datetime , time datetime.toordinal(ordinal)

Datetime MethodDescriptiondatetime.fromtimestamp(timestamp[,tz])Posix timestampe(time.time()) datetime.datetime datetime.combine(date,time)datetime.date datetime.time datetime.datetime

instance method MethodDescriptiondatetime.date()datetime date datetime.time()datetime time datetime.timetz() datetime time time zone datetime.time() datetime.replace([year[,month[,day[,hour[,minute[,second[,microsecond[,tzinfo]]]]]]]]) datetime datetime datetime.astimezone(tz) . >>> now.astimezone(date_example.MyUTCOffsetTimezone())datetime.utcoffset() utc . Timedelta datetime.dst() datetime.tzname()datetime.timetuple()

Formatting

datetime format datetime format

Formatting MethodDescriptiondatetime.strftime(format) format datetime.strptime(date_string,format) datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") datetime.datetime(2006, 11, 21, 16, 30)

UTC/GMT/EST

UTC/GMT/EST datetime.datetime UTC/GMT/EST

datetime . timedeltaclass

Timedelta class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)timedelta . 1 : datetime.timedelta(weeks=1) 1 : datetime.timedelta(days=1) 1 : datetime.timedelta(hours=1) 1 : datetime.timedelta(minutes=1) 1 : datetime.timedelta(seconds=1) 1 : datetime.timedelta(milliseconds=1) 1 : datetime.timedelta(microseconds=1)timedelta 5 30 datetime.timedelta(hours=5, minutes=30) .

Timedelta instance days, seconds,microseconds read-only

AttributeValuedaysBetween -999999999 and 999999999 inclusivesecondsBetween 0 and 86399 inclusivemicrosecondsBetween 0 and 999999 inclusive

Timedelta total_seconds .

Timedelta: datatime() + timedelta(days=1) = datatime()

timedelta : , datatime datatime = timedelta

Utcoffset() timedelta UTC timezone timedelta

datetime . tzinfoclass

datetime.tzinfo

datetime.tzinfo time zone datetime.tzinfo

@classvariable zone : strtzname:str

@classmethodfromutc(dt)

@instancemethoddst(self, dt)fromutc(self,dt)localize(self,dt) normalize(self,dt) utcoffset(self,dt)

tzinfo tzinfo tzinfo.utcoffset(self,dt)tzinfo.dst(self,dt)tzinfo.tzname(self,dt)tzinfo.fromutc(self,dt)

tzinfo : Subclass Tzinfo