feat!: migrate to gleam_time for internal Time and Duration representation#54
Conversation
BREAKING CHANGES: - Time type now uses timestamp.Timestamp and time_duration.Duration internally - TimeOfDay.milli_second field renamed to nanosecond (now stores nanoseconds) - unix_epoch changed from constant to function: unix_epoch() New features: - Added get_milli_second() deprecated helper for TimeOfDay backward compatibility - Added NanoSecond to duration.Unit enum - Added duration.nano_seconds() constructor - Added gleam_time interop: to_gleam_timestamp(), from_gleam_timestamp(), to_unix_nano(), from_unix_nano(), get_offset_duration() - Added duration interop: to_gleam_duration(), from_gleam_duration() FFI changes: - Removed now(), local_offset(), to_parts(), from_parts() (now provided by gleam_time) - Kept monotonic_now(), weekday(), local_timezone() (not provided by gleam_time) Version bumped to 2.0.0 for breaking changes.
|
@tylerbutler Could you resolve the conflicts? |
|
I just realized the |
Hmmm, good catch. I think I might have been thinking about back compat or something... let me take a fresh look. I'll submit an update shortly. |
|
Good question — I think it was just an oversight. I focused on migrating I think the right approach is to migrate The one question is what to do with the birl-specific utilities that don't have equivalents in I'm going to go ahead and implement the functions to work on |
Replace birl's internal Duration(Int) type with a type alias for gleam_time's duration.Duration. All duration functions now operate on gleam_time Duration values directly (nanosecond precision). - Duration is now a type alias for gleam/time/duration.Duration - All internal constants converted from microseconds to nanoseconds - to_gleam_duration/from_gleam_duration deprecated as no-ops - decompose now includes NanoSecond and MicroSecond units - Updated birl.gleam add/subtract to work without unwrapping Duration - Updated range negate_step to use time_duration.difference
…ation - Extract pad2/pad3 formatting helpers in birl.gleam, replacing dozens of repeated int.to_string |> string.pad_start patterns - Extract validate_timezone helper to deduplicate timezone validation - Reuse existing to_nanoseconds helper in compare, scale_up, scale_down - Consolidate new/accurate_new and decompose/accurate_decompose via shared parameterized helpers in duration.gleam - Replace single-arm case destructuring with let destructuring in interval.gleam - Simplify now_with_timezone and set_timezone using result.try - Simplify is_invalid_date/is_invalid_time with direct boolean expressions - Remove redundant int.absolute_value call in legible_difference
|
@massivefermion This should be ready now. I did some code cleanup too, since a major release seems like a good time. I can split that out if you prefer. |
Remove "now" / historical-change language from comments to describe current behavior rather than migration history. Fix factual error in get_milli_second doc comment (multiply → divide). Add test coverage for all new public functions: get_milli_second, to_unix_nano, from_unix_nano, get_offset_duration, to_gleam_timestamp, from_gleam_timestamp, and duration.nano_seconds.
Remove tests that exercised deprecated no-op functions (to_gleam_duration, from_gleam_duration, get_milli_second) and replace duration conversion test with one that uses Duration as gleam_time's Duration type directly.
|
Thanks |
Summary
This is a major version bump (v2.0.0) that replaces birl's hand-rolled internal representations with types from
gleam_time, reducing FFI surface area and aligning birl with the Gleam ecosystem's standard time library.Breaking Changes
Timeinternal representationwall_time: Int(microseconds) is nowtimestamp: timestamp.Timestampoffset: Int(microseconds) is nowoffset: time_duration.DurationDurationtype replacedDuration(Int)(a wrapper around microseconds) is now a type alias forgleam_time'stime_duration.Duration(nanoseconds internally)milli_second,second,hour) updated from microsecond to nanosecond scaleduration.parsetreats bare numbers as nanoseconds instead of microsecondsduration.decompose/duration.accurate_decomposeincludeNanoSecondin their outputTimeOfDayfield renamedTimeOfDay.milli_secondfield renamed toTimeOfDay.nanosecondwith nanosecond-scale valuesget_milli_secondhelper is provided for backward compatibilityunix_epochis now a functionbirl.unix_epochchanged from aconsttobirl.unix_epoch()because Gleam constants cannot hold opaque types (Timestamp)NanoSecondunit addedduration.NanoSecondvariant added to theUnittypeNew APIs
birl.to_unix_nano/birl.from_unix_nanobirl.to_gleam_timestamp/birl.from_gleam_timestampto_timestamp/from_timestampfor naming consistencybirl.get_offset_durationtime_duration.Durationdirectlyduration.nano_secondsFFI Reduction
The following FFI functions were removed from both
birl_ffi.erlandbirl_ffi.mjs, replaced bygleam_time:ffi_now→timestamp.system_time()ffi_local_offset→calendar.local_offset()ffi_to_parts→timestamp.to_calendar()ffi_from_parts→timestamp.from_calendar()The Erlang FFI also drops
calculate_days_from_year,calculate_days_from_month, and theDaysInMonthsconstant. Onlymonotonic_now,weekday, andlocal_timezoneremain as FFI.Gleam_time Interop
duration.to_gleam_duration/duration.from_gleam_durationare deprecated no-ops sinceDurationistime_duration.Durationtime_of_day_to_calendarandcalendar_to_time_of_daysimplified sinceTimeOfDaystores nanoseconds nativelyto_timestamp/from_timestampsimplified to direct field accessCode Quality
pad2/pad3formatting helpers, eliminating ~30 repetitiveint.to_string |> string.pad_startblocksvalidate_timezonehelper to deduplicate timezone validation innow()andfrom_erlang_local_datetime()duration.new/duration.accurate_newvia sharednew_with_constantsandduration.decompose/duration.accurate_decomposevia shareddecompose_with_constantsgenerate_offsetfrom pattern-match-on-decomposed-duration to direct arithmeticinterval.gleamby replacingcase interval { Interval(a, b) -> ... }withlet Interval(a, b) = intervalthroughoutcompareanddifferenceto usetimestamp.compare/timestamp.differencedirectlyCI
ubuntu-latesttoubuntu-22.04