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

Add support for RFC 2822 in DateTime #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

CleverSource
Copy link

@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)

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.

1 participant