chrisimcevoy / pyoda-time

A better date and time API for Python
https://pyodatime.org/
Apache License 2.0
1 stars 0 forks source link

Conversions to stdlib types #160

Open chrisimcevoy opened 1 month ago

chrisimcevoy commented 1 month ago

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:

Where things become slightly more nuanced is when you are dealing with conversions to/from DateTime or DateTimeOffset.

In Noda Time, the distinguishing features of these methods are:

In Python's stdlib, we don't have a real equivalent to DateTimeOffset. We just have timezone-aware datetimes. We also don't have a DateTimeKind to deal with - just the notion of aware and naive datetimes.

Take Instant as an example. In Noda Time, we have FromDateTimeOffset() and FromDateTimeUtc(). In Python, it probably makes sense to have a single from_aware_date_time() method. Similarly, the ToDateTimeOffset() and ToDateTimeUtc methods could probably can be represented in Python with a single to_utc_datetime() method.

chrisimcevoy commented 2 days 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.