mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
Naming scheme updated
This commit is contained in:
parent
f5d0967c24
commit
ce713b3edc
7 changed files with 83 additions and 20 deletions
|
@ -183,7 +183,7 @@ impl DateTime {
|
||||||
|
|
||||||
Returns an error if the input string is not a valid RFC 2822 date-time.
|
Returns an error if the input string is not a valid RFC 2822 date-time.
|
||||||
*/
|
*/
|
||||||
pub fn from_rfc_date(rfc_date: impl AsRef<str>) -> DateTimeResult<Self> {
|
pub fn from_rfc_2822_date(rfc_date: impl AsRef<str>) -> DateTimeResult<Self> {
|
||||||
let inner = ChronoDateTime::parse_from_rfc2822(rfc_date.as_ref())?.with_timezone(&Utc);
|
let inner = ChronoDateTime::parse_from_rfc2822(rfc_date.as_ref())?.with_timezone(&Utc);
|
||||||
Ok(Self { inner })
|
Ok(Self { inner })
|
||||||
}
|
}
|
||||||
|
@ -222,7 +222,7 @@ impl DateTime {
|
||||||
See [`chrono::DateTime::to_rfc2822`] for additional details.
|
See [`chrono::DateTime::to_rfc2822`] for additional details.
|
||||||
*/
|
*/
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn to_rfc_date(self) -> String {
|
pub fn to_rfc_2822_date(self) -> String {
|
||||||
self.inner.to_rfc2822()
|
self.inner.to_rfc2822()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,7 +255,8 @@ impl LuaUserData for DateTime {
|
||||||
);
|
);
|
||||||
// Normal methods
|
// Normal methods
|
||||||
methods.add_method("toIsoDate", |_, this, ()| Ok(this.to_iso_date()));
|
methods.add_method("toIsoDate", |_, this, ()| Ok(this.to_iso_date()));
|
||||||
methods.add_method("toRfcDate", |_, this, ()| Ok(this.to_rfc_date()));
|
methods.add_method("toRfc3339", |_, this, ()| Ok(this.to_iso_date()));
|
||||||
|
methods.add_method("toRfc2822", |_, this, ()| Ok(this.to_rfc_2822_date()));
|
||||||
methods.add_method(
|
methods.add_method(
|
||||||
"formatUniversalTime",
|
"formatUniversalTime",
|
||||||
|_, this, (format, locale): (Option<String>, Option<String>)| {
|
|_, this, (format, locale): (Option<String>, Option<String>)| {
|
||||||
|
|
|
@ -22,8 +22,11 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
|
||||||
.with_function("fromIsoDate", |_, iso_date: String| {
|
.with_function("fromIsoDate", |_, iso_date: String| {
|
||||||
Ok(DateTime::from_iso_date(iso_date)?)
|
Ok(DateTime::from_iso_date(iso_date)?)
|
||||||
})?
|
})?
|
||||||
.with_function("fromRfcDate", |_, rfc_date: String| {
|
.with_function("fromRfc3339", |_, iso_date: String| {
|
||||||
Ok(DateTime::from_rfc_date(rfc_date)?)
|
Ok(DateTime::from_iso_date(iso_date)?)
|
||||||
|
})?
|
||||||
|
.with_function("fromRfc2822", |_, rfc_date: String| {
|
||||||
|
Ok(DateTime::from_rfc_2822_date(rfc_date)?)
|
||||||
})?
|
})?
|
||||||
.with_function("fromLocalTime", |_, values| {
|
.with_function("fromLocalTime", |_, values| {
|
||||||
Ok(DateTime::from_local_time(&values)?)
|
Ok(DateTime::from_local_time(&values)?)
|
||||||
|
|
|
@ -92,13 +92,14 @@ create_tests! {
|
||||||
datetime_format_local_time: "datetime/formatLocalTime",
|
datetime_format_local_time: "datetime/formatLocalTime",
|
||||||
datetime_format_universal_time: "datetime/formatUniversalTime",
|
datetime_format_universal_time: "datetime/formatUniversalTime",
|
||||||
datetime_from_iso_date: "datetime/fromIsoDate",
|
datetime_from_iso_date: "datetime/fromIsoDate",
|
||||||
datetime_from_rfc_date: "datetime/fromRfcDate",
|
datetime_from_rfc_2822_date: "datetime/fromRfc2822",
|
||||||
|
datetime_from_rfc_3339_date: "datetime/fromRfc3339",
|
||||||
datetime_from_local_time: "datetime/fromLocalTime",
|
datetime_from_local_time: "datetime/fromLocalTime",
|
||||||
datetime_from_universal_time: "datetime/fromUniversalTime",
|
datetime_from_universal_time: "datetime/fromUniversalTime",
|
||||||
datetime_from_unix_timestamp: "datetime/fromUnixTimestamp",
|
datetime_from_unix_timestamp: "datetime/fromUnixTimestamp",
|
||||||
datetime_now: "datetime/now",
|
datetime_now: "datetime/now",
|
||||||
datetime_to_iso_date: "datetime/toIsoDate",
|
datetime_to_iso_date: "datetime/toIsoDate",
|
||||||
datetime_to_rfc_date: "datetime/toRfcDate",
|
datetime_to_rfc_2822_date: "datetime/toRfc2822",
|
||||||
datetime_to_local_time: "datetime/toLocalTime",
|
datetime_to_local_time: "datetime/toLocalTime",
|
||||||
datetime_to_universal_time: "datetime/toUniversalTime",
|
datetime_to_universal_time: "datetime/toUniversalTime",
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
local DateTime = require("@lune/datetime")
|
local DateTime = require("@lune/datetime")
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
DateTime.fromRfcDate("Fri, 21 Nov 1997 09:55:06 -0600") ~= nil,
|
DateTime.fromRfc2822("Fri, 21 Nov 1997 09:55:06 -0600") ~= nil,
|
||||||
"expected DateTime.fromRfcDate() to return DateTime, got nil"
|
"expected DateTime.fromRfcDate() to return DateTime, got nil"
|
||||||
)
|
)
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
DateTime.fromRfcDate("Tue, 1 Jul 2003 10:52:37 +0200") ~= nil,
|
DateTime.fromRfc2822("Tue, 1 Jul 2003 10:52:37 +0200") ~= nil,
|
||||||
"expected DateTime.fromRfcDate() to return DateTime, got nil"
|
"expected DateTime.fromRfcDate() to return DateTime, got nil"
|
||||||
)
|
)
|
11
tests/datetime/fromRfc3339.luau
Normal file
11
tests/datetime/fromRfc3339.luau
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
local DateTime = require("@lune/datetime")
|
||||||
|
|
||||||
|
assert(
|
||||||
|
DateTime.fromRfc3339("2023-08-26T16:56:28Z") ~= nil,
|
||||||
|
"expected DateTime.fromIsoDate() to return DateTime, got nil"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert(
|
||||||
|
DateTime.fromRfc3339("1929-12-05T23:18:23Z") ~= nil,
|
||||||
|
"expected DateTime.fromIsoDate() to return DateTime, got nil"
|
||||||
|
)
|
|
@ -1,7 +1,7 @@
|
||||||
local DateTime = require("@lune/datetime")
|
local DateTime = require("@lune/datetime")
|
||||||
|
|
||||||
local now = DateTime.now()
|
local now = DateTime.now()
|
||||||
local nowRfc = now:toRfcDate()
|
local nowRfc = now:toRfc2822()
|
||||||
|
|
||||||
assert(type(nowRfc) == "string", "toRfcDate should return a string")
|
assert(type(nowRfc) == "string", "toRfcDate should return a string")
|
||||||
assert(
|
assert(
|
||||||
|
@ -19,8 +19,18 @@ end
|
||||||
|
|
||||||
-- Validate month
|
-- Validate month
|
||||||
local validMonths = {
|
local validMonths = {
|
||||||
Jan = true, Feb = true, Mar = true, Apr = true, May = true, Jun = true,
|
Jan = true,
|
||||||
Jul = true, Aug = true, Sep = true, Oct = true, Nov = true, Dec = true,
|
Feb = true,
|
||||||
|
Mar = true,
|
||||||
|
Apr = true,
|
||||||
|
May = true,
|
||||||
|
Jun = true,
|
||||||
|
Jul = true,
|
||||||
|
Aug = true,
|
||||||
|
Sep = true,
|
||||||
|
Oct = true,
|
||||||
|
Nov = true,
|
||||||
|
Dec = true,
|
||||||
}
|
}
|
||||||
assert(validMonths[month], "Month must be a valid RFC 2822 month abbreviation")
|
assert(validMonths[month], "Month must be a valid RFC 2822 month abbreviation")
|
||||||
|
|
||||||
|
@ -38,8 +48,14 @@ if not hour or not minute or not second then
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(hour and tonumber(hour) >= 0 and tonumber(hour) < 24, "Hour must be between 0 and 23")
|
assert(hour and tonumber(hour) >= 0 and tonumber(hour) < 24, "Hour must be between 0 and 23")
|
||||||
assert(minute and tonumber(minute) >= 0 and tonumber(minute) < 60, "Minute must be between 0 and 59")
|
assert(
|
||||||
assert(second and tonumber(second) >= 0 and tonumber(second) < 60, "Second must be between 0 and 59")
|
minute and tonumber(minute) >= 0 and tonumber(minute) < 60,
|
||||||
|
"Minute must be between 0 and 59"
|
||||||
|
)
|
||||||
|
assert(
|
||||||
|
second and tonumber(second) >= 0 and tonumber(second) < 60,
|
||||||
|
"Second must be between 0 and 59"
|
||||||
|
)
|
||||||
|
|
||||||
-- Validate timezone
|
-- Validate timezone
|
||||||
local tzHour, tzMinute = timezone:match("^([+-]%d%d)(%d%d)$")
|
local tzHour, tzMinute = timezone:match("^([+-]%d%d)(%d%d)$")
|
||||||
|
@ -47,5 +63,11 @@ if not tzHour or not tzMinute then
|
||||||
error("Failed to extract timezone components from RFC 2822 date string")
|
error("Failed to extract timezone components from RFC 2822 date string")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(tzHour and tonumber(tzHour) >= -14 and tonumber(tzHour) <= 14, "Timezone hour offset must be between -14 and +14")
|
assert(
|
||||||
assert(tzMinute and tonumber(tzMinute) >= 0 and tonumber(tzMinute) < 60, "Timezone minute offset must be between 0 and 59")
|
tzHour and tonumber(tzHour) >= -14 and tonumber(tzHour) <= 14,
|
||||||
|
"Timezone hour offset must be between -14 and +14"
|
||||||
|
)
|
||||||
|
assert(
|
||||||
|
tzMinute and tonumber(tzMinute) >= 0 and tonumber(tzMinute) < 60,
|
||||||
|
"Timezone minute offset must be between 0 and 59"
|
||||||
|
)
|
|
@ -203,7 +203,7 @@ end
|
||||||
|
|
||||||
@return string -- The RFC 2822 formatted string
|
@return string -- The RFC 2822 formatted string
|
||||||
]=]
|
]=]
|
||||||
function DateTime.toRfcDate(self: DateTime): string
|
function DateTime.toRfc2822(self: DateTime): string
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -416,6 +416,7 @@ end
|
||||||
@tag Constructor
|
@tag Constructor
|
||||||
|
|
||||||
Creates a new `DateTime` from an ISO 8601 date-time string.
|
Creates a new `DateTime` from an ISO 8601 date-time string.
|
||||||
|
This function behaves the same as `fromRfc3339`.
|
||||||
|
|
||||||
### Errors
|
### Errors
|
||||||
|
|
||||||
|
@ -435,6 +436,30 @@ function dateTime.fromIsoDate(isoDate: string): DateTime
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[=[
|
||||||
|
@within DateTime
|
||||||
|
@tag Constructor
|
||||||
|
|
||||||
|
Creates a new `DateTime` from an RFC 3339 date-time string.
|
||||||
|
|
||||||
|
### Errors
|
||||||
|
|
||||||
|
This constructor is fallible and may throw an error if the given
|
||||||
|
string does not strictly follow the RFC 3339 date-time string format.
|
||||||
|
|
||||||
|
Some examples of valid RFC 3339 date-time strings are:
|
||||||
|
|
||||||
|
- `2020-02-22T18:12:08Z`
|
||||||
|
- `2000-01-31T12:34:56+05:00`
|
||||||
|
- `1970-01-01T00:00:00.055Z`
|
||||||
|
|
||||||
|
@param rfc3339Date -- An RFC 3339 formatted string
|
||||||
|
@return DateTime -- The new DateTime object
|
||||||
|
]=]
|
||||||
|
function dateTime.fromRfc3339(rfc3339Date: string): DateTime
|
||||||
|
return nil :: any
|
||||||
|
end
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
@within DateTime
|
@within DateTime
|
||||||
@tag Constructor
|
@tag Constructor
|
||||||
|
@ -452,10 +477,10 @@ end
|
||||||
- `Tue, 1 Jul 2003 10:52:37 +0200`
|
- `Tue, 1 Jul 2003 10:52:37 +0200`
|
||||||
- `Mon, 23 Dec 2024 01:58:48 GMT`
|
- `Mon, 23 Dec 2024 01:58:48 GMT`
|
||||||
|
|
||||||
@param rfcDate -- An RFC 2822 formatted string
|
@param rfc2822Date -- An RFC 2822 formatted string
|
||||||
@return DateTime -- The new DateTime object
|
@return DateTime -- The new DateTime object
|
||||||
]=]
|
]=]
|
||||||
function dateTime.fromRfcDate(rfcDate: string): DateTime
|
function dateTime.fromRfc2822(rfc2822Date: string): DateTime
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue