Skip to content

Replace Date, Time, DateTime with their counterparts from gleam_time. Closes #8#9

Merged
lpil merged 8 commits intolpil:mainfrom
ibarchenkov:feature/issue-8-replace-date-types-with-gleam_time
May 31, 2025
Merged

Replace Date, Time, DateTime with their counterparts from gleam_time. Closes #8#9
lpil merged 8 commits intolpil:mainfrom
ibarchenkov:feature/issue-8-replace-date-types-with-gleam_time

Conversation

@ibarchenkov
Copy link
Contributor

I'm a Gleam newbie, so please review carefully. Let me know if there’s any room for improvement or if you spot any obvious mistakes.

Closes #8

Copy link
Owner

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thank you! Looks great.

I'm not sure about the conversion into Timestamp here. It is the type we want to encourage people to use, but unfortunately TOML supports ambiguous dates without a timezone, so we can't do this without discarding that information about whether the offset is local or not.

Here's the part from the spec:

If you omit the offset from an RFC 3339 formatted date-time, it will represent the given date-time without any relation to an offset or timezone. It cannot be converted to an instant in time without additional information. Conversion to an instant, if required, is implementation-specific.

Do you have any thoughts on this?

#("1", 100_000_000),
#("001", 1_000_000),
#("000000789", 789),
]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one test case per test please 🙏

@ibarchenkov
Copy link
Contributor Author

I'm not sure about the conversion into Timestamp here. It is the type we want to encourage people to use, but unfortunately TOML supports ambiguous dates without a timezone, so we can't do this without discarding that information about whether the offset is local or not.

Good point, I completely overlooked that. 🤦

Two options come to my mind:

pub type DateTime {
  // Represents a local/ambiguous/naive datetime, e.g., "1979-05-27T07:32:00"
  LocalDateTime(date: calendar.Date, time: calendar.TimeOfDay)
  // Represents a specific, unambiguous point in time,
  // taking the offset into account, e.g., "1979-05-27T07:32:00Z", "1979-05-27T00:32:00-07:00"
  Timestamp(timestamp.Timestamp)
}
pub type DateTime {
  DateTimeValue(date: calendar.Date, time: calendar.TimeOfDay, offset: Offset)
}

pub type Offset {
  Local
  Offset(duration.Duration)
}

What do you think?

@lpil
Copy link
Owner

lpil commented May 21, 2025

Although we want to promote the use of timestamps as much as possible I think it might make sense to use calendar time here, since TOML doesn't permit you to always use them? Did you have any thoughts?

@ibarchenkov
Copy link
Contributor Author

ibarchenkov commented May 21, 2025

There seems to be a clear distinction in the spec between Offset Date-Time and Local Date-Time.
Can we represent these as individual variants in the Toml type?

pub type Toml {
  OffsetDateTime(timestamp.Timestamp)
  LocalDateTime(LocalDateTime)
  ... 
}

And replace as_date_time function with:

pub fn as_offset_date_time(toml: Toml) -> Result(timestamp.Timestamp, GetError) {
  case toml {
    OffsetDateTime(timestamp) -> Ok(timestamp)
    other -> Error(WrongType([], "OffsetDateTime", classify(other)))
  }
}

pub fn as_local_date_time(toml: Toml) -> Result(LocalDateTime, GetError) {
  case toml {
    LocalDateTime(local_datetime) -> Ok(local_datetime)
    other -> Error(WrongType([], "LocalDateTime", classify(other)))
  }
}

As a side note, it might be useful to introduce a type similar to Elixir's NaiveDateTime in gleam_time, so it can be reused here and in other libraries, for example in pog to represent Postgres' timestamp without time zone.

@lpil
Copy link
Owner

lpil commented May 28, 2025

That design would discard the offset information, which is encoded in the TOML.

Let's store it as calendar time in the data structure, and then we can make getter functions for both calendar and timestamp time

@ibarchenkov
Copy link
Contributor Author

Let's store it as calendar time in the data structure

Could you please take a look at my last commit? Do you think using Duration for the Offset makes sense, or would you prefer representing it explicitly as hours and minutes?

Copy link
Owner

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Owner

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!! Thank you very much!

I've fixed CI and renamed some of the already existing functions.

@lpil lpil merged commit fd74ff0 into lpil:main May 31, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace Date, Time and DateTime types with their counterpars from gleam_time

2 participants