Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalizing two-digit years #2

Open
aantron opened this issue May 10, 2023 · 2 comments
Open

Normalizing two-digit years #2

aantron opened this issue May 10, 2023 · 2 comments

Comments

@aantron
Copy link

aantron commented May 10, 2023

Thank you for this library! It seems exactly what is needed in aantron/dream#270.

I tried round-tripping the three formats from the HTTP RFCs. For the RFC 850 format, with a two-digit year, the round-tripping doesn't quite work. The resulting date has the two-digit year. When printed in the preferred format,

Sat, 06 Nov 0094 08:49:37 GMT

Assuming these years refer to the 1900s (and since this format is obsolete), I had to write a function

let normalize_year ptime =
  let ((year, month, day), time) = Ptime.to_date_time ptime in
  let year = if year < 100 then year + 1900 else year in
  Ptime.of_date_time ((year, month, day), time) |> Option.get
  (* Option.get is safe here because this function updates the year. If the date
     was valid to begin with, it will be valid after the update. From @beajeanm:
     an additional consideration here is whether a two-digit year Y is a leap
     year if and only if Y + 1900 is a leap year, and we believe it is, as
     suggested by testing Ptime itself. *)

to normalize the year.

Should this be done in http-date? Is this something for the user or framework to decide? I can open a PR if appropriate.

@bikallem
Copy link
Owner

bikallem commented May 10, 2023

Is providing a century parameter feasible? i.e.

# Http_date.decode ~century:19 "Sunday, 06-Nov-94 08:49:37 GMT" |> Http_date.encode;;
- : string = "Sun, 06 Nov 1994 08:49:37 GMT"

If not then a PR for normalize_year function as above should be okay.

@aantron
Copy link
Author

aantron commented May 10, 2023

A century parameter seems pointless. Wouldn't this always be 19 for dates in this format?

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

No branches or pull requests

2 participants