#timestamp.parse_rfc3339 with implicit precision?

1 messages · Page 1 of 1 (latest)

rugged leaf
#

I want to parse time strings with implicit precision, so parse_datetime("2018") would be a valid timestamp with precision YYYY. How much rewriting of https://github.com/gleam-lang/time/blob/2b63bbc3795489ca8658dd0e0b7452c7eb214aeb/src/gleam/time/timestamp.gleam#L533 would I need to do to only go through the bytes once and get timestamp + precision? Or what approach in general would you take for this

import gleam/time/timestamp

pub type DateTimePrecision {
  YYYY
  YYYYMM
  YYYYMMDD
  YYYYMMDDThhmmsszzzz
}

pub type DateTime {
  DateTime(timestamp: timestamp.Timestamp, precision: DateTimePrecision)
}

pub fn parse_datetime (in: String) -> DateTime {
  todo
}

pub fn main() {
  echo timestamp.parse_rfc3339("2018")
  echo timestamp.parse_rfc3339("1973-06")
  echo timestamp.parse_rfc3339("1905-08-23")
  echo timestamp.parse_rfc3339("2015-02-07T13:28:17-05:00")
  echo timestamp.parse_rfc3339("2017-01-01T00:00:00.000Z")
}
GitHub

🕰️ Work with time in Gleam! Contribute to gleam-lang/time development by creating an account on GitHub.

#

just want to check that messing around with timestamp.parse_rfc3339 would be the best answer

sage shuttle
#

You might get some mileage out of the gtempo library. That has a wider range of supported formats and can convert to gleam/time types.

#

Afaiu gleam/time is deliberately quite minimal

prime delta
#

That would be against RFC3339

#

You would need to use some other specification, or specify your own syntax

#

Is there one in particular that you'd like to use?

#

If you do make this please do not call it parse_rfc3339. Having a function that is not RFC3339 but is called RFC3339 is going to confuse an already very complicated area.

#

ISO8601 miiight be what you want but it has a whole bunch of extra stuff in it, can be challenging to work with and there's not a singular good output for it

#
2026-02
2026
2026-02-08T14:30Z
2026-02T14:30+01
2026-W06-7T23:00
2026-039T12
2026-039

All valid ISO8601

#

It also has both 00:00 and 24:00 as two different times of day

rugged leaf
#

The spec (fhir datetime) says a subset of ISO8601, with implicit precision and no 24:00. On avoiding confusion I believe this would not be a pub fn anyone would see; would just be used in decoder

prime delta
#

There was a version of ISO8601 without 24:00, and they have have variable precision

#

Sounds like that one

#

Writing an ISO8601 parser sounds like it could be the way to go

#

I would avoid copying gtempo and instead copy gleam_time. gtempo's parsing code is slow and quite complex due to being regex based compared to gleam_time's pattern matching

rugged leaf
#

Ah ok ty. Yeah copying gleam_time sounds good

prime delta
#

Maybe I'll make an ISO8601 library...

rugged leaf
#

I feel like I want a very particular parser, especially implicit precision, so idk if it would make sense as a specific case of a general parser. But your library would at least be interesting to see for sure

prime delta
#

Can you share the spec with me? I can't find it 🙏 thanks

rugged leaf
prime delta
#

YYYY, YYYY-MM, YYYY-MM-DD or YYYY-MM-DDThh:mm:ss+zz:zz
oh cool it's much simpler than ISO8601

rugged leaf
#

Yeah I can probably just take gleam_time and not change anything too crazy

prime delta
#

I can't see where it says what to do if (for example) the month or day is missing

rugged leaf
#

Tbh idk, for now I’d be fine with failing

prime delta
#

I mean if it's the valid format YYYY

rugged leaf
#

Oh yyyy should succeed yeah

prime delta
#

does that mean "the whole of that year" or "the very start of the year" or something else?

rugged leaf
#

Ah I see, yeah idk probably up to the user to interpret

prime delta
#

Surprised it doesn't say tbh

rugged leaf
prime delta
#

that's hilarious 😁

solemn ether
prime delta
#

truly

#

there's a reason gleam_time doesn't have it

solemn ether
#

How important do you think an iso8601 parser is?

prime delta
#

I think it could be useful at some point

solemn ether
#

Parsers are fun to write at least so maybe important doesn’t matter

prime delta
#

I'd give it a horrible API that preserves all the info about precision

sage shuttle
#

Someone should recreate JS's new Date() in Gleam

prime delta
#

OK well I nerd sniped myself

#

This consumed my evening 😅

#
pub type DateTime {
  CalendarDate(
    year: Int,
    month: calendar.Month,
    day: Int,
    time: option.Option(Time),
  )
  CalendarMonthDate(year: Int, month: calendar.Month)
  CalendarYearDate(year: Int)
  OrdinalDate(
    year: Int,
    day_of_year: Int,
    time: option.Option(Time),
  )
  WeekDate(
    year: Int,
    week: Int,
    day_of_week: DayOfWeek,
    time: option.Option(Time),
  )
  YearWeek(year: Int, week: Int)
}

pub type Time {
  TimeSecond(
    hour: Int,
    minute: Int,
    second: Int,
    nanosecond: option.Option(Int),
    timezone: option.Option(Timezone),
  )
  TimeMinute(
    hour: Int,
    minute: Int,
    nanosecond: option.Option(Int),
    timezone: option.Option(Timezone),
  )
  TimeHour(
    hour: Int,
    nanosecond: option.Option(Int),
    timezone: option.Option(Timezone),
  )
}

I hate it

mortal wedge
#

all the web stuff is iso8601 so this is good to exist i think

prime delta
#

it's not

#

it claims to be but it's all actually RFC3339

#

Which is the subset of ISO8601 that isn't bonkers

mortal wedge
#

you sure 😂

solemn ether
#

Whoa! That was fast 🤯

tardy igloo
#

this is also very cool

prime delta
#

And the reduced precision stuff

prime delta
#

I’m more of an AI skeptic but I’m trying to understand it since Gleam people do use it, so I asked a model to find missing test cases at points, and it did manage that!

#

I also tried to use it to make the data types but it made a horrible mess and I deleted that and started again.

#

Tbh I’m a bit conflicted on the whole thing, but if I don’t understand it I can’t do anything to stop the steady increase of low quality generative code. But I’m getting off topic

solemn ether