Skip to content

Conversation

CleverSource
Copy link
Contributor

@CleverSource CleverSource commented Dec 23, 2024

This adds support for RFC 2822 to the DateTime library so dates in the RFC 2822 format can be parsed e.g. date headers when making requests with the Net library.

I propose a function such as fromDate is added as well, which will attempt to parse both an RFC 3339 date and RFC 2822 date, returning whichever does not fail. Erroring if neither a RFC 3339 date or RFC 2822 can't be returned. I have omitted this from the PR unless maintainers approve.

At the moment the following is the best alternative, which is why I propose this is implemented.

local Net = require("@lune/net")
local DateTime = require("@lune/datetime")

local response = Net.request {
    url = "https://google.com"
}

local date, month, year, time =
    response.headers.date:match("(%d%d?) (%a%a%a) (%d%d%d%d) (%d%d:%d%d:%d%d)")
local months = {
    Jan = 1,
    Feb = 2,
    Mar = 3,
    Apr = 4,
    May = 5,
    Jun = 6,
    Jul = 7,
    Aug = 8,
    Sep = 9,
    Oct = 10,
    Nov = 11,
    Dec = 12
}
local dateNow = DateTime.fromUniversalTime({
    year = year,
    month = months[month],
    day = date,
    hour = time:match("(%d%d):%d%d:%d%d"),
    minute = time:match("%d%d:(%d%d):%d%d"),
    second = time:match("%d%d:%d%d:(%d%d)")
}).unixTimestampMillis

print(dateNow)

Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

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

Code looks great and I'm willing to accept it as is - but I do think the naming needs a change. The existing fromIsoDate is really just another "rfc" date spec, which is unfortunate. It is named that way to mirror the Roblox DateTime datatype which does not have anything other than rfc 3339 (which they call "iso")..
I'm open to suggestions here.

@CleverSource
Copy link
Contributor Author

CleverSource commented Apr 19, 2025

Code looks great and I'm willing to accept it as is - but I do think the naming needs a change. The existing fromIsoDate is really just another "rfc" date spec, which is unfortunate. It is named that way to mirror the Roblox DateTime datatype which does not have anything other than rfc 3339 (which they call "iso").. I'm open to suggestions here.

That's fair. I can make the name more verbose, like fromRfc2822Date, and add fromRfc3339Date for consistency, while also keeping fromIsoDate to match the Roblox datatype. fromDateHeader might also be a decent naming convention since date headers are always RFC 2822, but I don't think I like that either. Any opinions on this?

@filiptibell
Copy link
Collaborator

I can make the name more verbose, like fromRfc2822Date, and add fromRfc3339Date for consistency

I like that. The Date suffix is probably unnecessary, we can do just fromRfc2822 + fromRfc3339 and in the future we mark the old fromIsoDate as hidden and/or deprecated in documentation.

@CleverSource
Copy link
Contributor Author

I like that. The Date suffix is probably unnecessary, we can do just fromRfc2822 + fromRfc3339 and in the future we mark the old fromIsoDate as hidden and/or deprecated in documentation.

Great! I just committed the changes

Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

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

LGTM 👍 the CI failing is just new clippy lints.. I'll get those fixed

@filiptibell filiptibell merged commit dd7f6d6 into lune-org:main Apr 23, 2025
2 of 6 checks 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.

2 participants