datetime --- åºæ¬çãªæ¥ä»ãšæéã®å¶
ãœãŒã¹ã³ãŒã: Lib/datetime.py
datetime ã¢ãžã¥ãŒã«ã¯ãæ¥ä»ãæå»ãæäœããããã®ã¯ã©ã¹ãæäŸããŠããŸãã
æ¥ä»ãæå»ã«å¯Ÿããç®è¡ããµããŒããããŠããäžæ¹ãå®è£ ã§ã¯åºåã®ãã©ãŒããããæäœã®ããã®å¹ççãªå±æ§ã®æœåºã«éç¹ã眮ããŠããŸãã
Tip
æžåŒã³ãŒã ã«é£ã¶ã
åè
calendarã¢ãžã¥ãŒã«æ±çšã®ã«ã¬ã³ããŒé¢é£é¢æ°ã
timeã¢ãžã¥ãŒã«æå»ãžã®ã¢ã¯ã»ã¹ãšå€æã
zoneinfoã¢ãžã¥ãŒã«IANAã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ã衚ãå ·äœçãªã¿ã€ã ãŸãŒã³ã
- dateutil ããã±ãŒãž
æ¡åŒµã¿ã€ã ãŸãŒã³ãšæ§æè§£æãµããŒãã®ãããµãŒãããŒãã£ãŒã©ã€ãã©ãªã
- DateType ããã±ãŒãž
Third-party library that introduces distinct static types to for example, allow static type checkers to differentiate between naive and aware datetimes.
Aware and naive objects¶
æ¥æã®ãªããžã§ã¯ãã¯ãããããã¿ã€ã ãŸãŒã³ã®æ å ±ãå«ãã§ãããã©ããã«ãã£ãŠ "aware" ããã㯠"naive" ã«åé¡ãããŸãã
ã¿ã€ã ãŸãŒã³ã倿éã®æ å ±ã®ãããªãã¢ã«ãŽãªãºã çã§æ¿æ²»çãªé©çšå¯èœãªæé調ç¯ã«é¢ããç¥èãæã£ãŠããããã aware ãªããžã§ã¯ãã¯ä»ã® aware ãªããžã§ã¯ããšã®çžå¯Ÿé¢ä¿ãç¹å®ã§ããŸãã aware ãªããžã§ã¯ãã¯è§£éã®äœå°ã®ãªãç¹å®ã®å®æå»ã衚çŸããŸãã [1]
naive ãªããžã§ã¯ãã«ã¯ä»ã®æ¥ä»æå»ãªããžã§ã¯ããšã®çžå¯Ÿé¢ä¿ãææ¡ããã®ã«è¶³ãæ å ±ãå«ãŸããŸãããããããã°ã©ã å ã®æ°åãã¡ãŒãã«ã衚ãããŠããã®ãããã€ã«ãªã®ãããããšã質éãªã®ããããã°ã©ã ã«ãã£ãŠç°ãªãããã«ãnaive ãªããžã§ã¯ããåå®äžçæ (UTC) ãªã®ããçŸå°æéãªã®ãããããšãä»ã®ã¿ã€ã ãŸãŒã³ãªã®ãã¯ãã®ããã°ã©ã ã«äŸåããŸããNaive ãªããžã§ã¯ãã¯ããã€ãã®çŸå®çãªåŽé¢ãç¡èŠããŠããŸããšããã³ã¹ããç¡èŠããã°ãç°¡åã«çè§£ã§ããããŸãå©çšããããšãã§ããŸãã
For applications requiring aware objects, datetime and time
objects have an optional time zone information attribute, tzinfo, that
can be set to an instance of a subclass of the abstract tzinfo class.
These tzinfo objects capture information about the offset from UTC
time, the time zone name, and whether daylight saving time is in effect.
ãã äžã€ã®å
·è±¡ tzinfo ã¯ã©ã¹ã§ãã timezone ã¯ã©ã¹ã datetime ã¢ãžã¥ãŒã«ã§æäŸãããŠããŸãã timezone ã¯ã©ã¹ã¯ãUTCããã®ãªãã»ãããåºå®ã§ããåçŽãªã¿ã€ã ãŸãŒã³ïŒäŸãã°UTCããèªäœïŒãããã³åã¢ã¡ãªã«ã«ãããæ±éšæšæºæïŒESTïŒïŒæ±éšå€æéïŒEDTïŒã®ãããªåçŽã§ã¯ãªãã¿ã€ã ãŸãŒã³ã®äž¡æ¹ã衚çŸã§ããŸããããæ·±ã詳现ãŸã§ã¿ã€ã ãŸãŒã³ããµããŒããããã¯ã¢ããªã±ãŒã·ã§ã³ã«äŸåããŸããäžçäžã®æå»ã®èª¿æŽã決ããã«ãŒã«ã¯åççãšãããããã¯æ¿æ²»çãªãã®ã§ãé »ç¹ã«å€ãããUTC ãé€ããšéœåã®ããåºæºãšãããã®ã¯ãããŸããã
宿°Â¶
datetime ã¢ãžã¥ãŒã«ã§ã¯ä»¥äžã®å®æ°ãå
¬éããŠããŸã:
- datetime.MINYEAR¶
dateãdatetimeãªããžã§ã¯ãã§èš±ãããŠããã幎ã衚çŸããæå°ã®æ°åã§ããMINYEARã¯1ã§ãã
- datetime.MAXYEAR¶
dateãdatetimeãªããžã§ã¯ãã§èš±ãããŠããã幎ã衚çŸããæå€§ã®æ°åã§ããMAXYEARã¯9999ã§ãã
- datetime.UTC¶
UTCã¿ã€ã ãŸãŒã³ã·ã³ã°ã«ãã³
datetime.timezone.utcã®å¥åãAdded in version 3.11.
Available types¶
- class datetime.date
çæ³ç㪠naive ãªæ¥ä»ã§ããããŸã§ããããããçŸåšã®ã°ã¬ãŽãªãªæŠ (Gregorian calender) ãæå¹ã§ããããšãä»®å®ããŠããŸãã 屿§ã¯
year,month,ããã³dayã§ãã
- class datetime.time
çæ³çãªæå»ã§ãç¹å®ã®æ¥ããç¬ç«ããŠãããæ¯æ¥ãå³å¯ã« 24*60*60 ç§ã§ãããšä»®å®ããŠããŸãã("ãããç§: leap seconds" ã®æŠå¿µã¯ãããŸããã) 屿§ã¯
hour,minute,second,microsecond, ããã³tzinfoã§ãã
- class datetime.datetime
æ¥ä»ãšæå»ãçµã¿åããããã®ã§ãã 屿§ã¯
year,month,day,hour,minute,second,microsecond, ããã³tzinfoã§ãã
- class datetime.timedelta
datetimeãããã¯dateã¯ã©ã¹ã®äºã€ã®ã€ã³ã¹ã¿ã³ã¹éã®æéå·®ããã€ã¯ãç§ç²ŸåºŠã§è¡šãçµéæéå€ã§ãã
- class datetime.tzinfo
ã¿ã€ã ãŸãŒã³æ å ±ãªããžã§ã¯ãã®æœè±¡åºåºã¯ã©ã¹ã§ãã
datetimeããã³timeã¯ã©ã¹ã§çšããããã«ã¹ã¿ãã€ãºå¯èœãªæå»ä¿®æ£ã®æŠå¿µ (ããšãã°ã¿ã€ã ãŸãŒã³ã倿éã®èšç®) ãæäŸããŸãã
- class datetime.timezone
tzinfoæœè±¡åºåºã¯ã©ã¹ã UTC ããã®åºå®ãªãã»ãããšããŠå®è£ ããã¯ã©ã¹ã§ããAdded in version 3.2.
ãããã®åã®ãªããžã§ã¯ãã¯å€æŽäžå¯èœ (immutable) ã§ãã
Subclass relationships:
Common properties¶
date åãdatetime åãtime åãtimezone åã«ã¯å
±éããç¹åŸŽããããŸã:
ãããã®åã®ãªããžã§ã¯ãã¯å€æŽäžå¯èœ (immutable) ã§ãã
ãããã®åã®ãªããžã§ã¯ã㯠ããã·ã¥å¯èœ ã§ãããèŸæžã®ããŒãšããŠäœ¿ããããšã«ãªããŸãã
ãããã®åã®ãªããžã§ã¯ãã¯
pickleã¢ãžã¥ãŒã«ãå©çšããŠå¹çç㪠pickle åããµããŒãããŠããŸãã
Determining if an object is aware or naive¶
date åã®ãªããžã§ã¯ãã¯åžžã« naive ã§ãã
time åããã㯠datetime åã®ãªããžã§ã¯ã㯠aware ã naive ã®ã©ã¡ããã§ãã
æ¬¡ã®æ¡ä»¶ãäž¡æ¹ãšãæºããå Žåã datetime ãªããžã§ã¯ã d 㯠aware ã§ã:
d.tzinfoãNoneã§ãªãd.tzinfo.utcoffset(d)ãNoneãè¿ããªã
ã©ã¡ãããæºãããªãå Žåã¯ã d 㯠naive ã§ãã
æ¬¡ã®æ¡ä»¶ãäž¡æ¹ãšãæºããå Žåã time ãªããžã§ã¯ã t 㯠aware ã§ã:
t.tzinfoãNoneã§ãªãt.tzinfo.utcoffset(None)ãNoneãè¿ããªã
ã©ã¡ãããæºãããªãå Žåã¯ã t 㯠naive ã§ãã
aware ãªãªããžã§ã¯ããš naive ãªãªããžã§ã¯ãã®åºå¥ã¯ timedelta ãªããžã§ã¯ãã«ã¯ããŠã¯ãŸããŸããã
timedelta objects¶
timedelta ãªããžã§ã¯ãã¯çµéæéãããªãã¡äºã€ã® datetime ãŸã㯠date ã®ã€ã³ã¹ã¿ã³ã¹ã®å·®ã衚ããŸãã
- class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)¶
å šãŠã®åŒæ°ããªãã·ã§ã³ã§ãããã©ã«ãå€ã¯0ã§ãã åŒæ°ã¯æŽæ°ãæµ®åå°æ°ç¹æ°ã§ããããæ£ã§ãè² ã§ãããŸããŸããã
days, seconds, microseconds ã ããå éšçã«ä¿æãããŸãã åŒæ°ã¯ä»¥äžã®ããã«ããŠå€æãããŸã:
1 ããªç§ã¯ 1000 ãã€ã¯ãç§ã«å€æãããŸãã
1 å㯠60 ç§ã«å€æãããŸãã
1 æé㯠3600 ç§ã«å€æãããŸãã
1 é±é㯠7 æ¥ã«å€æãããŸãã
ããã«ãå€ãäžæã«è¡šãããããã« days, seconds, microseconds ã以äžã®ããã«æ£èŠåãããŸã
0 <= microseconds < 10000000 <= seconds < 3600*24(äžæ¥äžã®ç§æ°)-999999999 <= days <= 999999999
次ã®äŸã¯ã days, seconds, microseconds ã«å ããŠä»»æã®åŒæ°ãã©ã "éçŽ" ãããæçµçã«3ã€ã®å±æ§ã«æ£èŠåããããã®èª¬æãããŠããŸã:
>>> import datetime as dt >>> delta = dt.timedelta( ... days=50, ... seconds=27, ... microseconds=10, ... milliseconds=29000, ... minutes=5, ... hours=8, ... weeks=2 ... ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)
Tip
import datetime as dtinstead ofimport datetimeorfrom datetime import datetimeto avoid confusion between the module and the class. See How I Import Pythonâs datetime Module.åŒæ°ã®ãããããæµ®åå°æ°ç¹ã§ãããå°æ°ã®ãã€ã¯ãç§ãååšããå Žåãå°æ°ã®ãã€ã¯ãç§ã¯å šãŠã®åŒæ°ããäžåºŠåã眮ããããããã®åã¯æè¿æ¥å¶æ°ã®ãã€ã¯ãç§ã«äžžããããŸããæµ®åå°æ°ç¹ã®åŒæ°ããªãå Žåãå€ã®å€æãšæ£èŠåã®éçšã¯å³å¯ãª (倱ãããæ å ±ããªã) ãã®ãšãªããŸãã
æ¥ã®å€ãæ£èŠåããçµæãæå®ãããç¯å²ã®å€åŽã«ãªã£ãå Žåã«ã¯ã
OverflowErrorãéåºãããŸããè² ã®å€ãæ£èŠåãããšãæåã¯æ··ä¹±ãããããªå€ã«ãªããŸããäŸãã°:
>>> import datetime as dt >>> d = dt.timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)
Since the string representation of
timedeltaobjects can be confusing, use the following recipe to produce a more readable format:>>> def pretty_timedelta(td): ... if td.days >= 0: ... return str(td) ... return f'-({-td!s})' ... >>> d = timedelta(hours=-1) >>> str(d) # not human-friendly '-1 day, 23:00:00' >>> pretty_timedelta(d) '-(1:00:00)'
以äžã«ã¯ã©ã¹å±æ§ã瀺ããŸã:
- timedelta.min¶
æå°ã®å€ã衚ã
timedeltaãªããžã§ã¯ãã§ãtimedelta(-999999999)ã§ãã
- timedelta.max¶
æå€§ã®å€ã衚ã
timedeltaãªããžã§ã¯ãã§ãtimedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)ã§ãã
- timedelta.resolution¶
timedeltaãªããžã§ã¯ããçãããªããªãæå°ã®æéå·®ã§ãtimedelta(microseconds=1)ã§ãã
æ£èŠåã®ããã«ã timedelta.max 㯠-timedelta.min ãã倧ããããšã«æ³šæããŠãã ããã -timedelta.max 㯠timedelta ãªããžã§ã¯ããšããŠè¡šçŸããããšãã§ããŸããã
ã€ã³ã¹ã¿ã³ã¹ã®å±æ§ (èªã¿åºãã®ã¿):
- timedelta.days¶
䞡端å€ãå«ã-999,999,999 ãã 999,999,999 ã®é
- timedelta.seconds¶
䞡端å€ãå«ã 0 ãã 86,399 ã®é
泚æ
It is a somewhat common bug for code to unintentionally use this attribute when it is actually intended to get a
total_seconds()value instead:>>> import datetime as dt >>> duration = dt.timedelta(seconds=11235813) >>> duration.days, duration.seconds (130, 3813) >>> duration.total_seconds() 11235813.0
- timedelta.microseconds¶
䞡端å€ãå«ã 0 ãã 999,999 ã®é
ãµããŒããããŠããæŒç®ã以äžã«ç€ºããŸã:
æŒç® |
çµæ |
|---|---|
|
|
|
|
|
æéå·®ãšæŽæ°ã®ç©ãæŒç®åŸã |
äžè¬çã«ã |
|
|
æéå·®ãšæµ®åå°æ°ç¹ã®ç©ãçµæã¯æè¿æ¥å¶æ°ãžã®äžžããå©çšããŠæãè¿ã timedelta.resolution ã®åæ°ã«äžžããããŸãã |
|
|
|
æéå·®ãæµ®åå°æ°ç¹ãæŽæ°ã§é€ãããã®ãçµæã¯æè¿æ¥å¶æ°ãžã®äžžããå©çšããŠæãè¿ã timedelta.resolution ã®åæ°ã«äžžããããŸãã |
|
floor ãèšç®ãããäœã㯠(ããããã°) æšãŠãããŸããåŸè ã®å ŽåãæŽæ°ãè¿ãããŸãã(3) |
|
å°äœã |
|
åãšå°äœãèšç®ãããŸã: |
|
åãå€ãæã€ |
|
|
|
|
|
|
|
|
泚é:
ãã®æŒç®ã¯æ£ç¢ºã§ããããªãŒããããŒãããããããŸããã
ãã®æŒç®ã¯æ£ç¢ºã§ããããªãŒããããŒãåŸãŸããã
0 ã«ããé€ç®ã¯
ZeroDivisionErrorãéåºããŸãã-timedelta.maxã¯timedeltaãªããžã§ã¯ãã§è¡šçŸããããšãã§ããŸãããtimedeltaãªããžã§ã¯ãã®æåå衚çŸã¯å éšè¡šçŸã«é¡äŒŒããåœ¢ã«æ£èŠåãããŸãããã®ããè² ã® timedelta ã¯å°ãå€ãªçµæã«ãªããŸããäŸãã°:>>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:00
t3 ã
timedelta.maxã®ãšããé€ãã°ãåŒt2 - t3ã¯åžžã«ãåŒt2 + (-t3)ãšåçã§ããt3 ãtimedelta.maxã®å Žåãåè ã®åŒã¯çµæã®å€ãåºãŸãããåŸè ã¯ãªãŒããŒãããŒãèµ·ãããŸãã
äžã«åæããæäœã«å ã timedelta ãªããžã§ã¯ã㯠date ããã³ datetime ãªããžã§ã¯ããšã®éã§å æžç®ããµããŒãããŠããŸã (äžãåç
§ããŠãã ãã)ã
ããŒãžã§ã³ 3.2 ã§å€æŽ: Floor division and true division of a timedelta object by another
timedelta object are now supported, as are remainder operations and
the divmod() function. True division and multiplication of a
timedelta object by a float object are now supported.
timedelta ãªããžã§ã¯ãã¯ç䟡æ§ãšé åºã®æ¯èŒããµããŒãããŸãã
ããŒã«æŒç®ã³ã³ããã¹ãã§ã¯ã timedelta ãªããžã§ã¯ã㯠timedelta(0) ã«çãããªãå Žåãã€ãã®ãšãã«éãçãšãªããŸãã
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã:
- timedelta.total_seconds()¶
Return the total number of seconds contained in the duration. Equivalent to
td / timedelta(seconds=1). For interval units other than seconds, use the division form directly (for example,td / timedelta(microseconds=1)).éåžžã«é·ãæé (å€ãã®ãã©ãããã©ãŒã ã§ã¯270幎以äž) ã«ã€ããŠã¯ããã®ã¡ãœããã¯ãã€ã¯ãç§ã®ç²ŸåºŠã倱ãããšãããããšã«æ³šæããŠãã ããã
Added in version 3.2.
Examples of usage: timedelta¶
æ£èŠåã®è¿œå ã®äŸã§ã:
>>> # Components of another_year add up to exactly 365 days
>>> import datetime as dt
>>> year = dt.timedelta(days=365)
>>> another_year = dt.timedelta(weeks=40, days=84, hours=23,
... minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
timedelta ã®èšç®ã®äŸã§ã:
>>> import datetime as dt
>>> year = dt.timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
date objects¶
date ãªããžã§ã¯ãã¯ãäž¡æ¹åã«ç¡æéã«æ¡åŒµãããçŸåšã®ã°ã¬ãŽãªãªæŠãšããçæ³åãããæŠã®æ¥ä» (å¹Žææ¥) ã衚ããŸãã
1 幎 1 æ 1 æ¥ã¯æ¥çªå· 1ã1 幎 1 æ 2 æ¥ã¯æ¥çªå· 2 ãšåŒã°ããä»ãåæ§ã§ãã [2]
- class datetime.date(year, month, day)¶
å šãŠã®åŒæ°ãå¿ é ã§ãã åŒæ°ã¯æŽæ°ã§ã次ã®ç¯å²ã«åãŸã£ãŠããªããã°ãªããŸãã:
MINYEAR <= year <= MAXYEAR1 <= month <= 121 <= day <= æå®ãããæãšå¹Žã«ãããæ¥æ°
ç¯å²ãè¶ ããåŒæ°ãäžããå Žåã
ValueErrorãéåºãããŸãã
ä»ã®ã³ã³ã¹ãã©ã¯ã¿ãããã³å šãŠã®ã¯ã©ã¹ã¡ãœããã以äžã«ç€ºããŸã:
- classmethod date.today()¶
çŸåšã®ããŒã«ã«ãªæ¥ä»ãè¿ããŸãã
date.fromtimestamp(time.time())ãšç䟡ã§ãã
- classmethod date.fromtimestamp(timestamp)¶
Return the local date corresponding to the POSIX timestamp, such as is returned by
time.time().timestamp ããã©ãããã©ãŒã ã® C 颿°
localtime()ããµããŒãããå€ã®ç¯å²ããå€ããŠããå ŽåãOverflowErrorãéåºãããããããŸããããŸãlocaltime()åŒã³åºãã倱æããå Žåã«ã¯OSErrorãéåºãããããããŸããããã®ç¯å²ã¯é垞㯠1970 幎ãã 2038 幎ãŸã§ã«å¶éãããŠããŸããã¿ã€ã ã¹ã¿ã³ãã®è¡šèšã«ãããç§ãå«ããé POSIX ãªã·ã¹ãã ã§ã¯ããããç§ã¯fromtimestamp()ã§ã¯ç¡èŠãããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: timestamp ããã©ãããã©ãŒã ã® C 颿°
localtime()ã®ãµããŒãããå€ã®ç¯å²ããå€ããŠããå ŽåãValueErrorã§ã¯ãªãOverflowErrorãéåºããããã«ãªããŸãããlocaltime()ã®åŒã³åºã倱æã§ValueErrorã§ã¯ãªãOSErrorãéåºããããã«ãªããŸãããããŒãžã§ã³ 3.15 ã§å€æŽ: Accepts any real number as timestamp, not only integer or float.
- classmethod date.fromordinal(ordinal)¶
Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1.
1 <= ordinal <= date.max.toordinal()ã§ãªãå ŽåãValueErrorãéåºãããŸãã ä»»æã®æ¥ä»dã«å¯Ÿããdate.fromordinal(d.toordinal()) == dãšãªããŸãã
- classmethod date.fromisoformat(date_string)¶
以äžã®äŸå€ãé€ããæå¹ãª ISO 8601 ãã©ãŒãããã§äžãããã date_string ã«å¯Ÿå¿ãã
dateãè¿ããŸã :粟床ã®äœãæ¥ä»ã¯çŸåšãµããŒããããŠããŸãã(
YYYY-MM,YYYY)ãæ¡åŒµãããæ¥ä»è¡šçŸã¯çŸåšãµããŒããããŠããŸãã(
±YYYYYY-MM-DD)ãåºæ°ã®æ¥ä»ã¯çŸåšãµããŒããããŠããŸãã(
YYYY-OOO)ã
äŸ:
>>> import datetime as dt >>> dt.date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> dt.date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> dt.date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)
Added in version 3.7.
ããŒãžã§ã³ 3.11 ã§å€æŽ: 以åã¯ãã®ã¡ãœããã¯
YYYY-MM-DDãã©ãŒãããã®ã¿ããµããŒãããŠããŸããã
- classmethod date.fromisocalendar(year, week, day)¶
Return a
datecorresponding to the ISO calendar date specified by year, week and day. This is the inverse of the functiondate.isocalendar().Added in version 3.8.
- classmethod date.strptime(date_string, format)¶
Return a
datecorresponding to date_string, parsed according to format. This is equivalent to:date(*(time.strptime(date_string, format)[0:3]))
date_string ãš format ã
time.strptime()ã§æ§æè§£æã§ããªãå Žåãããã®é¢æ°ãæå»ã¿ãã«ãè¿ããŠããªãå Žåã«ã¯ValueErrorãéåºããŸããstrftime() and strptime() behavior ããã³date.fromisoformat()ãåç §ããŠãã ãããæ³šé
If format specifies a day of month (
%d) without a year,ValueErroris raised. This is to avoid a quadrennial leap year bug in code seeking to parse only a month and day as the default year used in absence of one in the format is not a leap year. The workaround is to always include a year in your format. If parsing date_string values that do not have a year, explicitly add a year that is a leap year before parsing:>>> import datetime as dt >>> date_string = "02/29" >>> when = dt.date.strptime(f"{date_string};1984", "%m/%d;%Y") # Avoids leap year bug. >>> when.strftime("%B %d") 'February 29'
Added in version 3.14.
以äžã«ã¯ã©ã¹å±æ§ã瀺ããŸã:
- date.min¶
衚çŸã§ããæãå€ãæ¥ä»ã§ã
date(MINYEAR, 1, 1)ã§ãã
- date.max¶
衚çŸã§ããæãæ°ããæ¥ä»ã§ã
date(MAXYEAR, 12, 31)ã§ãã
- date.resolution¶
çãããªãæ¥ä»ãªããžã§ã¯ãéã®æå°ã®å·®ã§ã
timedelta(days=1)ã§ãã
ã€ã³ã¹ã¿ã³ã¹ã®å±æ§ (èªã¿åºãã®ã¿):
- date.month¶
䞡端å€ãå«ã 1 ãã 12 ãŸã§ã®å€ã§ãã
- date.day¶
1 ããäžããããæãšå¹Žã«ãããæ¥æ°ãŸã§ã®å€ã§ãã
ãµããŒããããŠããæŒç®ã以äžã«ç€ºããŸã:
æŒç® |
çµæ |
|---|---|
|
|
|
|
|
(3) |
date1 == date2date1 != date2 |
ç䟡æ§ã®æ¯èŒã(4) |
date1 < date2date1 > date2date1 <= date2date1 >= date2 |
é åºã®æ¯èŒã(5) |
泚é:
date2 ã¯ã
timedelta.days > 0ã®å Žåã¯é²ãæ¹åã«ãtimedelta.days < 0ã®å Žåã¯æ»ãæ¹åã«ç§»åããŸãã æŒç®åŸã¯date2 - date1 == timedelta.daysãæç«ããŸããtimedelta.secondsããã³timedelta.microsecondsã¯ç¡èŠãããŸããdate2.yearãMINYEARã«ãªã£ãŠããŸã£ãããMAXYEARãã倧ãããªã£ãŠããŸãå Žåã«ã¯OverflowErrorãéåºãããŸããtimedelta.secondsãštimedelta.microsecondsã¯ç¡èŠãããŸãããã®æŒç®ã¯å³å¯ã§ããªãŒããããŒããŸããã
timedelta.secondsããã³timedelta.microseconds㯠0 ã§ãæŒç®åŸã«ã¯date2 + timedelta == date1ãšãªããŸããåãæ¥ã衚ã
dateãªããžã§ã¯ãã¯çããã§ããdatetimeã®ã€ã³ã¹ã¿ã³ã¹ã§ã¯ãªãdateãªããžã§ã¯ãã¯ãåãæ¥ã衚ããŠããŠããdatetimeãªããžã§ã¯ããšã¯æ±ºããŠç䟡ã«ã¯ãªããŸãããdate1 is considered less than date2 when date1 precedes date2 in time. In other words,
date1 < date2if and only ifdate1.toordinal() < date2.toordinal().Order comparison between a
dateobject that is not also adatetimeinstance and adatetimeobject raisesTypeError.
ããŒãžã§ã³ 3.13 ã§å€æŽ: Comparison between datetime object and an instance of
the date subclass that is not a datetime subclass
no longer converts the latter to date, ignoring the time part
and the time zone.
The default behavior can be changed by overriding the special comparison
methods in subclasses.
ããŒã«æŒç®ã³ã³ããã¹ãã§ã¯ãå
šãŠã® time ãªããžã§ã¯ãã¯çãšã¿ãªãããŸãã
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã:
- date.replace(year=self.year, month=self.month, day=self.day)¶
Return a new
dateobject with the same values, but with specified parameters updated.以äžã¯ããã°ã©ã äŸã§ã:
>>> import datetime as dt >>> d = dt.date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)
The generic function
copy.replace()also supportsdateobjects.
- date.timetuple()¶
time.localtime()ãè¿ããããªtime.struct_timeãè¿ããŸããæåç§ã 0 ã§ã DST ãã©ã°ã -1 ã§ãã
d.timetuple()ã¯æ¬¡ã®åŒãšç䟡ã§ã:time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))
ããã§ã
yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1ã¯æ¬å¹Žã® 1 æ 1 æ¥ã 1 ãšãããšãã®æ¥ä»çªå·ã§ãã
- date.toordinal()¶
å çºã°ã¬ãŽãªãªæŠã«ãããæ¥ä»åºæ°ãè¿ããŸãã 1 幎㮠1 æ 1 æ¥ãåºæ° 1 ãšãªããŸããä»»æã®
dateãªããžã§ã¯ãdã«ã€ããŠãdate.fromordinal(d.toordinal()) == dãšãªããŸãã
- date.weekday()¶
æææ¥ã 0ãæ¥ææ¥ã 6 ãšããŠãææ¥ãæŽæ°ã§è¿ããŸããäŸãã°ã
date(2002, 12, 4).weekday() == 2ã§ãããæ°Žææ¥ã瀺ããŸããisoweekday()ãåç §ããŠãã ããã
- date.isoweekday()¶
æææ¥ã 1,æ¥ææ¥ã 7 ãšããŠãææ¥ãæŽæ°ã§è¿ããŸããäŸãã°ã
date(2002, 12, 4).isoweekday() == 3ã§ãããæ°Žææ¥ã瀺ããŸããweekday(),isocalendar()ãåç §ããŠãã ããã
- date.isocalendar()¶
yearãweekãweekdayã®3ã€ã§æ§æããã named tuple ãè¿ããŸããISO æŠã¯ã°ã¬ãŽãªãªæŠã®å€çš®ãšããŠåºãçšããããŠããŸãã [3]
ISO 幎ã¯å®å šãªé±ã 52 é±ãŸã㯠53 é±ãããé±ã¯ææããå§ãŸã£ãŠæ¥æã«çµãããŸããISO 幎ã§ã®ãã幎ã«ãããæåã®é±ã¯ããã®å¹Žã®æšææ¥ãå«ãæåã® (ã°ã¬ãŽãªãªæŠã§ã®) é±ãšãªããŸãããã®é±ã¯é±çªå· 1 ãšåŒã°ãããã®æšææ¥ã§ã® ISO 幎ã¯ã°ã¬ãŽãªãªæŠã«ããã幎ãšçãããªããŸãã
äŸãã°ã2004 å¹Žã¯æšææ¥ããå§ãŸããããISO å¹Žã®æåã®é±ã¯ 2003 幎 12 æ 29 æ¥ãæææ¥ããå§ãŸãã2004 幎 1 æ 4 æ¥ãæ¥ææ¥ã«çµãããŸã
>>> import datetime as dt >>> dt.date(2003, 12, 29).isocalendar() datetime.IsoCalendarDate(year=2004, week=1, weekday=1) >>> dt.date(2004, 1, 4).isocalendar() datetime.IsoCalendarDate(year=2004, week=1, weekday=7)
ããŒãžã§ã³ 3.9 ã§å€æŽ: çµæã ã¿ãã« ãã named tuple ãžå€æŽãããŸããã
- date.isoformat()¶
æ¥ä»ã ISO 8601 æžåŒã®
YYYY-MM-DDã§è¡šããæååãè¿ããŸã:>>> import datetime as dt >>> dt.date(2002, 12, 4).isoformat() '2002-12-04'
- date.__str__()¶
dateãªããžã§ã¯ãdã«ãããŠãstr(d)ã¯d.isoformat()ãšç䟡ã§ãã
- date.ctime()¶
æ¥ä»ã衚ãæååãè¿ããŸã:
>>> import datetime as dt >>> dt.date(2002, 12, 4).ctime() 'Wed Dec 4 00:00:00 2002'
d.ctime()ã¯æ¬¡ã®åŒãšç䟡ã§ã:time.ctime(time.mktime(d.timetuple()))
ãããç䟡ã«ãªãã®ã¯ã (
time.ctime()ã«åŒã³åºãããdate.ctime()ã«åŒã³åºãããªã) ãã€ãã£ãã® C 颿°ctime()ã C æšæºã«æºæ ããŠãããã©ãããã©ãŒã äžã§ã§ãã
- date.strftime(format)¶
æç€ºçãªæžåŒæååã§å¶åŸ¡ããããæ¥ä»ã衚çŸããæååãè¿ããŸãã æéãåãç§ãè¡šãæžåŒã³ãŒãã¯å€ 0 ã«ãªããŸãã strftime() and strptime() behavior ããã³
date.isoformat()ãåç §ããŠãã ããã
- date.__format__(format)¶
date.strftime()ãšç䟡ã§ãã ããã«ããã ãã©ãŒãããæžã¿æååãªãã©ã« ã®äžãstr.format()ã䜿ã£ãŠãããšãã«dateãªããžã§ã¯ãã®æžåŒæååãæå®ã§ããŸãã strftime() and strptime() behavior ããã³date.isoformat()ãåç §ããŠãã ããã
Examples of usage: date¶
ã€ãã³ããŸã§ã®æ¥æ°ãæ°ããäŸã瀺ããŸã:
>>> import time
>>> import datetime as dt
>>> today = dt.date.today()
>>> today
datetime.date(2007, 12, 5)
>>> today == dt.date.fromtimestamp(time.time())
True
>>> my_birthday = dt.date(today.year, 6, 24)
>>> if my_birthday < today:
... my_birthday = my_birthday.replace(year=today.year + 1)
...
>>> my_birthday
datetime.date(2008, 6, 24)
>>> time_to_birthday = abs(my_birthday - today)
>>> time_to_birthday.days
202
ãããªã date ã䜿ãäŸ:
>>> import datetime as dt
>>> d = dt.date.fromordinal(730920) # 730920th day after 1. 1. 0001
>>> d
datetime.date(2002, 3, 11)
>>> # Methods related to formatting string output
>>> d.isoformat()
'2002-03-11'
>>> d.strftime("%d/%m/%y")
'11/03/02'
>>> d.strftime("%A %d. %B %Y")
'Monday 11. March 2002'
>>> d.ctime()
'Mon Mar 11 00:00:00 2002'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")
'The day is 11, the month is March.'
>>> # Methods for extracting 'components' under different calendars
>>> t = d.timetuple()
>>> for i in t:
... print(i)
2002 # year
3 # month
11 # day
0
0
0
0 # weekday (0 = Monday)
70 # 70th day in the year
-1
>>> ic = d.isocalendar()
>>> for i in ic:
... print(i)
2002 # ISO year
11 # ISO week number
1 # ISO day number ( 1 = Monday )
>>> # A date object is immutable; all operations produce a new object
>>> d.replace(year=2005)
datetime.date(2005, 3, 11)
datetime objects¶
datetime ãªããžã§ã¯ã㯠date ãªããžã§ã¯ãããã³ time ãªããžã§ã¯ãã®å
šãŠã®æ
å ±ãå
¥ã£ãŠããåäžã®ãªããžã§ã¯ãã§ãã
Like a date object, datetime assumes the current Gregorian
calendar extended in both directions; like a time object,
datetime assumes there are exactly 3600*24 seconds in every day.
以äžã«ã³ã³ã¹ãã©ã¯ã¿ã瀺ããŸã:
- class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)¶
year, month, day åŒæ°ã¯å¿ é ã§ãã tzinfo ã¯
NoneãŸãã¯tzinfoãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãã æ®ãã®åŒæ°ã¯æ¬¡ã®ç¯å²ã®æŽæ°ã§ãªããã°ãªããŸãã:MINYEAR <= year <= MAXYEAR,1 <= month <= 12,1 <= day <= æå®ãããæãšå¹Žã«ãããæ¥æ°,0 <= hour < 24,0 <= minute < 60,0 <= second < 60,0 <= microsecond < 1000000,fold in [0, 1].
ç¯å²ãè¶ ããåŒæ°ãäžããå Žåã
ValueErrorãéåºãããŸããããŒãžã§ã³ 3.6 ã§å€æŽ: fold ãã©ã¡ãŒã¿ã远å ãããŸããã
ä»ã®ã³ã³ã¹ãã©ã¯ã¿ãããã³å šãŠã®ã¯ã©ã¹ã¡ãœããã以äžã«ç€ºããŸã:
- classmethod datetime.today()¶
tzinfoãNoneã§ããçŸåšã®ããŒã«ã«ã®æ¥ä»ããã³æå»ãè¿ããŸããæ¬¡ãšç䟡ã§ã:
datetime.fromtimestamp(time.time())
now(),fromtimestamp()ãåç §ããŠãã ããããã®ã¡ãœããã®æ©èœã¯
now()ãšç䟡ã§ãããtzåŒæ°ã¯ãããŸããã
- classmethod datetime.now(tz=None)¶
çŸåšã®ããŒã«ã«ãªæ¥æãè¿ããŸãã
ãªãã·ã§ã³ã®åŒæ° tz ã
Noneã§ãããæå®ãããŠããªãå Žåããã®ã¡ãœããã¯today()ãšåæ§ã§ãããå¯èœãªãã°time.time()ã¿ã€ã ã¹ã¿ã³ããéããŠåŸãããšãã§ãããããé«ãç²ŸåºŠã§æå»ãæäŸããŸã (äŸãã°ããã©ãããã©ãŒã ã C 颿°gettimeofday()ããµããŒãããå Žåã«ã¯å¯èœãªããšããããŸã)ãtz ã
Noneã§ãªãå Žåã tz ã¯tzinfoã®ãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãªããã°ãªãããçŸåšã®æ¥ä»ããã³æå»ã¯ tz ã®ã¿ã€ã ãŸãŒã³ã«å€æãããŸããtoday()ããã³utcnow()ããããã®é¢æ°ãäœ¿ãæ¹ã奜ãŸããã§ããæ³šé
Subsequent calls to
datetime.now()may return the same instant depending on the precision of the underlying clock.
- classmethod datetime.utcnow()¶
tzinfoãNoneã§ããçŸåšã® UTC ã®æ¥ä»ããã³æå»ãè¿ããŸãããã®ã¡ãœããã¯
now()ãšäŒŒãŠããŸããã naive ãªdatetimeãªããžã§ã¯ããšããŠçŸåšã® UTC æ¥ä»ããã³æå»ãè¿ããŸãã aware ãªçŸåšã® UTC datetime ã¯datetime.now(timezone.utc)ãåŒã³åºãããšã§ååŸã§ããŸããnow()ãåç §ããŠãã ãããèŠå
naive ãª
datetimeãªããžã§ã¯ãã¯å€ãã®datetimeã¡ãœããã§ããŒã«ã«ãªæéãšããŠæ±ãããããã aware 㪠datetime ã䜿ã£ãŠ UTC ã®æå»ã衚ãã®ã奜ãŸããã§ãã ãã®ããã UTC ã§ã®çŸåšã®æå»ã衚ããªããžã§ã¯ãã®äœæã§ã¯datetime.now(timezone.utc)ãåŒã³åºãæ¹æ³ãæšå¥šãããŸããããŒãžã§ã³ 3.12 ã§éæšå¥š: Use
datetime.now()withUTCinstead.
- classmethod datetime.fromtimestamp(timestamp, tz=None)¶
time.time()ãè¿ããããªã POSIX ã¿ã€ã ã¹ã¿ã³ãã«å¯Ÿå¿ããããŒã«ã«ãªæ¥ä»ãšæå»ãè¿ããŸãããªãã·ã§ã³ã®åŒæ° tz ãNoneã§ããããæå®ãããŠããªãå Žåãã¿ã€ã ã¹ã¿ã³ãã¯ãã©ãããã©ãŒã ã®ããŒã«ã«ãªæ¥ä»ããã³æå»ã«å€æãããè¿ãããdatetimeãªããžã§ã¯ã㯠naive ãªãã®ã«ãªããŸããtz ã
Noneã§ãªãå Žåã tz ã¯tzinfoã®ãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãªããã°ãªãããã¿ã€ã ã¹ã¿ã³ã㯠tz ã®ã¿ã€ã ãŸãŒã³ã«å€æãããŸããã¿ã€ã ã¹ã¿ã³ãããã©ãããã©ãŒã ã® C 颿°
localtime()ãgmtime()ã§ãµããŒããããŠããç¯å²ãè¶ ããå Žåãfromtimestamp()ã¯OverflowErrorãéåºããããšããããŸãããã®ç¯å²ã¯ãã 1970 幎ãã 2038 幎ã«å¶éãããŠããŸãã ãŸãlocaltime()ãgmtime()ã倱æããéã¯OSErrorãéåºããŸãã ãããç§ãã¿ã€ã ã¹ã¿ã³ãã®æŠå¿µã«å«ãŸããŠããé POSIX ã·ã¹ãã ã§ã¯ãfromtimestamp()ã¯ãããç§ãç¡èŠããŸãã ãã®ãããç§ã®ç°ãªãäºã€ã®ã¿ã€ã ã¹ã¿ã³ããåäžã®datetimeãªããžã§ã¯ããšãªãããšãèµ·ããåŸãŸããutcfromtimestamp()ãããããã®ã¡ãœããã®æ¹ã奜ãŸããã§ããããŒãžã§ã³ 3.3 ã§å€æŽ: timestamp ããã©ãããã©ãŒã ã® C 颿°
localtime()ãããã¯gmtime()ã®ãµããŒãããå€ã®ç¯å²ããå€ããŠããå ŽåãValueErrorã§ã¯ãªãOverflowErrorãéåºããããã«ãªããŸãããlocaltime()ãããã¯gmtime()ã®åŒã³åºã倱æã§ValueErrorã§ã¯ãªãOSErrorãéåºããããã«ãªããŸãããããŒãžã§ã³ 3.6 ã§å€æŽ:
fromtimestamp()ã¯foldã1ã«ããŠã€ã³ã¹ã¿ã³ã¹ãè¿ããŸããããŒãžã§ã³ 3.15 ã§å€æŽ: Accepts any real number as timestamp, not only integer or float.
- classmethod datetime.utcfromtimestamp(timestamp)¶
POSIX ã¿ã€ã ã¹ã¿ã³ãã«å¯Ÿå¿ããã
tzinfoãNoneã® UTC ã§ã®datetimeãè¿ããŸãã(è¿ããããªããžã§ã¯ã㯠naive ã§ãã)ã¿ã€ã ã¹ã¿ã³ãããã©ãããã©ãŒã ã«ããã C 颿°
localtime()ã§ãµããŒããããŠããç¯å²ãè¶ ããŠããå Žåã«ã¯OverflowErrorããgmtime()ã倱æããå Žåã«ã¯OSErrorãéåºããŸãã ããã¯ãããŠã 1970 幎ãã 2038 幎ã«å¶éãããŠããŸããaware ãª
datetimeãªããžã§ã¯ããåŸãã«ã¯fromtimestamp()ãåŒãã§ãã ãã:datetime.fromtimestamp(timestamp, timezone.utc)
POSIX äºæãã©ãããã©ãŒã ã§ã¯ãããã¯ä»¥äžã®è¡šçŸãšç䟡ã§ã:
datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp)
åŸè ãé€ããåŒã¯åžžã«å¹Žã®å šç¯å² (
MINYEARããMAXYEARãå«ã¿ãŸã) ããµããŒãããŸããèŠå
naive ãª
datetimeãªããžã§ã¯ãã¯å€ãã®datetimeã¡ãœããã§ããŒã«ã«ãªæéãšããŠæ±ãããããã aware 㪠datetime ã䜿ã£ãŠ UTC ã®æå»ã衚ãã®ã奜ãŸããã§ãã ãã®ããã UTC ã§ã®ããç¹å®ã®ã¿ã€ã ã¹ã¿ã³ãã衚ããªããžã§ã¯ãã®äœæã§ã¯datetime.fromtimestamp(timestamp, tz=timezone.utc)ãåŒã³åºãæ¹æ³ãæšå¥šãããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: timestamp ããã©ãããã©ãŒã ã® C 颿°
gmtime()ã®ãµããŒãããå€ã®ç¯å²ããå€ããŠããå ŽåãValueErrorã§ã¯ãªãOverflowErrorãéåºããããã«ãªããŸãããgmtime()ã®åŒã³åºã倱æã§ValueErrorã§ã¯ãªãOSErrorãéåºããããã«ãªããŸãããããŒãžã§ã³ 3.15 ã§å€æŽ: Accepts any real number as timestamp, not only integer or float.
ããŒãžã§ã³ 3.12 ã§éæšå¥š: Use
datetime.fromtimestamp()withUTCinstead.
- classmethod datetime.fromordinal(ordinal)¶
1 幎 1 æ 1 æ¥ãåºæ° 1 ãšããæ©æã°ã¬ãŽãªãªæŠåºæ°ã«å¯Ÿå¿ãã
datetimeãªããžã§ã¯ããè¿ããŸãã1 <= ordinal <= datetime.max.toordinal()ã§ãªããã°ValueErrorãéåºãããŸãã è¿ããããªããžã§ã¯ãã®æéãåãç§ãããã³ãã€ã¯ãç§ã¯ãã¹ãŠ 0 ã§ãtzinfoã¯Noneãšãªã£ãŠããŸãã
- classmethod datetime.combine(date, time, tzinfo=time.tzinfo)¶
Return a new
datetimeobject whose date components are equal to the givendateobject's, and whose time components are equal to the giventimeobject's. If the tzinfo argument is provided, its value is used to set thetzinfoattribute of the result, otherwise thetzinfoattribute of the time argument is used. If the date argument is adatetimeobject, its time components andtzinfoattributes are ignored.For any
datetimeobjectd,d == datetime.combine(d.date(), d.time(), d.tzinfo).ããŒãžã§ã³ 3.6 ã§å€æŽ: tzinfo åŒæ°ã远å ãããŸããã
- classmethod datetime.fromisoformat(date_string)¶
以äžã®äŸå€ãé€ããæå¹ãª ISO 8601 ãã©ãŒãããã§äžãããã date_string ã«å¯Ÿå¿ãã
datetimeãè¿ããŸã :å°æ°ã®ç§ãããã¿ã€ã ãŸãŒã³ãªãã»ããã
Tã»ãã¬ãŒã¿ãŒãä»ã®1æåã®ãŠãã³ãŒãã«çœ®ãæãããã®ãå°æ°ã®æãšåã¯ãµããŒããããŠããŸããã
粟床ã®äœãæ¥ä»ã¯çŸåšãµããŒããããŠããŸãã(
YYYY-MM,YYYY)ãæ¡åŒµãããæ¥ä»è¡šçŸã¯çŸåšãµããŒããããŠããŸãã(
±YYYYYY-MM-DD)ãåºæ°ã®æ¥ä»ã¯çŸåšãµããŒããããŠããŸãã(
YYYY-OOO)ã
äŸ:
>>> import datetime as dt >>> dt.datetime.fromisoformat('2011-11-04') datetime.datetime(2011, 11, 4, 0, 0) >>> dt.datetime.fromisoformat('20111104') datetime.datetime(2011, 11, 4, 0, 0) >>> dt.datetime.fromisoformat('2011-11-04T00:05:23') datetime.datetime(2011, 11, 4, 0, 5, 23) >>> dt.datetime.fromisoformat('2011-11-04T00:05:23Z') datetime.datetime(2011, 11, 4, 0, 5, 23, tzinfo=datetime.timezone.utc) >>> dt.datetime.fromisoformat('20111104T000523') datetime.datetime(2011, 11, 4, 0, 5, 23) >>> dt.datetime.fromisoformat('2011-W01-2T00:05:23.283') datetime.datetime(2011, 1, 4, 0, 5, 23, 283000) >>> dt.datetime.fromisoformat('2011-11-04 00:05:23.283') datetime.datetime(2011, 11, 4, 0, 5, 23, 283000) >>> dt.datetime.fromisoformat('2011-11-04 00:05:23.283+00:00') datetime.datetime(2011, 11, 4, 0, 5, 23, 283000, tzinfo=datetime.timezone.utc) >>> dt.datetime.fromisoformat('2011-11-04T00:05:23+04:00') datetime.datetime(2011, 11, 4, 0, 5, 23, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))
Added in version 3.7.
ããŒãžã§ã³ 3.11 ã§å€æŽ: Previously, this method only supported formats that could be emitted by
date.isoformat()ordatetime.isoformat().
- classmethod datetime.fromisocalendar(year, week, day)¶
Return a
datetimecorresponding to the ISO calendar date specified by year, week and day. The non-date components of the datetime are populated with their normal default values. This is the inverse of the functiondatetime.isocalendar().Added in version 3.8.
- classmethod datetime.strptime(date_string, format)¶
date_string ã«å¯Ÿå¿ãã
datetimeãè¿ããŸãã format ã«ãããã£ãŠæ§æè§£æãããŸããformat ããã€ã¯ãç§ãã¿ã€ã ãŸãŒã³æ å ±ãå«ãŸãªãå Žåã¯ã以äžãšç䟡ã§ã:
datetime(*(time.strptime(date_string, format)[0:6]))
date_string ãš format ã
time.strptime()ã§æ§æè§£æã§ããªãå Žåãããã®é¢æ°ãæå»ã¿ãã«ãè¿ããŠããªãå Žåã«ã¯ValueErrorãéåºããŸããstrftime() and strptime() behavior ããã³datetime.fromisoformat()ãåç §ããŠãã ãããããŒãžã§ã³ 3.15 ã§å€æŽ: If format specifies a day of month (
%d) without a year,ValueErroris raised. This is to avoid a quadrennial leap year bug in code seeking to parse only a month and day as the default year used in absence of one in the format is not a leap year. The workaround is to always include a year in your format. If parsing date_string values that do not have a year, explicitly add a year that is a leap year before parsing:>>> import datetime as dt >>> date_string = "02/29" >>> when = dt.datetime.strptime(f"{date_string};1984", "%m/%d;%Y") # Avoids leap year bug. >>> when.strftime("%B %d") 'February 29'
以äžã«ã¯ã©ã¹å±æ§ã瀺ããŸã:
- datetime.min¶
衚çŸã§ããæãå€ã
datetimeã§ãdatetime(MINYEAR, 1, 1, tzinfo=None)ã§ãã
- datetime.max¶
衚çŸã§ããæãæ°ãã
datetimeã§ãdatetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)ã§ãã
- datetime.resolution¶
çãããªã
datetimeãªããžã§ã¯ãéã®æå°ã®å·®ã§ãtimedelta(microseconds=1)ã§ãã
ã€ã³ã¹ã¿ã³ã¹ã®å±æ§ (èªã¿åºãã®ã¿):
- datetime.month¶
䞡端å€ãå«ã 1 ãã 12 ãŸã§ã®å€ã§ãã
- datetime.day¶
1 ããäžããããæãšå¹Žã«ãããæ¥æ°ãŸã§ã®å€ã§ãã
- datetime.hour¶
in
range(24)ãæºãããŸãã
- datetime.minute¶
in
range(60)ãæºãããŸãã
- datetime.second¶
in
range(60)ãæºãããŸãã
- datetime.microsecond¶
in
range(1000000)ãæºãããŸãã
- datetime.tzinfo¶
datetimeã³ã³ã¹ãã©ã¯ã¿ã« tzinfo åŒæ°ãšããŠäžãããããªããžã§ã¯ãã«ãªããäœãæž¡ãããªãã£ãå Žåã«ã¯Noneã«ãªããŸãã
- datetime.fold¶
[0, 1]ã®ã©ã¡ããã§ãã ç¹°ãè¿ãæéäžã®å®æéã®ææ§ãé€å»ã«äœ¿ãããŸãã (ç¹°ãè¿ãæéã¯ã倿éã®çµããã«æèšãå·»ãæ»ããšãããçŸåšã®ãŸãŒã³ã® UTC ãªãã»ãããæ¿æ²»çãªçç±ã§æžå°ãããšãã«çºçããŸãã) 0ãŸãã¯1ãšããå€ã¯ãåã宿éã§è¡šçŸããã 2 ã€ã®æå»ã®ãã¡ã®ããããæ©ãæ¹ãŸãã¯é ãæ¹ã衚ããŸããAdded in version 3.6.
ãµããŒããããŠããæŒç®ã以äžã«ç€ºããŸã:
æŒç® |
çµæ |
|---|---|
|
(1) |
|
(2) |
|
(3) |
datetime1 == datetime2datetime1 != datetime2 |
ç䟡æ§ã®æ¯èŒã(4) |
datetime1 < datetime2datetime1 > datetime2datetime1 <= datetime2datetime1 >= datetime2 |
é åºã®æ¯èŒã(5) |
datetime2ã¯datetime1ããæétimedeltaç§»åãããã®ã§ãtimedelta.days > 0ã®å Žåæªæ¥ãžãtimedelta.days < 0ã®å Žåéå»ãžç§»åããŸãã çµæã¯å ¥åã® datetime ãšåãtzinfo屿§ãæã¡ãæŒç®åŸã«ã¯datetime2 - datetime1 == timedeltaãšãªããŸããdatetime2.yearãMINYEARãããå°ããããMAXYEARãã倧ããå Žåã«ã¯OverflowErrorãéåºãããŸãã å ¥åã aware ãªãªããžã§ã¯ãã®å Žåã§ãã¿ã€ã ãŸãŒã³ä¿®æ£ã¯å šãè¡ãããŸãããdatetime2 + timedelta == datetime1ãšãªããããªdatetime2ãèšç®ããŸãã ã¡ãªã¿ã«ãçµæã¯å ¥åã® datetime ãšåãtzinfo屿§ãæã¡ãå ¥åã aware ã ãšããŠãã¿ã€ã ãŸãŒã³ä¿®æ£ã¯å šãè¡ãããŸãããSubtraction of a
datetimefrom adatetimeis defined only if both operands are naive, or if both are aware. If one is aware and the other is naive,TypeErroris raised.If both are naive, or both are aware and have the same
tzinfoattribute, thetzinfoattributes are ignored, and the result is atimedeltaobjecttsuch thatdatetime2 + t == datetime1. No time zone adjustments are done in this case.If both are aware and have different
tzinfoattributes,a-bacts as ifaandbwere first converted to naive UTC datetimes. The result is(a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None) - b.utcoffset())except that the implementation never overflows.datetimeãªããžã§ã¯ãã¯ã¿ã€ã ãŸãŒã³ãèæ ®ããŠåãæ¥ä»ãšæå»ã衚ãå Žåãçããã§ããNaive and aware
datetimeobjects are never equal.If both comparands are aware, and have the same
tzinfoattribute, thetzinfoandfoldattributes are ignored and the base datetimes are compared. If both comparands are aware and have differenttzinfoattributes, the comparison acts as comparands were first converted to UTC datetimes except that the implementation never overflows.datetimeinstances in a repeated interval are never equal todatetimeinstances in other time zone.ã¿ã€ã ãŸãŒã³ãèæ ®ããŠãdatetime1 ãæå»ãšã㊠datetime2 ãããåã衚ãå Žåã«ãdatetime1 㯠datetime2 ãããå°ãããšèŠãªãããŸãã
Order comparison between naive and aware
datetimeobjects raisesTypeError.If both comparands are aware, and have the same
tzinfoattribute, thetzinfoandfoldattributes are ignored and the base datetimes are compared. If both comparands are aware and have differenttzinfoattributes, the comparison acts as comparands were first converted to UTC datetimes except that the implementation never overflows.
ããŒãžã§ã³ 3.3 ã§å€æŽ: aware 㪠datetime ã€ã³ã¹ã¿ã³ã¹ãš naive 㪠datetime ã€ã³ã¹ã¿ã³ã¹ã®ç䟡æ¯èŒã§ã¯ TypeError ã¯éåºãããŸããã
ããŒãžã§ã³ 3.13 ã§å€æŽ: Comparison between datetime object and an instance of
the date subclass that is not a datetime subclass
no longer converts the latter to date, ignoring the time part
and the time zone.
The default behavior can be changed by overriding the special comparison
methods in subclasses.
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã:
- datetime.time()¶
åãhourãminuteãsecondãmicrosecond åã³ foldãæã€
timeãªããžã§ã¯ããè¿ããŸããtzinfoã¯Noneã§ããtimetz()ãåç §ããŠãã ãããããŒãžã§ã³ 3.6 ã§å€æŽ: å€ foldã¯è¿ããã
time ãªããžã§ã¯ãã«ã³ããŒãããŸãã
- datetime.timetz()¶
åãhourãminuteãsecondãmicrosecondãfold ããã³ tzinfo 屿§ãæã€
timeãªããžã§ã¯ããè¿ããŸããtime()ã¡ãœãããåç §ããŠãã ãããããŒãžã§ã³ 3.6 ã§å€æŽ: å€ foldã¯è¿ããã
time ãªããžã§ã¯ãã«ã³ããŒãããŸãã
- datetime.replace(year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)¶
Return a new
datetimeobject with the same attributes, but with specified parameters updated. Note thattzinfo=Nonecan be specified to create a naive datetime from an aware datetime with no conversion of date and time data.datetimeãªããžã§ã¯ãã¯æ±çšçãªé¢æ°copy.replace()ã«ããµããŒããããŠããŸããããŒãžã§ã³ 3.6 ã§å€æŽ: fold ãã©ã¡ãŒã¿ã远å ãããŸããã
- datetime.astimezone(tz=None)¶
tz ãæ°ãã«
tzinfo屿§ ãšããŠæã€datetimeãªããžã§ã¯ããè¿ããŸãã æ¥ä»ããã³æå»ããŒã¿ã調æŽããŠãè¿ãå€ã self ãšåã UTC æå»ãæã¡ã tz ã«ãããããŒã«ã«ãªæå»ã衚ãããã«ããŸããããäžããããå Žåã tz ã¯
tzinfoã®ãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãªããã°ãªããã ã€ã³ã¹ã¿ã³ã¹ã®utcoffset()ããã³dst()ã¡ãœããã¯Noneãè¿ããŠã¯ãªããŸããããã self ã naive ãªãã°ãããããã·ã¹ãã ã®ã¿ã€ã ãŸãŒã³ã§æéã衚çŸããŸããåŒæ°ç¡ã (ãããã¯
tz=Noneã®åœ¢ ) ã§åŒã³åºãããå Žåãã·ã¹ãã ã®ããŒã«ã«ãªã¿ã€ã ãŸãŒã³ã倿Žå ã®ã¿ã€ã ãŸãŒã³ã ãšä»®å®ãããŸãã 倿åŸã® datetime ã€ã³ã¹ã¿ã³ã¹ã®.tzinfo屿§ã«ã¯ã OS ããååŸãããŸãŒã³åãšãªãã»ãããæã€timezoneã€ã³ã¹ã¿ã³ã¹ãèšå®ãããŸããself.tzinfoã tz ã®å Žåãself.astimezone(tz)㯠self ã«çãããªããŸããã€ãŸããdate ããã³ time ã«å¯Ÿãã調æŽã¯è¡ãããŸãããããã§ãªãå Žåãçµæã¯ã¿ã€ã ãŸãŒã³ tz ã«ãããããŒã«ã«æå»ã§ã self ãšåã UTC æå»ã衚ãããã«ãªããŸããããã¯ãastz = dt.astimezone(tz)ãšããåŸãastz - astz.utcoffset()ã¯éåžždt - dt.utcoffset()ãšåã date ããã³ time ãæã€ããšã瀺ããŸããåã«
timezoneãªããžã§ã¯ã tz ãdatetimeãªããžã§ã¯ã dt ã«è¿œå ãããã ãã§ãæ¥ä»ãæå»ããŒã¿ãžã®èª¿æŽãè¡ããªãã®ãªããdt.replace(tzinfo=tz)ã䜿ã£ãŠãã ãããåã« aware ãªdatetimeãªããžã§ã¯ã dt ããtimezoneãªããžã§ã¯ããé€å»ãããã ãã§ãæ¥ä»ãæå»ããŒã¿ã®å€æãè¡ããªãã®ãªããdt.replace(tzinfo=None)ã䜿ã£ãŠãã ãããããã©ã«ãã®
tzinfo.fromutc()ã¡ãœãããtzinfoã®ãµãã¯ã©ã¹ã§äžæžãããŠ,astimezone()ãè¿ãçµæã«åœ±é¿ãåãŒãããšãã§ããŸãããšã©ãŒã®å Žåãç¡èŠãããšãastimezone()ã¯ä»¥äžã®ããã«åäœããŸã:def astimezone(self, tz): if self.tzinfo is tz: return self # Convert self to UTC, and attach the new timezone object. utc = (self - self.utcoffset()).replace(tzinfo=tz) # Convert from UTC to tz's local time. return tz.fromutc(utc)
ããŒãžã§ã³ 3.3 ã§å€æŽ: tz ãçç¥å¯èœã«ãªããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ:
datetime.datetime.astimezone()ã¡ãœããã naive ãªã€ã³ã¹ã¿ã³ã¹ã«å¯ŸããŠåŒã³åºããããã«ãªããŸãããããã¯ãã·ã¹ãã ã®ããŒã«ã«ãªæéã衚çŸããŠãããšæ³å®ãããŸãã
- datetime.utcoffset()¶
tzinfoãNoneã®å ŽåãNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.utcoffset(self)ãè¿ããŸãã åŸè ã®åŒãNoneããã㯠1 æ¥ä»¥äžã®å€§ãããæã€timedeltaãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- datetime.dst()¶
tzinfoãNoneã®å ŽåNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.dst(self)ãè¿ããŸãã åŸè ã®åŒãNoneãããã¯ã1 æ¥æªæºã®å€§ãããæã€timedeltaãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: DST ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- datetime.tzname()¶
tzinfoãNoneã®å ŽåNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.tzname(self)ãè¿ããŸãã åŸè ã®åŒãNoneãæååãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸãã
- datetime.timetuple()¶
time.localtime()ãè¿ããããªtime.struct_timeãè¿ããŸããd.timetuple()ã¯æ¬¡ã®åŒãšç䟡ã§ã:time.struct_time((d.year, d.month, d.day, d.hour, d.minute, d.second, d.weekday(), yday, dst))
ããã§
yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1ã¯ãã®å¹Žã®1æ1æ¥ã 1 ãšãããšãã®ãã®æ¥ã®äœçœ®ã§ãã è¿ãããã¿ãã«ã®tm_isdstãã©ã°ã¯dst()ã¡ãœããã«åŸã£ãŠèšå®ãããŸã:tzinfoãNoneãdst()ãNoneãè¿ãå Žåãtm_isdstã¯-1ã«èšå®ãããŸã; ããã§ãªãå Žåãdst()ããŒãã§ãªãå€ãè¿ããštm_isdstã¯1ãšãªããŸã; ãã以å€ã®å Žåã«ã¯tm_isdstã¯0ã«èšå®ãããŸãã
- datetime.utctimetuple()¶
If
datetimeinstancedis naive, this is the same asd.timetuple()except thattm_isdstis forced to 0 regardless of whatd.dst()returns. DST is never in effect for a UTC time.If
dis aware,dis normalized to UTC time, by subtractingd.utcoffset(), and atime.struct_timefor the normalized time is returned.tm_isdstis forced to 0. Note that anOverflowErrormay be raised ifd.yearwasMINYEARorMAXYEARand UTC adjustment spills over a year boundary.èŠå
naive ãª
datetimeãªããžã§ã¯ãã¯å€ãã®datetimeã¡ãœããã§ããŒã«ã«ãªæéãšããŠæ±ãããããã aware 㪠datetime ã䜿ã£ãŠ UTC ã®æå»ã衚ãã®ã奜ãŸããã§ããçµæãšããŠãdatetime.utctimetuple()ã¯èª€è§£ãæããããè¿ãå€ãè¿ããããããŸããã UTC ã衚ã naive ãªdatetimeããã£ãå Žåãdatetime.timetuple()ã䜿ãããšããã§ã¯datetime.replace(tzinfo=timezone.utc)ã§ aware ã«ããŸãã
- datetime.toordinal()¶
å çºã°ã¬ãŽãªãªæŠã«ãããæ¥ä»åºæ°ãè¿ããŸãã
self.date().toordinal()ãšåãã§ãã
- datetime.timestamp()¶
datetimeã€ã³ã¹ã¿ã³ã¹ã«å¯Ÿå¿ãã POSIX ã¿ã€ã ã¹ã¿ã³ããè¿ããŸãã è¿ãå€ã¯time.time()ã§è¿ãããå€ã«è¿ãfloatã§ããNaive
datetimeinstances are assumed to represent local time and this method relies on platform C functions to perform the conversion. Sincedatetimesupports a wider range of values than the platform C functions on many platforms, this method may raiseOverflowErrororOSErrorfor times far in the past or far in the future.aware ãª
datetimeã€ã³ã¹ã¿ã³ã¹ã«å¯ŸããŠã¯ä»¥äžã®ããã«è¿ãå€ãèšç®ãããŸã:(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
泚é
UTC æå»ã衚ã naive ãª
datetimeã€ã³ã¹ã¿ã³ã¹ããçŽæ¥ POSIX ã¿ã€ã ã¹ã¿ã³ããååŸããã¡ãœããã¯ãããŸããã ã¢ããªã±ãŒã·ã§ã³ããã®å€æã䜿ã£ãŠãããã·ã¹ãã ã®ã¿ã€ã ãŸãŒã³ã UTC ã«èšå®ãããŠããªãã£ãå Žåãtzinfo=timezone.utcãåŒæ°ã«äžããããšã§ POSIX ã¿ã€ã ã¹ã¿ã³ããååŸã§ããŸã:timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
ãããã¯çŽæ¥ã¿ã€ã ã¹ã¿ã³ããèšç®ããããšãã§ããŸã:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
Added in version 3.3.
ããŒãžã§ã³ 3.6 ã§å€æŽ: The
timestamp()method uses thefoldattribute to disambiguate the times during a repeated interval.ããŒãžã§ã³ 3.6 ã§å€æŽ: This method no longer relies on the platform C
mktime()function to perform conversions.
- datetime.weekday()¶
æææ¥ã 0ãæ¥ææ¥ã 6 ãšããŠãææ¥ãæŽæ°ã§è¿ããŸãã
self.date().weekday()ãšåãã§ããisoweekday()ãåç §ããŠãã ããã
- datetime.isoweekday()¶
æææ¥ã 1ãæ¥ææ¥ã 7 ãšããŠãææ¥ãæŽæ°ã§è¿ããŸãã
self.date().isoweekday()ãšç䟡ã§ããweekday()ãisocalendar()ãåç §ããŠãã ããã
- datetime.isocalendar()¶
yearãweekãweekdayã®3ã€ã§æ§æããã named tuple ãè¿ããŸããself.date().isocalendar()ãšç䟡ã§ãã
- datetime.isoformat(sep='T', timespec='auto')¶
æ¥æã ISO 8601 æžåŒã§è¡šããæååã§è¿ããŸã:
microsecondã 0 ã§ãªãå Žåã¯YYYY-MM-DDTHH:MM:SS.ffffffmicrosecondã 0 ã®å Žåã¯YYYY-MM-DDTHH:MM:SS
utcoffset()ãNoneãè¿ããªãå Žåã¯ãæååã®åŸãã« UTC ãªãã»ããã远èšãããŸã:microsecondã 0 ã§ãªãå Žåã¯YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]microsecondã 0 ã®å Žåã¯YYYY-MM-DDTHH:MM:SS+HH:MM[:SS[.ffffff]]
äŸ:
>>> import datetime as dt >>> dt.datetime(2019, 5, 18, 15, 17, 8, 132263).isoformat() '2019-05-18T15:17:08.132263' >>> dt.datetime(2019, 5, 18, 15, 17, tzinfo=dt.timezone.utc).isoformat() '2019-05-18T15:17:00+00:00'
ãªãã·ã§ã³ã®åŒæ° sep (ããã©ã«ãã§ã¯
'T'ã§ã) 㯠1 æåã®ã»ãã¬ãŒã¿ã§ãçµæã®æååã®æ¥ä»ãšæå»ã®éã«çœ®ãããŸããäŸãã°:>>> import datetime as dt >>> class TZ(dt.tzinfo): ... """A time zone with an arbitrary, constant -06:39 offset.""" ... def utcoffset(self, when): ... return dt.timedelta(hours=-6, minutes=-39) ... >>> dt.datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ') '2002-12-25 00:00:00-06:39' >>> dt.datetime(2009, 11, 27, microsecond=100, tzinfo=TZ()).isoformat() '2009-11-27T00:00:00.000100-06:39'
ãªãã·ã§ã³åŒæ° timespec ã¯ãå«ãã远å ã®æéã®èŠçŽ ã®æ°ãæå®ããŸã(ããã©ã«ãã§ã¯
'auto'ã§ã)ã以äžã®å äžã€ãæå®ããŠãã ããã'auto':microsecondã0ã§ããå Žå'seconds'ãšçãããããã§ãªãå Žåã¯'microseconds'ãšçãããªããŸãã'hours':hourã2æ¡ã®HHæžåŒã§å«ããŸãã'minutes':hourããã³minuteãHH:MMã®æžåŒã§å«ããŸãã'seconds':hourãminuteãsecondãHH:MM:SSã®æžåŒã§å«ããŸãã'milliseconds': å šãŠã®æå»ãå«ã¿ãŸãããå°æ°ç¬¬äºäœãããªç§ã«åãæšãŠãŸããHH:MM:SS.sssã®æžåŒã§è¡šçŸããŸãã'microseconds': å šãŠã®æå»ãHH:MM:SS.mmmmmmã®æžåŒã§å«ããŸãã
泚é
é€å€ãããèŠçŽ ã¯äžžã蟌ã¿ã§ã¯ãªããåãæšãŠãããŸãã
äžæ£ãª timespec åŒæ°ã«ã¯
ValueErrorããããããŸã:>>> import datetime as dt >>> dt.datetime.now().isoformat(timespec='minutes') '2002-12-25T00:00' >>> my_datetime = dt.datetime(2015, 1, 1, 12, 30, 59, 0) >>> my_datetime.isoformat(timespec='microseconds') '2015-01-01T12:30:59.000000'
ããŒãžã§ã³ 3.6 ã§å€æŽ: timespec ãã©ã¡ãŒã¿ã远å ããŸãã.
- datetime.ctime()¶
æ¥ä»ããã³æå»ã衚ãæååãè¿ããŸã:
>>> import datetime as dt >>> dt.datetime(2002, 12, 4, 20, 30, 40).ctime() 'Wed Dec 4 20:30:40 2002'
åºåæååã¯å ¥åã aware ã§ãã naive ã§ãããã¿ã€ã ãŸãŒã³æ å ±ãå«ã¿ ãŸããã
d.ctime()ã¯æ¬¡ã®åŒãšç䟡ã§ã:time.ctime(time.mktime(d.timetuple()))
ãããç䟡ã«ãªãã®ã¯ã (
time.ctime()ã«åŒã³åºãããdatetime.ctime()ã«åŒã³åºãããªã) ãã€ãã£ãã® C 颿°ctime()ã C æšæºã«æºæ ããŠãããã©ãããã©ãŒã äžã§ã§ãã
- datetime.strftime(format)¶
æç€ºçãªæžåŒæååã§å¶åŸ¡ããããæ¥ä»ããã³æå»ã衚çŸããæååãè¿ããŸããstrftime() and strptime() behavior ããã³
datetime.isoformat()ãåç §ããŠãã ããã
- datetime.__format__(format)¶
datetime.strftime()ãšç䟡ã§ãã ããã«ããã ãã©ãŒãããæžã¿æååãªãã©ã« ã®äžãstr.format()ã䜿ã£ãŠãããšãã«datetimeãªããžã§ã¯ãã®æžåŒæååãæå®ã§ããŸãã strftime() and strptime() behavior ããã³datetime.isoformat()ãåç §ããŠãã ããã
Examples of usage: datetime¶
datetime ãªããžã§ã¯ãã䜿ãäŸ:
>>> import datetime as dt
>>> # Using datetime.combine()
>>> d = dt.date(2005, 7, 14)
>>> t = dt.time(12, 30)
>>> dt.datetime.combine(d, t)
datetime.datetime(2005, 7, 14, 12, 30)
>>> # Using datetime.now()
>>> dt.datetime.now()
datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1
>>> dt.datetime.now(dt.timezone.utc)
datetime.datetime(2007, 12, 6, 15, 29, 43, 79060, tzinfo=datetime.timezone.utc)
>>> # Using datetime.strptime()
>>> my_datetime = dt.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> my_datetime
datetime.datetime(2006, 11, 21, 16, 30)
>>> # Using datetime.timetuple() to get tuple of all attributes
>>> tt = my_datetime.timetuple()
>>> for it in tt:
... print(it)
...
2006 # year
11 # month
21 # day
16 # hour
30 # minute
0 # second
1 # weekday (0 = Monday)
325 # number of days since 1st January
-1 # dst - method tzinfo.dst() returned None
>>> # Date in ISO format
>>> ic = my_datetime.isocalendar()
>>> for it in ic:
... print(it)
...
2006 # ISO year
47 # ISO week
2 # ISO weekday
>>> # Formatting a datetime
>>> my_datetime.strftime("%A, %d. %B %Y %I:%M%p")
'Tuesday, 21. November 2006 04:30PM'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(my_datetime, "day", "month", "time")
'The day is 21, the month is November, the time is 04:30PM.'
äžã«ããäŸã§ã¯ã1945幎ãŸã§ã¯ +4 UTC ããã以é㯠+4:30 UTC ã䜿çšããŠããã¢ãã¬ãã¹ã¿ã³ã®ã«ããŒã«ã®ã¿ã€ã ãŸãŒã³æ
å ±ã衚çŸãã tzinfo ã®ãµãã¯ã©ã¹ãå®çŸ©ããŠããŸã:
import datetime as dt
class KabulTz(dt.tzinfo):
# Kabul used +4 until 1945, when they moved to +4:30
UTC_MOVE_DATE = dt.datetime(1944, 12, 31, 20, tzinfo=dt.timezone.utc)
def utcoffset(self, when):
if when.year < 1945:
return dt.timedelta(hours=4)
elif (1945, 1, 1, 0, 0) <= when.timetuple()[:5] < (1945, 1, 1, 0, 30):
# An ambiguous ("imaginary") half-hour range representing
# a 'fold' in time due to the shift from +4 to +4:30.
# If when falls in the imaginary range, use fold to decide how
# to resolve. See PEP 495.
return dt.timedelta(hours=4, minutes=(30 if when.fold else 0))
else:
return dt.timedelta(hours=4, minutes=30)
def fromutc(self, when):
# Follow same validations as in datetime.tzinfo
if not isinstance(when, dt.datetime):
raise TypeError("fromutc() requires a datetime argument")
if when.tzinfo is not self:
raise ValueError("when.tzinfo is not self")
# A custom implementation is required for fromutc as
# the input to this function is a datetime with utc values
# but with a tzinfo set to self.
# See datetime.astimezone or fromtimestamp.
if when.replace(tzinfo=dt.timezone.utc) >= self.UTC_MOVE_DATE:
return when + dt.timedelta(hours=4, minutes=30)
else:
return when + dt.timedelta(hours=4)
def dst(self, when):
# Kabul does not observe daylight saving time.
return dt.timedelta(0)
def tzname(self, when):
if when >= self.UTC_MOVE_DATE:
return "+04:30"
return "+04"
äžã«åºãŠãã KabulTz ã®äœ¿ãæ¹:
>>> tz1 = KabulTz()
>>> # Datetime before the change
>>> dt1 = dt.datetime(1900, 11, 21, 16, 30, tzinfo=tz1)
>>> print(dt1.utcoffset())
4:00:00
>>> # Datetime after the change
>>> dt2 = dt.datetime(2006, 6, 14, 13, 0, tzinfo=tz1)
>>> print(dt2.utcoffset())
4:30:00
>>> # Convert datetime to another time zone
>>> dt3 = dt2.astimezone(dt.timezone.utc)
>>> dt3
datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)
>>> dt2
datetime.datetime(2006, 6, 14, 13, 0, tzinfo=KabulTz())
>>> dt2 == dt3
True
time objects¶
time ãªããžã§ã¯ã㯠(ããŒã«ã«ã®) æ¥äžæå»ã衚çŸããŸãã
ãã®æå»è¡šçŸã¯ç¹å®ã®æ¥ã®åœ±é¿ãåããã tzinfo ãªããžã§ã¯ããä»ããä¿®æ£ã®å¯Ÿè±¡ãšãªããŸãã
- class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)¶
å šãŠã®åŒæ°ã¯ãªãã·ã§ã³ã§ãã tzinfo ã¯
NoneãŸãã¯tzinfoã¯ã©ã¹ã®ãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã«ããããšãã§ããŸããæ®ãã®åŒæ°ã¯æŽæ°ã§ã以äžã®ãããªç¯å²ã«å ¥ããªããã°ãªããŸãã:0 <= hour < 24,0 <= minute < 60,0 <= second < 60,0 <= microsecond < 1000000,fold in [0, 1].
åŒæ°ããããã®ç¯å²å€ã«ããå Žåã
ValueErrorãéåºãããŸãã tzinfo ã®ããã©ã«ãå€ãNoneã§ãã以å€ã®ããã©ã«ãå€ã¯0ã§ãã
以äžã«ã¯ã©ã¹å±æ§ã瀺ããŸã:
- time.resolution¶
çãããªã
timeãªããžã§ã¯ãéã®æå°ã®å·®ã§ãtimedelta(microseconds=1)ã§ãã,timeãªããžã§ã¯ãéã®ååæŒç®ã¯ãµããŒããããŠããªãã®ã§æ³šæããŠãã ããã
ã€ã³ã¹ã¿ã³ã¹ã®å±æ§ (èªã¿åºãã®ã¿):
- time.hour¶
in
range(24)ãæºãããŸãã
- time.minute¶
in
range(60)ãæºãããŸãã
- time.second¶
in
range(60)ãæºãããŸãã
- time.microsecond¶
in
range(1000000)ãæºãããŸãã
- time.tzinfo¶
timeã³ã³ã¹ãã©ã¯ã¿ã« tzinfo åŒæ°ãšããŠäžãããããªããžã§ã¯ãã«ãªããäœãæž¡ãããªãã£ãå Žåã«ã¯Noneã«ãªããŸãã
- time.fold¶
[0, 1]ã®ã©ã¡ããã§ãã ç¹°ãè¿ãæéäžã®å®æéã®ææ§ãé€å»ã«äœ¿ãããŸãã (ç¹°ãè¿ãæéã¯ã倿éã®çµããã«æèšãå·»ãæ»ããšãããçŸåšã®ãŸãŒã³ã® UTC ãªãã»ãããæ¿æ²»çãªçç±ã§æžå°ãããšãã«çºçããŸãã) 0ãŸãã¯1ãšããå€ã¯ãåã宿éã§è¡šçŸããã 2 ã€ã®æå»ã®ãã¡ã®ããããæ©ãæ¹ãŸãã¯é ãæ¹ã衚ããŸããAdded in version 3.6.
time objects support equality and order comparisons,
where a is considered less than b when a precedes b in time.
Naive and aware time objects are never equal.
Order comparison between naive and aware time objects raises
TypeError.
æ¯èŒå¯Ÿè±¡ãäž¡æ¹ãšã aware ã§ãããåã tzinfo 屿§ãæã€å Žåã tzinfo ãš fold 屿§ã¯ç¡èŠããæéã ãã§æ¯èŒãè¡ãããŸããæ¯èŒå¯Ÿè±¡ãäž¡æ¹ãšã aware ã§ãããç°ãªã tzinfo 屿§ãæã€å ŽåããŸãæåã« (self.utcoffset() ã§ååŸã§ãã) ããããã® UTC ãªãã»ãããåŒã調æŽãè¡ãããŸãã
ããŒãžã§ã³ 3.3 ã§å€æŽ: aware 㪠ã€ã³ã¹ã¿ã³ã¹ãš naive 㪠time ã€ã³ã¹ã¿ã³ã¹ã®ç䟡æ¯èŒã§ã¯ TypeError ã¯éåºãããŸããã
ããŒã«å€ã®æèã§ã¯ã time ãªããžã§ã¯ãã¯åžžã«çãšã¿ãªãããŸãã
ããŒãžã§ã³ 3.5 ã§å€æŽ: Before Python 3.5, a time object was considered to be false if it
represented midnight in UTC. This behavior was considered obscure and
error-prone and has been removed in Python 3.5. See bpo-13936 for more
information.
Other constructors:
- classmethod time.fromisoformat(time_string)¶
以äžã®äŸå€ãé€ããæå¹ãª ISO 8601 ãã©ãŒãããã§äžãããã time_string ã«å¯Ÿå¿ãã
timeãè¿ããŸã :å°æ°ã®ç§ãããã¿ã€ã ãŸãŒã³ãªãã»ããã
The leading
T, normally required in cases where there may be ambiguity between a date and a time, is not required.Fractional seconds may have any number of digits (anything beyond 6 will be truncated).
å°æ°ã®æãšåã¯ãµããŒããããŠããŸããã
äŸ:
>>> import datetime as dt >>> dt.time.fromisoformat('04:23:01') datetime.time(4, 23, 1) >>> dt.time.fromisoformat('T04:23:01') datetime.time(4, 23, 1) >>> dt.time.fromisoformat('T042301') datetime.time(4, 23, 1) >>> dt.time.fromisoformat('04:23:01.000384') datetime.time(4, 23, 1, 384) >>> dt.time.fromisoformat('04:23:01,000384') datetime.time(4, 23, 1, 384) >>> dt.time.fromisoformat('04:23:01+04:00') datetime.time(4, 23, 1, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400))) >>> dt.time.fromisoformat('04:23:01Z') datetime.time(4, 23, 1, tzinfo=datetime.timezone.utc) >>> dt.time.fromisoformat('04:23:01+00:00') datetime.time(4, 23, 1, tzinfo=datetime.timezone.utc)
Added in version 3.7.
ããŒãžã§ã³ 3.11 ã§å€æŽ: Previously, this method only supported formats that could be emitted by
time.isoformat().
- classmethod time.strptime(date_string, format)¶
Return a
timecorresponding to date_string, parsed according to format.format ããã€ã¯ãç§ãã¿ã€ã ãŸãŒã³æ å ±ãå«ãŸãªãå Žåã¯ã以äžãšç䟡ã§ã:
time(*(time.strptime(date_string, format)[3:6]))
ValueErroris raised if the date_string and format cannot be parsed bytime.strptime()or if it returns a value which is not a time tuple. See also strftime() and strptime() behavior andtime.fromisoformat().Added in version 3.14.
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã:
- time.replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)¶
Return a new
timewith the same values, but with specified parameters updated. Note thattzinfo=Nonecan be specified to create a naivetimefrom an awaretime, without conversion of the time data.timeãªããžã§ã¯ãã¯æ±çšçãªé¢æ°copy.replace()ã«ããµããŒããããŠããŸããããŒãžã§ã³ 3.6 ã§å€æŽ: fold ãã©ã¡ãŒã¿ã远å ãããŸããã
- time.isoformat(timespec='auto')¶
æå»ã ISO 8601 æžåŒã§è¡šããæ¬¡ã®æååã®ãã¡1ã€ãè¿ããŸã:
microsecondã 0 ã§ãªãå Žåã¯HH:MM:SS.ffffffmicrosecondã 0 ã®å Žåã¯HH:MM:SSutcoffset()ãNoneãè¿ããªãå ŽåãHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]microsecondã 0 ã§utcoffset()ãNoneãè¿ããªãå ŽåãHH:MM:SS+HH:MM[:SS[.ffffff]]
ãªãã·ã§ã³åŒæ° timespec ã¯ãå«ãã远å ã®æéã®èŠçŽ ã®æ°ãæå®ããŸã(ããã©ã«ãã§ã¯
'auto'ã§ã)ã以äžã®å äžã€ãæå®ããŠãã ããã'auto':microsecondã0ã§ããå Žå'seconds'ãšçãããããã§ãªãå Žåã¯'microseconds'ãšçãããªããŸãã'hours':hourã2æ¡ã®HHæžåŒã§å«ããŸãã'minutes':hourããã³minuteãHH:MMã®æžåŒã§å«ããŸãã'seconds':hourãminuteãsecondãHH:MM:SSã®æžåŒã§å«ããŸãã'milliseconds': å šãŠã®æå»ãå«ã¿ãŸãããå°æ°ç¬¬äºäœãããªç§ã«åãæšãŠãŸããHH:MM:SS.sssã®æžåŒã§è¡šçŸããŸãã'microseconds': å šãŠã®æå»ãHH:MM:SS.mmmmmmã®æžåŒã§å«ããŸãã
泚é
é€å€ãããèŠçŽ ã¯äžžã蟌ã¿ã§ã¯ãªããåãæšãŠãããŸãã
äžæ£ãª timespec åŒæ°ã«ã¯
ValueErrorããããããŸãã以äžã¯ããã°ã©ã äŸã§ã:
>>> import datetime as dt >>> dt.time(hour=12, minute=34, second=56, microsecond=123456).isoformat(timespec='minutes') '12:34' >>> my_time = dt.time(hour=12, minute=34, second=56, microsecond=0) >>> my_time.isoformat(timespec='microseconds') '12:34:56.000000' >>> my_time.isoformat(timespec='auto') '12:34:56'
ããŒãžã§ã³ 3.6 ã§å€æŽ: timespec ãã©ã¡ãŒã¿ã远å ããŸãã.
- time.__str__()¶
For a time
t,str(t)is equivalent tot.isoformat().
- time.strftime(format)¶
æç€ºçãªæžåŒæååã§å¶åŸ¡ããããæå»ã衚çŸããæååãè¿ããŸããstrftime() and strptime() behavior ããã³
time.isoformat()ãåç §ããŠãã ããã
- time.__format__(format)¶
time.strftime()ãšç䟡ã§ãã ããã«ããã ãã©ãŒãããæžã¿æååãªãã©ã« ã®äžãstr.format()ã䜿ã£ãŠãããšãã«timeãªããžã§ã¯ãã®æžåŒæååãæå®ã§ããŸãã strftime() and strptime() behavior ããã³time.isoformat()ãåç §ããŠãã ããã
- time.utcoffset()¶
tzinfoãNoneã®å ŽåãNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.utcoffset(None)ãè¿ããŸãã åŸè ã®åŒãNoneããã㯠1 æ¥ä»¥äžã®å€§ãããæã€timedeltaãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- time.dst()¶
tzinfoãNoneã®å ŽåNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.dst(None)ãè¿ããŸãã åŸè ã®åŒãNoneãããã¯ã1 æ¥æªæºã®å€§ãããæã€timedeltaãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: DST ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- time.tzname()¶
tzinfoãNoneã®å ŽåNoneãè¿ããããã§ãªãå Žåã«ã¯self.tzinfo.tzname(None)ãè¿ããŸãã åŸè ã®åŒãNoneãæååãªããžã§ã¯ãã®ãããããè¿ããªãå Žåã«ã¯äŸå€ãéåºããŸãã
Examples of usage: time¶
time ãªããžã§ã¯ãã䜿ãäŸ:
>>> import datetime as dt
>>> class TZ1(dt.tzinfo):
... def utcoffset(self, when):
... return dt.timedelta(hours=1)
... def dst(self, when):
... return dt.timedelta(0)
... def tzname(self, when):
... return "+01:00"
... def __repr__(self):
... return f"{self.__class__.__name__}()"
...
>>> t = dt.time(12, 10, 30, tzinfo=TZ1())
>>> t
datetime.time(12, 10, 30, tzinfo=TZ1())
>>> t.isoformat()
'12:10:30+01:00'
>>> t.dst()
datetime.timedelta(0)
>>> t.tzname()
'+01:00'
>>> t.strftime("%H:%M:%S %Z")
'12:10:30 +01:00'
>>> 'The {} is {:%H:%M}.'.format("time", t)
'The time is 12:10.'
tzinfo objects¶
- class datetime.tzinfo¶
This is an abstract base class, meaning that this class should not be instantiated directly. Define a subclass of
tzinfoto capture information about a particular time zone.An instance of (a concrete subclass of)
tzinfocan be passed to the constructors fordatetimeandtimeobjects. The latter objects view their attributes as being in local time, and thetzinfoobject supports methods revealing offset of local time from UTC, the name of the time zone, and DST offset, all relative to a date or time object passed to them.You need to derive a concrete subclass, and (at least) supply implementations of the standard
tzinfomethods needed by thedatetimemethods you use. Thedatetimemodule providestimezone, a simple concrete subclass oftzinfowhich can represent time zones with fixed offset from UTC such as UTC itself or North American EST and EDT.pickle åã«ã€ããŠã®ç¹æ®ãªèŠæ±äºé :
tzinfoã®ãµãã¯ã©ã¹ã¯åŒæ°ãªãã§åŒã³åºãããšã®ã§ãã__init__()ã¡ãœãããæããªããã°ãªããŸãããããã§ãªããã°ã pickle åããããšã¯ã§ããŸãããããã unpickle åããããšã¯ã§ããªãã§ããããããã¯æè¡çãªåŽé¢ããã®èŠæ±ã§ãããå°æ¥ç·©åããããããããŸãããtzinfoã®å ·äœçãªãµãã¯ã©ã¹ã§ã¯ã以äžã®ã¡ãœãããå®è£ ããå¿ èŠããããŸããå³å¯ã«ã©ã®ã¡ãœãããå¿ èŠãªã®ãã¯ã aware ãªdatetimeãªããžã§ã¯ãããã®ãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãã©ã®ããã«äœ¿ããã«äŸåããŸããäžç¢ºããªãã°ãåã«å šãŠãå®è£ ããŠãã ããã
- tzinfo.utcoffset(dt)¶
ããŒã«ã«æéã® UTC ããã®ãªãã»ãããã UTC ããæ±åããæ£ãšãã
timedeltaãªããžã§ã¯ãã§è¿ããŸããããŒã«ã«æéã UTC ã®è¥¿åŽã«ããå Žåããã®å€ã¯è² ã«ãªããŸãããã®ã¡ãœãã㯠UTC ããã®ãªãã»ããã® ç·èš ã衚ããŠããŸããäŸãã°ã
tzinfoãªããžã§ã¯ããã¿ã€ã ãŸãŒã³ãš DST ä¿®æ£ã®äž¡æ¹ã衚çŸããå Žåãutcoffset()ã¯ãããã®åèšãè¿ããªããã°ãªããŸããã UTC ãªãã»ãããæªç¥ã§ããå ŽåãNoneãè¿ããŸãã ããã§ãªãå Žåã«ã¯ãè¿ãããå€ã¯-timedelta(hours=24)ããtimedelta(hours=24)ãŸã§ã®timedeltaå¢çãå«ãŸãªããªããžã§ã¯ãã§ãªããã°ãªããŸãã (ãªãã»ããã®å€§ãã㯠1 æ¥ããçããªããã°ãªããŸãã)ã ã»ãšãã©ã®utcoffset()å®è£ ã¯ããããã以äžã®äºã€ã®ãã¡ã®äžã€ã«äŒŒããã®ã«ãªãã§ããã:return CONSTANT # fixed-offset class return CONSTANT + self.dst(dt) # daylight-aware class
utcoffset()ãNoneãè¿ããªãå Žåãdst()ãNoneãè¿ããŠã¯ãªããŸãããutcoffset()ã®ããã©ã«ãã®å®è£ ã¯NotImplementedErrorãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- tzinfo.dst(dt)¶
倿é (DST) ä¿®æ£ãã
timedeltaãªããžã§ã¯ãã§è¿ããŸãã DST æ å ±ãæªç¥ã®å ŽåãNoneãè¿ãããŸããDST ãæå¹ã§ãªãå Žåã«ã¯
timedelta(0)ãè¿ããŸãã DST ãæå¹ã®å Žåããªãã»ããã¯timedeltaãªããžã§ã¯ãã§è¿ããŸã (詳现ã¯utcoffset()ãåç §ããŠãã ãã)ã DST ãªãã»ãããå©çšå¯èœãªå Žåããã®å€ã¯utcoffset()ãè¿ã UTC ããã®ãªãã»ããã«ã¯æ¢ã«å ç®ãããŠããããã DST ãåå¥ã«ååŸããå¿ èŠããªãéãdst()ã䜿ã£ãŠåãåãããå¿ èŠã¯ãªãã®ã§æ³šæããŠãã ããã äŸãã°ãdatetime.timetuple()ã¯tzinfo屿§ã®dst()ã¡ãœãããåŒãã§tm_isdstãã©ã°ãã»ãããããŠãããã©ãã倿ããtzinfo.fromutc()ã¯dst()ã¿ã€ã ãŸãŒã³ãç§»åããéã« DST ã«ãã倿Žããããã©ããã調ã¹ãŸããæšæºããã³å€æéã®äž¡æ¹ãã¢ãã«åããŠãã
tzinfoãµãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ tz ã¯ä»¥äžã®åŒ:tz.utcoffset(dt) - tz.dst(dt)must return the same result for every
datetimedt withdt.tzinfo == tz. For sanetzinfosubclasses, this expression yields the time zone's "standard offset", which should not depend on the date or the time, but only on geographic location. The implementation ofdatetime.astimezone()relies on this, but cannot detect violations; it's the programmer's responsibility to ensure it. If atzinfosubclass cannot guarantee this, it may be able to override the default implementation oftzinfo.fromutc()to work correctly withastimezone()regardless.ã»ãšãã©ã®
dst()å®è£ ã¯ããããã以äžã®äºã€ã®ãã¡ã®äžã€ã«äŒŒããã®ã«ãªãã§ããã:import datetime as dt def dst(self, when): # a fixed-offset class: doesn't account for DST return dt.timedelta(0)
ãããã¯:
import datetime as dt def dst(self, when): # Code to set dston and dstoff to the time zone's DST # transition times based on the input when.year, and expressed # in standard local time. if dston <= when.replace(tzinfo=None) < dstoff: return dt.timedelta(hours=1) else: return dt.timedelta(0)
ããã©ã«ãã®
dst()å®è£ ã¯NotImplementedErrorãéåºããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: DST ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- tzinfo.tzname(dt)¶
Return the time zone name corresponding to the
datetimeobject dt, as a string. Nothing about string names is defined by thedatetimemodule, and there's no requirement that it mean anything in particular. For example,"GMT","UTC","-500","-5:00","EDT","US/Eastern","America/New York"are all valid replies. ReturnNoneif a string name isn't known. Note that this is a method rather than a fixed string primarily because sometzinfosubclasses will wish to return different names depending on the specific value of dt passed, especially if thetzinfoclass is accounting for daylight time.ããã©ã«ãã®
tzname()å®è£ ã¯NotImplementedErrorãéåºããŸãã
These methods are called by a datetime or time object, in
response to their methods of the same names. A datetime object passes
itself as the argument, and a time object passes None as the
argument. A tzinfo subclass's methods should therefore be prepared to
accept a dt argument of None, or of class datetime.
None ãæž¡ãããå Žåãæè¯ã®å¿çæ¹æ³ã決ããã®ã¯ã¯ã©ã¹èšèšè
次第ã§ããäŸãã°ããã®ã¯ã©ã¹ã tzinfo ãããã³ã«ãšé¢ä¿ããããªããšããããšã衚æãããããã°ã None ãé©åã§ããæšæºæã®ãªãã»ãããèŠã€ããä»ã®ææ®µããªãå Žåã«ã¯ãæšæº UTC ãªãã»ãããè¿ãããã« utcoffset(None) ã䜿ããšãã£ãšäŸ¿å©ãããããŸããã
When a datetime object is passed in response to a datetime
method, dt.tzinfo is the same object as self. tzinfo methods can
rely on this, unless user code calls tzinfo methods directly. The
intent is that the tzinfo methods interpret dt as being in local
time, and not need worry about objects in other time zones.
ãµãã¯ã©ã¹ã§ãªãŒããŒã©ã€ããããšè¯ãããã 1 ã€ã® tzinfo ã®ã¡ãœããããããŸã:
- tzinfo.fromutc(dt)¶
ããã©ã«ãã® å®è£ ã§åŒã³åºãããŸãã
datetime.astimezone()ããåŒã°ããå Žåãdt.tzinfo㯠self ã§ããã dt ã®æ¥ä»ããã³æå»ããŒã¿ã¯ UTC æå»ã衚ããŠãããã®ãšããŠèŠããŸããfromutc()ã®ç®çã¯ã self ã®ããŒã«ã«æå»ã«çããdatetimeãªããžã§ã¯ããè¿ãããšã«ããæ¥ä»ãšæå»ããŒã¿ã¡ã³ããä¿®æ£ããããšã«ãããŸããã»ãšãã©ã®
tzinfoãµãã¯ã©ã¹ã§ã¯ããã©ã«ãã®fromutc()å®è£ ãåé¡ãªãç¶æ¿ã§ããŸããããã©ã«ãã®å®è£ ã¯ãåºå®ãªãã»ããã®ã¿ã€ã ãŸãŒã³ããæšæºæãšå€æéã®äž¡æ¹ã«ã€ããŠèšè¿°ããŠããã¿ã€ã ãŸãŒã³ããã㊠DST ç§»è¡æå»ã幎ã«ãã£ãŠç°ãªãå Žåã§ãããæ±ããããã匷åãªãã®ã§ããããã©ã«ãã®fromutc()å®è£ ãå šãŠã®å Žåã«å¯ŸããŠæ£ããæ±ãããšãã§ããªããããªäŸã¯ãæšæºæã® (UTCããã®) ãªãã»ãããåŒæ°ãšããŠæž¡ãããç¹å®ã®æ¥ãæå»ã«äŸåãããã®ã§ãããã¯æ¿æ²»çãªçç±ã«ãã£ãŠèµ·ããããšããããŸããããã©ã«ãã®astimezone()ãfromutc()ã®å®è£ ã¯ãçµæãæšæºæãªãã»ããã®å€åã«ãŸãããäœæéãã®äžã«ããå ŽåãæåŸ éãã®çµæãçæããªããããããŸããããšã©ãŒã®å Žåã®ããã®ã³ãŒããé€ããããã©ã«ãã®
fromutc()ã®å®è£ ã¯ä»¥äžã®ããã«åäœããŸã:import datetime as dt def fromutc(self, when): # raise ValueError error if when.tzinfo is not self dtoff = when.utcoffset() dtdst = when.dst() # raise ValueError if dtoff is None or dtdst is None delta = dtoff - dtdst # this is self's standard offset if delta: when += delta # convert to standard local time dtdst = when.dst() # raise ValueError if dtdst is None if dtdst: return when + dtdst else: return when
次㮠tzinfo_examples.py ãã¡ã€ã«ã«ã¯ã tzinfo ã¯ã©ã¹ã®äŸãããã€ãèŒã£ãŠããŸã:
import datetime as dt
# A class capturing the platform's idea of local time.
# (May result in wrong values on historical times in
# timezones where UTC offset and/or the DST rules had
# changed in the past.)
import time
ZERO = dt.timedelta(0)
HOUR = dt.timedelta(hours=1)
SECOND = dt.timedelta(seconds=1)
STDOFFSET = dt.timedelta(seconds=-time.timezone)
if time.daylight:
DSTOFFSET = dt.timedelta(seconds=-time.altzone)
else:
DSTOFFSET = STDOFFSET
DSTDIFF = DSTOFFSET - STDOFFSET
class LocalTimezone(dt.tzinfo):
def fromutc(self, when):
assert when.tzinfo is self
stamp = (when - dt.datetime(1970, 1, 1, tzinfo=self)) // SECOND
args = time.localtime(stamp)[:6]
dst_diff = DSTDIFF // SECOND
# Detect fold
fold = (args == time.localtime(stamp - dst_diff))
return dt.datetime(*args, microsecond=when.microsecond,
tzinfo=self, fold=fold)
def utcoffset(self, when):
if self._isdst(when):
return DSTOFFSET
else:
return STDOFFSET
def dst(self, when):
if self._isdst(when):
return DSTDIFF
else:
return ZERO
def tzname(self, when):
return time.tzname[self._isdst(when)]
def _isdst(self, when):
tt = (when.year, when.month, when.day,
when.hour, when.minute, when.second,
when.weekday(), 0, 0)
stamp = time.mktime(tt)
tt = time.localtime(stamp)
return tt.tm_isdst > 0
Local = LocalTimezone()
# A complete implementation of current DST rules for major US time zones.
def first_sunday_on_or_after(when):
days_to_go = 6 - when.weekday()
if days_to_go:
when += dt.timedelta(days_to_go)
return when
# US DST Rules
#
# This is a simplified (i.e., wrong for a few cases) set of rules for US
# DST start and end times. For a complete and up-to-date set of DST rules
# and timezone definitions, visit the Olson Database (or try pytz):
# http://www.twinsun.com/tz/tz-link.htm
# https://sourceforge.net/projects/pytz/ (might not be up-to-date)
#
# In the US, since 2007, DST starts at 2am (standard time) on the second
# Sunday in March, which is the first Sunday on or after Mar 8.
DSTSTART_2007 = dt.datetime(1, 3, 8, 2)
# and ends at 2am (DST time) on the first Sunday of Nov.
DSTEND_2007 = dt.datetime(1, 11, 1, 2)
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
# Sunday in April and to end at 2am (DST time) on the last
# Sunday of October, which is the first Sunday on or after Oct 25.
DSTSTART_1987_2006 = dt.datetime(1, 4, 1, 2)
DSTEND_1987_2006 = dt.datetime(1, 10, 25, 2)
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
# Sunday in April (the one on or after April 24) and to end at 2am (DST time)
# on the last Sunday of October, which is the first Sunday
# on or after Oct 25.
DSTSTART_1967_1986 = dt.datetime(1, 4, 24, 2)
DSTEND_1967_1986 = DSTEND_1987_2006
def us_dst_range(year):
# Find start and end times for US DST. For years before 1967, return
# start = end for no DST.
if 2006 < year:
dststart, dstend = DSTSTART_2007, DSTEND_2007
elif 1986 < year < 2007:
dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
elif 1966 < year < 1987:
dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
else:
return (dt.datetime(year, 1, 1), ) * 2
start = first_sunday_on_or_after(dststart.replace(year=year))
end = first_sunday_on_or_after(dstend.replace(year=year))
return start, end
class USTimeZone(dt.tzinfo):
def __init__(self, hours, reprname, stdname, dstname):
self.stdoffset = dt.timedelta(hours=hours)
self.reprname = reprname
self.stdname = stdname
self.dstname = dstname
def __repr__(self):
return self.reprname
def tzname(self, when):
if self.dst(when):
return self.dstname
else:
return self.stdname
def utcoffset(self, when):
return self.stdoffset + self.dst(when)
def dst(self, when):
if when is None or when.tzinfo is None:
# An exception may be sensible here, in one or both cases.
# It depends on how you want to treat them. The default
# fromutc() implementation (called by the default astimezone()
# implementation) passes a datetime with when.tzinfo is self.
return ZERO
assert when.tzinfo is self
start, end = us_dst_range(when.year)
# Can't compare naive to aware objects, so strip the timezone from
# when first.
when = when.replace(tzinfo=None)
if start + HOUR <= when < end - HOUR:
# DST is in effect.
return HOUR
if end - HOUR <= when < end:
# Fold (an ambiguous hour): use when.fold to disambiguate.
return ZERO if when.fold else HOUR
if start <= when < start + HOUR:
# Gap (a non-existent hour): reverse the fold rule.
return HOUR if when.fold else ZERO
# DST is off.
return ZERO
def fromutc(self, when):
assert when.tzinfo is self
start, end = us_dst_range(when.year)
start = start.replace(tzinfo=self)
end = end.replace(tzinfo=self)
std_time = when + self.stdoffset
dst_time = std_time + HOUR
if end <= dst_time < end + HOUR:
# Repeated hour
return std_time.replace(fold=1)
if std_time < start or dst_time >= end:
# Standard time
return std_time
if start <= std_time < end - HOUR:
# Daylight saving time
return dst_time
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
Central = USTimeZone(-6, "Central", "CST", "CDT")
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
æšæºæããã³å€æéã®äž¡æ¹ãèšè¿°ããŠãã tzinfo ã®ãµãã¯ã©ã¹ã§ã¯ã倿éã®ç§»è¡ã®ãšãã«ãåé¿äžèœã®é£è§£ãªåé¡ã幎㫠2 床ããã®ã§æ³šæããŠãã ããã
å
·äœçãªäŸãšããŠãæ±éšã¢ã¡ãªã«æå» (US Eastern, UTC -0500) ãèããŸãã
EDT 㯠3 æã®ç¬¬äºæ¥ææ¥ã® 1:59 (EST) ã® 1 ååŸã«éå§ãã11 æã®æåã®æ¥ææ¥ã® (EDTã®) 1:59 ã«çµäºããŸã:
UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM
EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM
EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM
end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM
DSTã®éå§ ("start" ã©ã€ã³) ã§ãããŒã«ã«ã®å®æé㯠1:59 ãã 3:00 ã«é£ã³ãŸãã
ãã®æ¥ã«ã¯ã 2:MM ãšãã圢åŒã®å®æéã¯æå³ããªããªãã®ã§ã DST ãå§ãŸã£ãæ¥ã« astimezone(Eastern) 㯠hour == 2 ãšãªãçµæãè¿ãããšã¯ãããŸããã
äŸãšããŠã 2016 å¹Žã®æ¥æ¹åã®ç§»è¡ã§ã¯ã次ã®ãããªçµæã«ãªããŸã:
>>> import datetime as dt
>>> from tzinfo_examples import HOUR, Eastern
>>> u0 = dt.datetime(2016, 3, 13, 5, tzinfo=dt.timezone.utc)
>>> for i in range(4):
... u = u0 + i*HOUR
... t = u.astimezone(Eastern)
... print(u.time(), 'UTC =', t.time(), t.tzname())
...
05:00:00 UTC = 00:00:00 EST
06:00:00 UTC = 01:00:00 EST
07:00:00 UTC = 03:00:00 EDT
08:00:00 UTC = 04:00:00 EDT
DST ãçµäº ("end" ã©ã€ã³) ã§ãæŽãªãåé¡ãæœãã§ããŸã: ããŒã«ã«ã®å®æéã§ãææ§ãç¡ãã«æãç¶Žããªã 1 æéãååšããŸã: ããã¯å€æéã®æåŸã® 1 æéã§ãã
æ±éšã§ã¯ã倿éãçµäºããæ¥ã® UTC ã§ã® 5:MM 圢åŒã®æéãããã§ãã
ããŒã«ã«ã®å®æé㯠(倿éã®) 1:59 ãã (æšæºæã®) 1:00 ã«åã³å·»ãæ»ãããŸãã
ããŒã«ã«ã®æå»ã«ããã 1:MM ã¯ææ§ã§ãã
ãã㊠astimezone() 㯠2 ã€ã®é£ãåã UTC æéãåãããŒã«ã«ã®æéã«å¯Ÿå¿ä»ããŠãããŒã«ã«ã®æèšã®æ¯ãèããç䌌ãŸãã
æ±éšã®äŸã§ã¯ã 5:MM ããã³ 6:MM ãšãã圢åŒã® UTC æå»ã¯äž¡æ¹ãšãæ±éšæå»ã«å€æãããéã« 1:MM ã«å¯Ÿå¿ä»ããããŸããããã以åã®æé㯠fold 屿§ã 0 ã«ãã以éã®æéã§ã¯ 1 ã«ããŸããäŸãã°ã 2016 幎ã§ã®ç§æ¹åã®ç§»è¡ã§ã¯ã次ã®ãããªçµæã«ãªããŸã:
>>> import datetime as dt
>>> from tzinfo_examples import HOUR, Eastern
>>> u0 = dt.datetime(2016, 11, 6, 4, tzinfo=dt.timezone.utc)
>>> for i in range(4):
... u = u0 + i*HOUR
... t = u.astimezone(Eastern)
... print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
...
04:00:00 UTC = 00:00:00 EDT 0
05:00:00 UTC = 01:00:00 EDT 0
06:00:00 UTC = 01:00:00 EST 1
07:00:00 UTC = 02:00:00 EST 0
fold 屿§ãç°ãªãã ãã® datetime ã€ã³ã¹ã¿ã³ã¹ã¯æ¯èŒã«ãããŠçãããšã¿ãªãããããšã«æ³šæããŠãã ããã
Applications that can't bear wall-time ambiguities should explicitly check the
value of the fold attribute or avoid using hybrid
tzinfo subclasses; there are no ambiguities when using timezone,
or any other fixed-offset tzinfo subclass (such as a class representing
only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).
åè
zoneinfo
datetimeã¢ãžã¥ãŒã«ã«ã¯ (UTC ããã®ä»»æã®åºå®ãªãã»ãããæ±ã) åºæ¬çãªtimezoneã¯ã©ã¹ãšã(UTCtimezoneã®ã€ã³ã¹ã¿ã³ã¹ã§ãã)timezone.utc屿§ããããŸãã
zoneinfo㯠Python ã« IANA ã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ (ãªã«ãœã³ããŒã¿ããŒã¹ãšããŠãç¥ãããŠããŸã) ãå°å ¥ãããã®ã§ãããã䜿ãããšãæšå¥šãããŠããŸãã
- IANA ã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹
(ãã°ãã° tzãtzdata ã zoneinfo ãšåŒã°ãã) ã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ã¯ã³ãŒããšããŒã¿ãä¿æããŠããããããã¯å°çå šäœã«ãããå€ãã®ä»£è¡šçãªå Žæã®ããŒã«ã«æå»ã®å±¥æŽã衚ããŠããŸããæ¿æ²»å£äœã«ããã¿ã€ã ãŸãŒã³ã®å¢çãUTC ãªãã»ããã倿éã®ã«ãŒã«ã®å€æŽãåæ ããããã宿çã«ããŒã¿ããŒã¹ãæŽæ°ãããŸãã
timezone objects¶
timezone ã¯ã©ã¹ã¯ tzinfo ã®ãµãã¯ã©ã¹ã§ãåã€ã³ã¹ã¿ã³ã¹ã¯ UTC ããã®åºå®ããããªãã»ããã§å®çŸ©ãããã¿ã€ã ãŸãŒã³ã衚ããŠããŸãã
ãã®ã¯ã©ã¹ã®ãªããžã§ã¯ãã¯ãäžå¹Žã®ãã¡ç°ãªãæ¥ã«ç°ãªããªãã»ããã䜿ãããŠããããåžžçšæ (civil time) ã«æŽå²çãªå€åãèµ·ããå Žæã®ã¿ã€ã ãŸãŒã³æ å ±ã衚ãã®ã«ã¯äœ¿ããªãã®ã§æ³šæããŠãã ããã
- class datetime.timezone(offset[, name])¶
ããŒã«ã«æå»ãš UTC ã®å·®åã衚ã
timedeltaãªããžã§ã¯ãã offset åŒæ°ã«æå®ããªããŠã¯ãããŸãããããã¯-timedelta(hours=24)ããtimedelta(hours=24)ãŸã§ã®äž¡ç«¯ãå«ãŸãªãç¯å²ã«åãŸã£ãŠããªããŠã¯ãªããŸãããããã§ãªãå ŽåValueErrorãéåºãããŸããname åŒæ°ã¯å¿ é ã§ã¯ãããŸãããããæå®ãããå Žåããã®å€ã¯
datetime.tzname()ã¡ãœããã®è¿ãå€ãšããŠäœ¿ãããæååã§ãªããã°ãªããŸãããAdded in version 3.2.
ããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- timezone.utcoffset(dt)¶
timezoneã€ã³ã¹ã¿ã³ã¹ãæ§ç¯ããããšãã«æå®ãããåºå®å€ãè¿ããŸããdt åŒæ°ã¯ç¡èŠãããŸãã è¿ãå€ã¯ãããŒã«ã«æå»ãš UTC ã®å·®åã«çãã
timedeltaã€ã³ã¹ã¿ã³ã¹ã§ããããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
- timezone.tzname(dt)¶
timezoneã€ã³ã¹ã¿ã³ã¹ãæ§ç¯ããããšãã«æå®ãããåºå®å€ãè¿ããŸããname ãæ§ç¯æã«äžããããªãã£ãå Žåã
tzname(dt)ã«ãã£ãŠè¿ããã name ã¯ä»¥äžã®æ§ã«offsetã®å€ããçæãããŸãã offset ãtimedelta(0)ã§ãã£ãå Žåã name 㯠"UTC"ã«ãªããŸãã ãã以å€ã®å Žåã 'UTC±HH:MM' ãšããæžåŒã®æååã«ãªãã± ã¯offsetããHH ãš MM ã¯ããããäºæ¡ã®offset.hoursãšoffset.minutesã衚çŸããŸããããŒãžã§ã³ 3.6 ã§å€æŽ:
offset=timedelta(0)ã«ãã£ãŠçæãããååã¯ãã¬ãŒã³ãª 'UTC' ã§ãã'UTC+00:00'ã§ã¯ãããŸããã
- timezone.dst(dt)¶
åžžã«
Noneãè¿ããŸãã
- timezone.fromutc(dt)¶
dt + offsetãè¿ããŸãã dt åŒæ°ã¯tzinfoãselfã«ãªã£ãŠãã aware ãªdatetimeã€ã³ã¹ã¿ã³ã¹ã§ãªããã°ãªããŸããã
以äžã«ã¯ã©ã¹å±æ§ã瀺ããŸã:
- timezone.utc¶
UTC ã¿ã€ã ãŸãŒã³
timezone(timedelta(0))ã§ãã
strftime() and strptime() behavior¶
date, datetime, time ãªããžã§ã¯ãã¯å
šãŠ strftime(format) ã¡ãœããããµããŒãããæå»ã衚çŸããæååãæç€ºçãªæžåŒæååã§çµ±å¶ããŠäœæããŠããŸãã
Conversely, the date.strptime(), datetime.strptime() and
time.strptime() class methods create an object from a string
representing the time and a corresponding format string.
äžã®è¡šã¯ strftime() ãš strptime() ãšã®é«ã¬ãã«ã®å¯Ÿæ¯ã衚ããŠããŸãã
|
|
|
|---|---|---|
äœ¿çšæ³ |
ãªããžã§ã¯ããäžããããæžåŒã«åŸã£ãŠæååã«å€æãã |
Parse a string into an object given a corresponding format |
ã¡ãœããã®çš®é¡ |
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã |
ã¯ã©ã¹ã¡ãœãã |
ã·ã°ãã㣠|
|
|
strftime() and strptime() format codes¶
These methods accept format codes that can be used to parse and format dates:
>>> import datetime as dt
>>> dt.datetime.strptime('31/01/22 23:59:59.999999',
... '%d/%m/%y %H:%M:%S.%f')
datetime.datetime(2022, 1, 31, 23, 59, 59, 999999)
>>> _.strftime('%a %d %b %Y, %I:%M%p')
'Mon 31 Jan 2022, 11:59PM'
The following is a list of all the format codes that the 2011 C standard requires, and these work on all supported platforms.
ãã£ã¬ã¯ãã£ã |
æå³ |
䜿çšäŸ |
泚é |
|---|---|---|---|
|
ãã±ãŒã«ã®ææ¥åãç瞮圢ã§è¡šç€ºããŸãã |
Sun, Mon, ..., Sat
(en_US);
So, Mo, ..., Sa
(de_DE)
|
(1) |
|
ãã±ãŒã«ã®ææ¥åã衚瀺ããŸãã |
Sunday, Monday, ...,
Saturday (en_US);
Sonntag, Montag, ...,
Samstag (de_DE)
|
(1) |
|
ãã±ãŒã«ã®æåãç瞮圢ã§è¡šç€ºããŸãã |
Jan, Feb, ..., Dec
(en_US);
Jan, Feb, ..., Dez
(de_DE)
|
(1) |
|
ãã±ãŒã«ã®æåã衚瀺ããŸãã |
January, February,
..., December (en_US);
Januar, Februar, ...,
Dezember (de_DE)
|
(1) |
|
ãã±ãŒã«ã®æ¥æãé©åãªåœ¢åŒã§è¡šããŸãã |
Tue Aug 16 21:30:00
1988 (en_US);
Di 16 Aug 21:30:00
1988 (de_DE)
|
(1) |
|
The year divided by 100 and truncated to an integer as a zero-padded decimal number. |
01, 02, ..., 99 |
(0) |
|
0åããã10鲿°ã§è¡šèšããæäžã®æ¥ã«ã¡ã |
01, 02, ..., 31 |
(9), (10) |
|
Equivalent to |
11/28/25 |
(9) |
|
The day of the month as a space-padded decimal number. |
â£1, â£2, ..., 31 |
(10) |
|
Equivalent to |
2025-10-11, 1001-12-30 |
|
|
Last 2 digits of ISO 8601 year
representing the year that
contains the greater part of
the ISO week ( |
00, 01, ..., 99 |
(0) |
|
ISO week( |
0001, 0002, ..., 2013, 2014, ..., 9998, 9999 |
(8) |
|
Equivalent to |
See |
(0) |
|
0åããã10鲿°ã§è¡šèšããæ (24æé衚èš)ã |
00, 01, ..., 23 |
(9) |
|
0åããã10鲿°ã§è¡šèšããæ (12æé衚èš)ã |
01, 02, ..., 12 |
(9) |
|
0åããã10鲿°ã§è¡šèšãã幎äžã®æ¥ã«ã¡ã |
001, 002, ..., 366 |
(9) |
|
0åããã10鲿°ã§è¡šèšããæã |
01, 02, ..., 12 |
(9) |
|
0åããã10鲿°ã§è¡šèšããåã |
00, 01, ..., 59 |
(9) |
|
The newline character
( |
|
|
|
ãã±ãŒã«ã® AM ããã㯠PM ãšçäŸ¡ãªæååã«ãªããŸãã |
AM, PM (en_US);
am, pm (de_DE)
|
(1), (3) |
|
Locale's 12-hour clock time. |
12:00:00 AM |
(1), (0) |
|
Equivalent to |
10:01 |
|
|
0åããã10鲿°ã§è¡šèšããç§ã |
00, 01, ..., 59 |
(4), (9) |
|
The tab character ( |
|
|
|
ISO 8601 time format,
equivalent to |
10:01:59 |
|
|
1 ãæææ¥ã衚ã 10鲿°è¡šèšã® ISO 8601 weekday ã§ãã |
1, 2, ..., 7 |
|
|
0åããã10鲿°ã§è¡šèšãã幎äžã®é±çªå· (é±ã®å§ãŸãã¯æ¥ææ¥ãšãã)ãæ°å¹Žã®æåã®æ¥ææ¥ã«å ç«ã€æ¥ã¯ 0é±ã«å±ãããšããŸãã |
00, 01, ..., 53 |
(7), (9) |
|
é±ã§æåã®æææ¥ãå§ããšãã ISO 8601 week ã§ããWeek 01 㯠1æ4æ¥ãå«ã¿ãŸãã |
01, 02, ..., 53 |
(8), (9) |
|
ææ¥ã10é²è¡šèšããæååã衚瀺ããŸãã0 ãæ¥ææ¥ã§ã6 ãåææ¥ã衚ããŸãã |
0, 1, ..., 6 |
|
|
0åããã10鲿°ã§è¡šèšãã幎äžã®é±çªå· (é±ã®å§ãŸãã¯æææ¥ãšãã)ãæ°å¹Žã®æåã®æææ¥ã«å ç«ã€æ¥ã¯ 0é±ã«å±ãããšããŸãã |
00, 01, ..., 53 |
(7), (9) |
|
ãã±ãŒã«ã®æ¥ä»ãé©åãªåœ¢åŒã§è¡šããŸãã |
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
|
(1) |
|
ãã±ãŒã«ã®æéãé©åãªåœ¢åŒã§è¡šããŸãã |
21:30:00 (en_US);
21:30:00 (de_DE)
|
(1) |
|
0åããã10鲿°ã§è¡šèšããäžçŽç¡ãã®å¹Žã |
00, 01, ..., 99 |
(9) |
|
è¥¿æŠ ( 4æ¡) ã® 10 é²è¡šèšã衚ããŸãã |
0001, 0002, ..., 2013, 2014, ..., 9998, 9999 |
(2) |
|
UTCãªãã»ããã |
(空æåå), +0000, -0400, +1030, +063415, -030712.345216 |
(6) |
|
ã¿ã€ã ãŸãŒã³ã®ååã衚瀺ããŸã (ãªããžã§ã¯ããnaiveã§ããã°ç©ºæåå)ã |
(空æåå), UTC, GMT |
(6) |
|
æå |
% |
The ISO 8601 year and ISO 8601 week directives are not interchangeable
with the year and week number directives above. Calling strptime() with
incomplete or ambiguous ISO 8601 directives will raise a ValueError.
Several additional directives not required by the C11 standard are included for convenience.
ãã£ã¬ã¯ãã£ã |
æå³ |
䜿çšäŸ |
泚é |
|---|---|---|---|
|
10鲿°ã§è¡šèšãããã€ã¯ãç§ (6æ¡ã«0åããããŸã)ã |
000000, 000001, ..., 999999 |
(5) |
|
UTCãªãã»ããã |
(空æåå), +00:00, -04:00, +10:30, +06:34:15, -03:07:12.345216 |
(6) |
Python ã¯ãã©ãããã©ãŒã ã® C ã©ã€ãã©ãªã® strftime() 颿°ãåŒã³åºããŠããŠããã©ãããã©ãŒã ããšã«ãã®å®è£
ãç°ãªãã®ã¯ããããããšãªã®ã§ããµããŒããããæžåŒã³ãŒãå
šäœã¯ãã©ãããã©ãŒã ããšã«æ§ã
ã§ãã
æå
ã®ãã©ãããã©ãŒã ã§ãµããŒããããŠãããã©ãŒãããèšå·å
šäœãèŠãã«ã¯ã strftime(3) ã®ããã¥ã¡ã³ããåç
§ããŠãã ããã
ãµããŒããããŠããªããã©ãŒãããæå®åã®æ±ãããã©ãããã©ãŒã éã§å·®ç°ããããŸãã
Added in version 3.6: %G, %u ããã³ %V ã远å ãããŸããã
Added in version 3.12: %:z was added for strftime().
Added in version 3.15: %D, %F, %n, %t, and %:z were added for
strptime().
Technical detail¶
倧éæã«ãããšã d.strftime(fmt) 㯠time ã¢ãžã¥ãŒã«ã® time.strftime(fmt, d.timetuple()) ã®ããã«åäœããŸãããã ãå
šãŠã®ãªããžã§ã¯ãã timetuple() ã¡ãœããããµããŒãããŠããããã§ã¯ãããŸããã
For the datetime.strptime() and date.strptime() class methods,
the default value is 1900-01-01T00:00:00.000: any components not specified
in the format string will be pulled from the default value.
泚é
Format strings without separators can be ambiguous for parsing. For
example, with %Y%m%d, the string 2026111 may be parsed either as
2026-11-01 or as 2026-01-11.
Use separators to ensure the input is parsed as intended.
泚é
When used to parse partial dates lacking a year, datetime.strptime()
and date.strptime() will raise when encountering February 29 because
the default year of 1900 is not a leap year. Always add a default leap
year to partial date strings before parsing.
>>> import datetime as dt
>>> value = "2/29"
>>> dt.datetime.strptime(value, "%m/%d")
Traceback (most recent call last):
...
ValueError: day 29 must be in range 1..28 for month 2 in year 1900
>>> dt.datetime.strptime(f"1904 {value}", "%Y %m/%d")
datetime.datetime(1904, 2, 29, 0, 0)
datetime.strptime(date_string, format) ã¯æ¬¡ã®åŒãšç䟡ã§ã:
datetime(*(time.strptime(date_string, format)[0:6]))
ãã ãã datetime.strptime ã¯ãµããŒãããŠããã time.strptime ã«ã¯ç¡ããç§æªæºã®åäœãã¿ã€ã ãŸãŒã³ãªãã»ããã®æ
å ±ã format ã« å«ãŸããŠãããšãã¯é€ããŸãã
time ãªããžã§ã¯ãã«ã¯ã幎ãæãæ¥ã®å€ããªããããããããæžåŒã³ãŒãã䜿ãããšãã§ããŸããã
ç¡çç¢ç䜿ã£ãå Žåã幎ã¯1900ã«çœ®ãæããããæãšæ¥ã¯1ã«çœ®ãæããããŸãã
date ãªããžã§ã¯ãã«ã¯ãæãåãç§ããã€ã¯ãç§ã®å€ããªãããããããã®æžåŒã³ãŒãã䜿ãããšãã§ããŸããã
ç¡çç¢ç䜿ã£ãå Žåããããã®å€ã¯0ã«çœ®ãæããããŸãã
åãçç±ã§ãçŸåšã®ãã±ãŒã«ã®æåéåã§è¡šçŸã§ããªã Unicode ã³ãŒããã€ã³ããå«ãæžåŒæååã®å¯ŸåŠããã©ãããã©ãŒã äŸåã§ãã
ãããã©ãããã©ãŒã ã§ã¯ãããã£ãã³ãŒããã€ã³ãã¯ãã®ãŸãŸåºåã«åºãããäžæ¹ãä»ã®ãã©ãããã©ãŒã ã§ã¯ strftime ã UnicodeError ãéåºãããããã®ä»£ããã«ç©ºæååãè¿ããããããããããŸããã
泚é:
This format code is currently unsupported by
strptime().Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, "month/day/year" versus "day/month/year"), and the output may contain non-ASCII characters.
strptime()ã¡ãœãã㯠[1, 9999] ã®ç¯å²ã®å¹Žæ°å šãŠãæ§æè§£æã§ããŸããã year < 1000 ã®ç¯å²ã®å¹Žæ°ã¯ 0 åãããã 4 æ¡ã®æ°åã§ãªããã°ãªããŸãããããŒãžã§ã³ 3.2 ã§å€æŽ: 以åã®ããŒãžã§ã³ã§ã¯ã
strftime()ã¡ãœãã㯠years >= 1900 ã®ç¯å²ã®å¹Žæ°ããæ±ããŸããã§ãããããŒãžã§ã³ 3.3 ã§å€æŽ: ããŒãžã§ã³ 3.2 ã§ã¯ã
strftime()ã¡ãœãã㯠years >= 1000 ã®ç¯å²ã®å¹Žæ°ããæ±ããŸããã§ãããstrptime()ã¡ãœãããšå ±ã«äœ¿ãããå Žåã%pæå®åã¯åºåã®æéãã£ãŒã«ãã®ã¿ã«åœ±é¿ãã%Iæå®åã䜿ããããã®ããã«æ¯ãèããŸããtimeã¢ãžã¥ãŒã«ãšéããdatetimeã¢ãžã¥ãŒã«ã¯ãããç§ããµããŒãããŠããŸãããstrptime()ã¡ãœãããšå ±ã«äœ¿ãããå Žåã%fæå®å㯠1 æ¡ãã 6 æ¡ã®æ°åãåãä»ããå³åŽãã0åããããŸãã%f㯠C æšæºèŠæ Œã®æžåŒæåã»ããã®æ¡åŒµã§ã (ãšã¯èšãã datetime ã¢ãžã¥ãŒã«ã®ãªããžã§ã¯ãããããã«å®è£ ãããŠããã®ã§ãã©ãã§ã§ã䜿ããŸã)ãnaive ãªããžã§ã¯ãã§ã¯ãæžåŒã³ãŒã
%zã%:zããã³%Zã¯ç©ºæååã«çœ®ãæããããŸããaware ãªããžã§ã¯ãã§ã¯æ¬¡ã®ããã«ãªããŸã:
%zutcoffset()ã¯Â±HHMM[SS[.ffffff]]圢åŒã®æååã«å€æãããŸããããã§ãHH㯠UTC ãªãã»ããã®æéã衚ã 2 æ¡ã®æååãMM㯠UTC ãªãã»ããã®åæ°ã衚ã 2 æ¡ã®æååãSS㯠UTC ãªãã»ããã®ç§æ°ã衚ã 2 æ¡ã®æååãffffff㯠UTC ãªãã»ããã®ãã€ã¯ãç§æ°ã衚ã 6 æ¡ã®æååã§ãã ãªãã»ããã«ç§æªæºã®ç«¯æ°ãç¡ããšãã¯fffffféšåã¯çç¥ããããªãã»ããã«åæªæºã®ç«¯æ°ãç¡ããšãã¯fffffféšåãSSéšåãçç¥ãããŸãã äŸãã°ãutcoffset()ãtimedelta(hours=-3, minutes=-30)ãè¿ãå Žåã%zã¯æåå'-0330'ã«çœ®ãæããããŸãã
ããŒãžã§ã³ 3.7 ã§å€æŽ: UTC ãªãã»ãããååäœã§ãªããã°ãªããªãå¶éãç¡ããªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: When the
%zdirective is provided to thestrptime()method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, both'+010000'and'+01:00:00'will be parsed as an offset of one hour. In addition, providing'Z'is identical to'+00:00'.%:zWhen used with
strftime(), behaves exactly as%z, except that a colon separator is added between hours, minutes and seconds.When used with
strptime(), the UTC offset is required to have a colon as a separator between hours, minutes and seconds. For example,'+01:00:00'(but not'+010000') will be parsed as an offset of one hour. In addition, providing'Z'is identical to'+00:00'.%ZIn
strftime(),%Zis replaced by an empty string iftzname()returnsNone; otherwise%Zis replaced by the returned value, which must be a string.strptime()ã¯%Zã«ç¹å®ã®å€ã®ã¿ãåãå ¥ããŸã:䜿çšããŠãããã·ã³ã®ãã±ãŒã«ã«ãã
time.tznameã®ä»»æã®å€ããŒãã³ãŒããããå€
UTCãŸãã¯GMT
ã€ãŸããæ¥æ¬ã«äœãã§ããå Žåã¯
JST,UTCãšGMTãæå¹ãªå€ã§ãããESTã¯ããããç¡å¹ãªå€ãšãªããŸããç¡å¹ãªå€ã®å Žåã¯ValueErrorãéåºããŸãã
ããŒãžã§ã³ 3.2 ã§å€æŽ:
%zæå®åãstrptime()ã¡ãœããã«äžããããå Žåã aware ãªdatetimeãªããžã§ã¯ããäœæãããŸããè¿ãå€ã®tzinfoã¯timezoneã€ã³ã¹ã¿ã³ã¹ã«ãªã£ãŠããŸããstrptime()ã¡ãœãããšå ±ã«äœ¿ãããå Žåã%Uãš%Wæå®åã¯ãææ¥ãšå¹Ž(%Y)ãæå®ãããå Žåã®èšç®ã§ã®ã¿äœ¿ãããŸãã%Uããã³%Wãšåæ§ã«ã%Vã¯ææ¥ãš ISO 幎 (%G) ãstrptime()ã®æžåŒæååã®äžã§æå®ãããå Žåã«èšç®ã§ã®ã¿äœ¿ãããŸãã%Gãš%Yã¯äºãã«å®å šãªäºææ§ãæããªãããšã«ã泚æããŠãã ãããstrptime()ã¡ãœãããšå ±ã«äœ¿ããããšããæžåŒ%d,%m,%H,%I,%M,%S,%j,%U,%W,%Vã§ã¯å è¡ãŒãã¯ä»»æã§ãã æžåŒ%yã§ã¯å è¡ãŒãã¯å¿ é ã§ããWhen parsing a month and day using
strptime(), always include a year in the format. If the value you need to parse lacks a year, append an explicit dummy leap year. Otherwise your code will raise an exception when it encounters leap day because the default year used by the parser (1900) is not a leap year. Users run into that bug every leap year.>>> month_day = "02/29" >>> dt.datetime.strptime(f"{month_day};1984", "%m/%d;%Y") # No leap year bug. datetime.datetime(1984, 2, 29, 0, 0)
ããŒãžã§ã³ 3.15 ã§å€æŽ: Using
%dwithout a year now raisesValueError.ããŒãžã§ã³ 3.15 ã§éæšå¥šãããŒãžã§ã³ 3.17 ã§åé€äºå®:
strptime()calls using a format string containing%ewithout a year now emit aDeprecationWarning.
èæ³š