Package: attotime
attotimeHigh precision datetime implementation for PythonFeaturesFractional nanosecond resolution using PythondecimalmoduleAPI as close to Python’s nativedatetimeimplementation as possibleattotimedeltaattotimedeltaobjects represent the difference between two dates or times. It wraps a nativetimedeltaobject, and stores fractional nanoseconds as aDecimal.classattotime.attotimedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks[, nanoseconds]]]]]]]])All arguments are optional and default to 0. All arguments may be ints, floats, orDecimal, and may be positive or negative.Onlydays,seconds,microseconds, andnanosecondsare stored internally.days,seconds, andmicrosecondsare stored in a nativetimedeltaobject,nanosecondsare contained in aDecimal.Instance attributes (read-only)daysBetween -999999999 and 999999999 inclusive.secondsBetween 0 and 86399 inclusive.microsecondsBetween 0 and 999999 inclusive.nanosecondsADecimalbetween 0 and 999 inclusive.Supported operationstd1 = td2 + td3Sum of td2 and td3.td1 = td2 - td3Difference of td2 and td3.td1 = i * td2ortd1 = td2 * iDelta multiplied by an integer, float, orDecimal.td1 = td2 // iComputes the floor, discarding the remainder.+td1Returns anattotimedeltawith the same value.-td1Equivalent totd1 * -1.abs(td1)Equivalent to+td1whentd1.days >= 0,-td1whent1.days < 0.str(td1)Returns a string in the form[D day[s], ][H]H:MM:SS[.UUUUUU], whereDis negative fortd1 < 0andUUUUUUcan be expanded for up to 16 place fixed point.repr(td1)Returns a string in the formattotime.objects.attotimedelta(D[, S[, U]]), whereDis negative fortd1 < 0.Instance methodsattotimedelta.total_seconds()Return the total number of seconds contained in the duration as aDecimal.attotimedelta.total_nanoseconds()Return the total number of nanoseconds contained in the duration as aDecimal.attodatetimeattodatetimeis a single object wrapping a nativedateobject and anattotimeobject for the purposes of storing date and time information with fractional nanoseconds stored as aDecimal.classattotime.attodatetime(year, month, day[, hour[, minute[, second[, microsecond[, nanosecond[, tzinfo]]]]]])Year, month, and day are required.tzinfomay beNone, or an instance of atzinfo subclass. The nanosecond argument may be a float orDecimal. The remaining arguments must be ints.Class methodsattodatetime.today()Return the current local datetime, withtzinfoNone. This is equivalent toattodatetime.fromtimestamp(time.time()).attodatetime.now([tz])Return the current local date and time. If optional argumenttzisNonethis is liketoday().Iftzis notNone, it must be an instance of a tzinfo subclass, and the current date and time are converted totz’s time zone.attodatetime.utcnow()Return the current UTC date and time, withtzinfoNone.attodatetime.fromtimestamp(timestamp, [tz])Return the local date and time corresponding to the POSIX timestamp, such as returned bytime.time(). If optional argumenttzisNoneor not specified, the timestamp is converted to the platform’s local date and time, and the returnedattodatetimeobject is naive.Iftzis notNone, it must be an instance of atzinfosubclass, and the timestamp is converted totz’s time zone. The returnedattodatetime’stzinfois set to the providedtz.attodatetime.utcfromtimestamp(timestamp)Return the UTCattodatetimecorresponding to the POSIX timestamp, withtzinfoNone.attodatetime.fromordinal(ordinal)Return anattodatetimecorresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1.ValueErroris raised unless 1 <= ordinal <=datetime.max.toordinal()(note native Pythondatetimerange checking). The hour, minute, second and microsecond of the result are all 0, andtzinfoisNone.attodatetime.combine(date, time)Return anattodatetimeobject whose date components are equal to the given date object’s, and whose time components andtzinfoattributes are equal to the given time object’s. If date is aattodatetime(or native Pythondatetime), its time components andtzinfoattributes are ignored.attodatetime.strptime(date_string, format)Return anattodatetimecorresponding todate_string, parsed according toformat. Only the directives explicitly listed in thestrftime() and strptime() Behaviorsection of the Python documentation are supported, as well as the following:DirectiveMeaningExample%oPicosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999%qAttosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999%vYoctosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999Instance attributes (read-only)yearBetween Python nativedatetimeMINYEARandMAXYEAR, inclusive.monthBetween 1 and 12 inclusive.dayBetween 1 and the number of days in the given month of the given year.hourInrange(24).minuteInrange(60).secondInrange(60).microsecondInrange(1000000).nanosecondInrange(1000), asDecimal.tzinfoThe object passed as thetzinfoargument to theattodatetimeconstructor, orNoneif none was passed.Supported operationsdt2 = dt1 + tddt1 moved forward the duration of theattotimedeltaifattotimedelta.days > 0, or backward ifattotimedelta.days < 0.dt2 = dt1 - tddt1 moved backward the duration of theattotimedeltaifattotimedelta.days > 0, or forward ifattotimedelta.days < 0.td = dt1 - dt2The duration of time between dt1 and dt2, as anattotimedelta.dt1 < dt2dt1 is considered less than dt2 if dt1 precedes dt2 in time.str(dt1)Equivalent todt1.isoformat(separator=' ').repr(dt1)Returns a string in the formattotime.objects.attodatetime(Y, M, D, h, m, s, us, ns, [tz]).Instance methodsattodatetime.date()Return adateobject with same year, month and day.attodatetime.time()Return anattotimeobject with the same hour, minute, second, microsecond, and nanosecond.tzinfoisNone.attodatetime.timetz()Return anattotimeobject with the same hour, minute, second, microsecond, nanosecond, andtzinfoattributes.attodatetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, nanosecond[, tzinfo]]]]]]]]])Return anattodatetimeobject with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note thattzinfo=Nonecan be specified to create a naiveattodatetimefrom an awareattodatetimewith no conversion of date and time data.attodatetime.astimezone(tz)Return anattodatetimeobject with newtzinfoattributetz, adjusting the date and time data so the result is the same UTC time as self, but intz’s local time.AValueErroris raised ifselfis naive.attodatetime.utcoffset()IftzinfoisNone, returnsNone, else returnself.tzinfo.utcoffset(self)as anattotimedelta.attodatetime.dst()IftzinfoisNone, returnsNone, else returnself.tzinfo.dst(self)as anattotimedelta.attodatetime.tzname()IftzinfoisNone, returnsNone, else returnsself.tzinfo.tzname(self).attodatetime.timetuple()Return the result ofdatetime.timetuple()for a native Pythondatetimematching theattodatetime. Nanosecond precision is lost.attodatetime.utctimetuple()Return the result ofdatetime.utctimetuple()for a native Pythondatetimematching theattodatetime. Nanosecond precision is lost.attodatetime.toordinal()Return the proleptic Gregorian ordinal of the date. The same asself.date().toordinal().attodatetime.weekday()Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same asself.date().weekday().attodatetime.isoweekday()Return the day of the week as an integer, where Monday is 1 and Sunday is 7. The same asself.date().isoweekday().attodatetime.isocalendar()Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The same asself.date().isocalendar().attodatetime.isoformat([sep])Return a string representing the date and time in ISO 8601 format,YYYY-MM-DDTHH:MM:SS.mmmmmmor, if microsecond is 0,YYYY-MM-DDTHH:MM:SSIfutcoffset()does not returnNone, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes:YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MMor, if microsecond is 0YYYY-MM-DDTHH:MM:SS+HH:MMThe optional argumentsep(default ‘T’) is a separator, placed between the date and time portions of the result.The decimal second component may be expanded up to 16 place fixed point.attodatetime.ctime()Return the result ofdatetime.ctime()for a native Pythondatetimematching theattodatetime. Nanosecond precision is lost.attodatetime.strftime(format)Return a string representing the date and time, controlled by an explicit format string. Only the directives explicitly listed in thestrftime() and strptime() Behaviorsection of the Python documentation are supported, as well as the following:DirectiveMeaningExample%oPicosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999%qAttosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999%vYoctosecond as a decimal
number, zero-padded on
the left.000000, 000001, …, 999999attotimeattotimeis an object wrapping a nativetimeobject along with fractional nanoseconds stored as aDecimal.classattotime.attotime([hour[, minute[, second[, microsecond[, nanosecond[, tzinfo]]]]]])All arguments are optional.tzinfomay beNone, or an instance of atzinfo subclass. The nanosecond argument may be float orDecimal. The remaining arguments must be ints.Instance attributes (read-only)hourInrange(24).minuteInrange(60).secondInrange(60).microsecondInrange(1000000).nanosecondInrange(1000), asDecimal.tzinfoThe object passed as thetzinfoargument to theattotimeconstructor, orNoneif none was passed.Supported operationst1 < t2t1 is considered less than t2 if t1 precedes t2 in time.str(t1)Equivalent tot1.isoformat().repr(t1)Returns a string in the formattotime.objects.attotime(h, m, s, us, ns, [tz]).Instance methodsattotime.replace([hour[, minute[, second[, microsecond[, nanosecond[, tzinfo]]]]]])Return anattotimeobject with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note thattzinfo=Nonecan be specified to create a naiveattotimefrom an awareattotimewith no conversion of date and time data.attotime.isoformat()Return a string representing the time in ISO 8601 format,HH:MM:SS.mmmmmmor, if microsecond is 0,HH:MM:SSIfutcoffset()does not returnNone, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes:HH:MM:SS.mmmmmm+HH:MMor, if microsecond is 0HH:MM:SS+HH:MMThe decimal second component may be expanded up to 16 place fixed point.attotime.strftime(formatstr)RaisesNotImplementedErrorattotime.utcoffset()IftzinfoisNone, returnsNone, else returnself.tzinfo.utcoffset(self)as anattotimedelta.attotime.dst()IftzinfoisNone, returnsNone, else returnself.tzinfo.dst(self)as anattotimedelta.attotime.tzname()IftzinfoisNone, returnsNone, else returnsself.tzinfo.tzname(self).DevelopmentSetupIt is recommended to develop using avirtualenv.Inside a virtualenv, development dependencies can be installed automatically:$ pip install -e .[dev]pre-commitis used for managing pre-commit hooks:$ pre-commit installTo run the pre-commit hooks manually:$ pre-commit run --all-filesTestsTests can be run using theunittest testing framework:$ python -m unittest discover attotimeContributingattotime is an open source project hosted onBitbucket.Any and all bugs are welcome on ourissue tracker. Of particular interest are places where the attotime implementation incorrectly deviates from native Python behavior. Pull requests containing unit tests or fixed bugs are always welcome!ReferencesPEP 410 which describes the need for high precision time typesBug report with implementation of PEP 410Bug report discussing loss of precision when parsing ISO8601 timestamps