Karteikarten
Python 3.8 Datetimes
Karten in diesem Stapel (44)
- 1ConstantsMINYEAR == 1
MAXYEAR == 9999 - 2Datime Available Typesdate
time
datetime
timedelta
tzinfo
timezone - 3Type: dateConcept: idealized and naive date, assume current Gregorian calendar always was, and always will be.
Attributes: day, month, year - 4Type: timeConcept: idealized time: assume every day has exactly 24*60*60 seconds. No leap seconds here.
Attributes: hour, minute, seconds, microsecond & tzinfo. - 5Type: datetimeCombination of date & time. It has all respective attrs
- 6Type: timedeltaExpress difference between two date, time or datetime instances to microsecond resolution.
- 7Type: tzinfoUn abstract base class for time zone information objects. These are used by datetime and time classes to provide a customizable notion of time adjustment.
- 8Type: timezoneA class that implements the tzinfo abstract base class as a fixed offset from UTC
- 9Subclass relationshipsobject
timedelta
tzinfo
timezone
time
date
datetime - 10Common propertiesDate, datetime, time & timezone share:
-Are immutables
-Are hashable, can be used as dict keys
- Support efficient pickling via pickle module - 11Determining if an obj is Aware or NaiveDate is always Naive
Datetime/time may be aware or naive
Datetime is aware if
d.tzinfo != None
d.tzinfo.utcoffset(d) != None
time is aware if
t.tzinfo != None
t.tzinfo.utcoffset(None) != None - 12timedelta. Concept and signatureRepresents a duration. Diff between two dates or times
timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) - 13timedelta. ArgumentsIntegers or float, positives or negatives. All optionals with default=0
- 14timedelta. Internal storage and calculationOnly days, seconds and microseconds are stored internally.
millisecond -> 1000 microseconds
minute -> 60 seconds
hour -> 3600 seconds
week -> 7 days
Days, seconds and microsecs are normalized - 15timedelta. attrsmin: -999999999 days
max: 999999999 days, 23 hours, 59 minutes, 59 seconds and 999999 microseconds.
resolution: 1 microseconds - 16timedelta. supported operations. Add, substract, productt1 = t2 + t3
t1 = t2 - t3
t1 = t2 * i or i * t2
t1 = t2 * f or f * t2 - 17timedelta. supported operations. Divisionf = t2 / t3
t1 = t2 / f or t2 / i
t1 = t2 // i or t2 // t3 - 18timedelta. supported operations. % & divmodt1 = t2 % t3
q, r = divmod(t1, t2)
q is int
r is timedelta - 19timedelta. supported operations. str(t)Returns a string in the form
[ D day[s], [H]H:MM:SS[.UUUUUU] ] where D is negative for negative t - 20timedelta. Comparisons== or != always return a bool
> or < could raise an exception if we try to compare different types
timedelta(0) == False, other timedeltas are always True - 21timedelta. Instance methodstotal_seconds() equivalent to td / timedelta(seconds=1)
For large intervals (>270 years) this method lose microsecond accuracy - 22date objectsuses an idealized calendar (gregorian) extending indefinitely in both directions.
- 23date objs. Constructiondate(year, month, day) -> all args required, all integers
MINYEAR <= year <= MAXYEAR
1 <= month <= 12
1 <= day <= days in the given month and year - 24date obj. Other constructors: fromtimestamp()today() == date.fromtimestamp(time.time())
fromtimestamp(tmstp)
years between 1970 & 2038 - 25date obj. Other constructors: fromordinal(ordinal)fromordinal(ordinal)
date corresponding to proleptic Gregorian calendar.
1 <= ordinal <= date.max.toordinal()
For any date d: date.fromordinal(d.toordinal())) == d - 26date obj. fromisoformat(date_str)returns a date corresponding to a date_string given in the format YYYY-MM-DD.
Inverse of isoformat()
New in version 3.7 - 27date obj. fromisocalendarNew in v3.8
fromisocalendar(year, week, day)
inverse of date.isocalendar() - 28date obj. class attrsmin: date(MINYEAR, 1, 1)
max: date(MAXYEAR, 12, 31)
resolution: smallest diff between two non-equal objs, timedelta(days=1) - 29date obj. operationsdate2 = date1 + timedelta
date2 = date1 - timedelta
timedelta = date1 - date2
date1 < date2
all dates are true - 30date obj. inst methods: replacereplace(year, month, day)
returns a date with the same value except for those parameters fiven - 31date obj. inst methods: timetupleReturns a time.struct_time
- 32date obj. inst methods: toordinalReturns the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1.
date.fromordinal(d.toordinal()) == d - 33date obj. inst methods: weekdayReturns the day of the week as an integer. Monday is 0 and Sunday is 6.
- 34date obj. inst methods: isoweekdayReturns the day of the week as an integer, where Monday is 0.
- 35date obj. inst method: isocalendar()Returns a 3-tuple (ISO year, ISO week number, ISO weekday)
ISO year: 52 or 53 weeks, weeks begin on monday. - 36date obj. inst method: isoformat()Return a string representing the date in ISO 8601.
str(d) == d.isoformat() - 37date obj. inst method: ctime()Returns a string representing the date.
'Wed Dec 4 00:00:00 2002'
Equivalent to time.ctime(time.mktime(d.timetuple()))
on platforms where native C ctime invokes (time.ctime()) - 38date obj. inst method: strftime(format)Returns a string representing the date. Hours, minutes and seconds will see 0 values.
Equivalent to __format__ - 39Datetime objectsLike a date object, assumes the curren Gregorian calendar extended in both directions
- 40Datetime constructorsdatetime(year, month, day, hour=0, minute=0, second=0, microsencond=0, tzinfo=None, *, fold = 0)
tzinfo may be None or an instance of a tzinfo subclass
fold in [0, 1] - 41datetime. Class method: today()return the current local datetime with tzinfo==None
if tzinfo==None is equivalent to datetime.now() - 42datetime. Class method: now()like today but receives tzinfo. More precision using time.time() timestamp
Prefered over today() and utcnow() - 43datetime. Class method: utcnow()It's like now() but returns the current UTC date and time as a naive datetime object. An aware current UTC datetime can be obtained by calling datetime.now(timezone.utc)
- 44datetime. Class method: fromtimestamp()fromtimestamp(timestamp, tz=None)
Return the local date and time corresponding to the POSIX timestamp
Informationen
- Beschreibung
- Karteikarten am Python 3.8 Datetimes, erstellt von damian rott am 31/08/2020.
- Erstellt von
- damian rott
- Aufrufe
- 0
- Bewertung