Open chrisimcevoy opened 1 month ago
I've spent a lot of time thinking about this and not actually doing it. I'm going to just bite the bullet and get something out there.
This is the result of that thinking I've been doing... subject to change still.
Class | Noda | Pyoda | Comments |
---|---|---|---|
Duration | FromTimeSpan() | from_timedelta() | |
Duration | ToTimeSpan() | to_timedelta() | |
Instant | FromDateTimeOffset() | from_aware_datetime() | Same as FromDateTimeUtc(). Should raise if a naive datetime is passed. |
Instant | FromDateTimeUtc() | from_aware_datetime() | Same as FromDateTimeOffset(). Should raise if a naive datetime is passed. |
Instant | ToDateTimeOffset() | to_datetime_utc() | Same as ToDateTimeUtc() |
Instant | ToDateTimeUtc() | to_datetime_utc() | Same as ToDateTimeOffset() |
LocalDate | FromDateOnly() | from_date() | |
LocalDate | ToDateOnly() | to_date() | |
LocalDate | FromDateTime() | from_naive_datetime() | Accepts a naive datetime and extracts the date from it. No time zone conversion is performed. Should raise if an aware datetime is passed. |
LocalDate | ToDateTimeUnspecified() | to_naive_datetime() | Implicit conversion to Gregorian calendar!!! |
LocalDateTime | FromDateTime() | from_naive_datetime() | Raises if an aware datetime is passed. |
LocalDateTime | ToDateTimeUnspecified() | to_naive_datetime() | Note that there is an implicit conversion to gregorian, and that fractional seconds precision may be lost. |
LocalTime | FromTimeOnly() | from_time() | |
LocalTime | ToTimeOnly() | to_time() | |
Offset | FromTimeSpan() | from_timedelta() | |
Offset | ToTimeSpan() | to_timedelta() | |
OffsetDateTime | FromDateTimeOffset() | from_aware_datetime() | Note that this has a fixed offset from utc!!! |
OffsetDateTime | ToDateTimeOffset() | to_aware_datetime() | Note that the tzinfo is a timezone instance with a fixed utcoffset. |
ZonedDateTime | FromDateTimeOffset() | from_aware_datetime() | In Noda Time, the offset is fixed and unchanging... We can probably make this look up the zone id though in the default provider. Raises if a naive datetime is passed, or if the timezone cannot be found. |
ZonedDateTime | ToDateTimeOffset() | to_aware_datetime() | Represents the same date and time in the same timezone. |
ZonedDateTime | ToDateTimeUtc() | to_datetime_utc() | Represents the same instant in time (expressed as utc) rather than the same local time. |
ZonedDateTime | ToDateTimeUnspecified() | to_naive_datetime() | Represents the same local time rather than the same instant in time. |
Noda Time provides the following methods for conversion to/from BCL types:
When thinking about how to port these to Python, some of them are relatively self-explanatory:
.ToTimeSpan()
and.FromTimeSpan()
become.to_timedelta()
and.from_timedelta()
.ToDateOnly()
and.FromDateOnly()
becometo_date()
andfrom_date()
.ToTimeOnly()
and.FromTimeOnly()
becometo_time()
andfrom_time()
Where things become slightly more nuanced is when you are dealing with conversions to/from
DateTime
orDateTimeOffset
.In Noda Time, the distinguishing features of these methods are:
DateTime
orDateTimeOffset
DateTime
conversions, these are further distinguished by theDateTimeKind
.In Python's stdlib, we don't have a real equivalent to
DateTimeOffset
. We just have timezone-aware datetimes. We also don't have aDateTimeKind
to deal with - just the notion ofaware
andnaive
datetimes.Take
Instant
as an example. In Noda Time, we haveFromDateTimeOffset()
andFromDateTimeUtc()
. In Python, it probably makes sense to have a singlefrom_aware_date_time()
method. Similarly, theToDateTimeOffset()
andToDateTimeUtc
methods could probably can be represented in Python with a singleto_utc_datetime()
method.