Differences between revisions 2 and 8 (spanning 6 versions)
Revision 2 as of 2023-04-11 18:12:49
Size: 2106
Comment:
Revision 8 as of 2023-04-11 19:35:07
Size: 2692
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
== TimeDelta == == Date ==
Line 11: Line 11:
A `datetime.timedelta` object stores some duration of time as a tuple of days, seconds, and milliseconds. See [[Python/Datetime/Date|here]].
Line 17: Line 17:
=== Operations === == Datetime ==
Line 19: Line 19:
`datetime.timedelta` objects can be used with the following [[Python/Builtins/Operators|operators]] and [[Python/Builtins/Functions|functions]].

||'''Operation''' ||'''Meaning''' ||
||`t1 + t2` ||sum ||
||`t1 - t2` ||difference ||
||`t1 * i` ||multiplication by integer ||
||`t1 * f` ||multiplication by float rounded to nearest even microsecond ||
||`t1 / t2` ||division of `t1` in terms of `t2`, returning a float ||
||`t1 / n` ||division by a number rounded to nearest even microsecond ||
||`t1 // i` ||floored division by an integer ||
||`t1 // t2` ||floored division of `t1` in terms of `t2`, returning an integer||
||`t1 % t2` ||the remainder of `t1` from `t1 // t2` ||
||`q, r = divmod(t1, t2)`||`q = t1 // t2` and `r = t1 % t2` ||
||`-t1` ||`datetime.timedelta(-t1.days, -t1.seconds, -t1.microseconds)` ||
||`abs(t1)` ||`t` if t >= 0 else -t`` ||
||`str(t1)` ||returns a string like `[D day[s], ][H]H:MM:SS[.UUUUUU]` ||
||`repr(t1)` ||returns a string constructor call ||

Some of these operations are not safe from overflows.
See [[Python/Datetime/Datetime|here]].
Line 43: Line 25:
=== Total_Seconds === == Time ==
Line 45: Line 27:
Convert the stored days, seconds, and milliseconds into just seconds. See [[Python/Datetime/Time|here]].
Line 47: Line 29:
Equivalent to: ----
Line 49: Line 31:
{{{
import datetime
Line 52: Line 32:
t1 = datetime.timedelta(days=3)
Line 54: Line 33:
seconds = t1 / datetime.timedelta(seconds=1)
}}}
== TimeDelta ==

See [[Python/Datetime/TimeDelta|here]].

----



== TimeZone ==

A `datetime.timezone` represents a fixed offset from UTC.

To access the UTC timezone, use the class attribute `datetime.timezone.utc`.

----



== TZInfo ==

An abstract base class for `datetime.timezone`. Not to be used directly.

----



== StrFTime and StrPTime ==


||'''Directive'''||'''Meaning'''||'''Example Values'''||
||`%a`||weekday in locale abbreviated name||Sun||
||`%A`||weekday in locale full name||Sunday||
||`%w`||weekday as an integer||0 ''(as Sunday)'', 6 ''(as Saturday)''||
||`%d`||day of the month as a zero-padded integer||01||
||`%b`||month in locale abbreviated name||Jan||
||`%B`||month in locale full name||January||
||`%m`||month as a zero-padded integer||01||
||`%y`||year as a zero-padded two-digit integer||00||
||`%Y`||year as a zero-padded four-digit integer||0001||
||`%H`||24-hour clock hour as a zero-padded integer||00||
||`%I`||12-hour clock hour as a zero-padded integer||01||
||`%p`||locale equivalent of AM/PM.||AM||
||`%M`||minute as a zero-padded integer||00||
||`%S`||second as a zero-padded integer||00||
||`%f`||microsecond as a zero-padded six-digit integer||000000||
||`%z`||UTC offset in the form ±HHMM[SS[.ffffff]], or empty if the object is naive||+0000, -0400, +1030, +063415, -030712.345216||
||`%Z`||time zone name, or empty if the object is naive||UTC||
||`%j`||day of the year as a zero-padded integer||001||
||`%U`||week of the year as a zero-padded two-digit integer; all days preceding the first Sunday are in week 0||00||
||`%W`||week of the year as a zero-padded two-digit integer; all days preceding the first Monday are in week 0||00||
||`%c`||locale datetime representation||Tue Aug 16 21:30:00 1988||
||`%x`||locale date representation||08/16/1988||
||`%X`||locale time representation||21:30:00||
||`%%`||literal %|| ||
||`%G`||year that contains the greater part of the ISO week (`%V`) zero-padded four-digit integer||0001||
||`%u`||weekday as an integer||1 ''(as Monday)'', 7 ''(as Sunday)''||
||`%V`||week of the year as a zero-padded two-digit number; week 01 is the week beginning on a Monday that contains January 4th; all preceding days belong to the prior year||01||

----



== See also ==

[[https://docs.python.org/3/library/datetime.html|Python datetime module documentation]]

Python Datetime


Date

See here.


Datetime

See here.


Time

See here.


TimeDelta

See here.


TimeZone

A datetime.timezone represents a fixed offset from UTC.

To access the UTC timezone, use the class attribute datetime.timezone.utc.


TZInfo

An abstract base class for datetime.timezone. Not to be used directly.


StrFTime and StrPTime

Directive

Meaning

Example Values

%a

weekday in locale abbreviated name

Sun

%A

weekday in locale full name

Sunday

%w

weekday as an integer

0 (as Sunday), 6 (as Saturday)

%d

day of the month as a zero-padded integer

01

%b

month in locale abbreviated name

Jan

%B

month in locale full name

January

%m

month as a zero-padded integer

01

%y

year as a zero-padded two-digit integer

00

%Y

year as a zero-padded four-digit integer

0001

%H

24-hour clock hour as a zero-padded integer

00

%I

12-hour clock hour as a zero-padded integer

01

%p

locale equivalent of AM/PM.

AM

%M

minute as a zero-padded integer

00

%S

second as a zero-padded integer

00

%f

microsecond as a zero-padded six-digit integer

000000

%z

UTC offset in the form ±HHMM[SS[.ffffff]], or empty if the object is naive

+0000, -0400, +1030, +063415, -030712.345216

%Z

time zone name, or empty if the object is naive

UTC

%j

day of the year as a zero-padded integer

001

%U

week of the year as a zero-padded two-digit integer; all days preceding the first Sunday are in week 0

00

%W

week of the year as a zero-padded two-digit integer; all days preceding the first Monday are in week 0

00

%c

locale datetime representation

Tue Aug 16 21:30:00 1988

%x

locale date representation

08/16/1988

%X

locale time representation

21:30:00

%%

literal %

%G

year that contains the greater part of the ISO week (%V) zero-padded four-digit integer

0001

%u

weekday as an integer

1 (as Monday), 7 (as Sunday)

%V

week of the year as a zero-padded two-digit number; week 01 is the week beginning on a Monday that contains January 4th; all preceding days belong to the prior year

01


See also

Python datetime module documentation


CategoryRicottone

Python/Datetime (last edited 2023-06-15 18:29:46 by DominicRicottone)