Skip to content

Commit 8ffbb32

Browse files
committed
Enforce some more consistency with new datetime changes + add deprecation warning to iso methods
1 parent d6f4cb2 commit 8ffbb32

9 files changed

Lines changed: 77 additions & 126 deletions

File tree

crates/lune-std-datetime/src/date_time.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl DateTime {
159159
}
160160

161161
/**
162-
Parses a time string in the ISO 8601 format, such as
162+
Parses a time string in the RFC 3339 format, such as
163163
`1996-12-19T16:39:57-08:00`, into a new `DateTime` struct.
164164
165165
See [`chrono::DateTime::parse_from_rfc3339`] for additional details.
@@ -168,8 +168,8 @@ impl DateTime {
168168
169169
Returns an error if the input string is not a valid RFC 3339 date-time.
170170
*/
171-
pub fn from_iso_date(iso_date: impl AsRef<str>) -> DateTimeResult<Self> {
172-
let inner = ChronoDateTime::parse_from_rfc3339(iso_date.as_ref())?.with_timezone(&Utc);
171+
pub fn from_rfc_3339(date: impl AsRef<str>) -> DateTimeResult<Self> {
172+
let inner = ChronoDateTime::parse_from_rfc3339(date.as_ref())?.with_timezone(&Utc);
173173
Ok(Self { inner })
174174
}
175175

@@ -183,8 +183,8 @@ impl DateTime {
183183
184184
Returns an error if the input string is not a valid RFC 2822 date-time.
185185
*/
186-
pub fn from_rfc_2822_date(rfc_date: impl AsRef<str>) -> DateTimeResult<Self> {
187-
let inner = ChronoDateTime::parse_from_rfc2822(rfc_date.as_ref())?.with_timezone(&Utc);
186+
pub fn from_rfc_2822(date: impl AsRef<str>) -> DateTimeResult<Self> {
187+
let inner = ChronoDateTime::parse_from_rfc2822(date.as_ref())?.with_timezone(&Utc);
188188
Ok(Self { inner })
189189
}
190190

@@ -207,12 +207,12 @@ impl DateTime {
207207
}
208208

209209
/**
210-
Formats a time string in the ISO 8601 format, such as `1996-12-19T16:39:57-08:00`.
210+
Formats a time string in the RFC 3339 format, such as `1996-12-19T16:39:57-08:00`.
211211
212212
See [`chrono::DateTime::to_rfc3339`] for additional details.
213213
*/
214214
#[must_use]
215-
pub fn to_iso_date(self) -> String {
215+
pub fn to_rfc_3339(self) -> String {
216216
self.inner.to_rfc3339()
217217
}
218218

@@ -222,7 +222,7 @@ impl DateTime {
222222
See [`chrono::DateTime::to_rfc2822`] for additional details.
223223
*/
224224
#[must_use]
225-
pub fn to_rfc_2822_date(self) -> String {
225+
pub fn to_rfc_2822(self) -> String {
226226
self.inner.to_rfc2822()
227227
}
228228
}
@@ -254,9 +254,9 @@ impl LuaUserData for DateTime {
254254
},
255255
);
256256
// Normal methods
257-
methods.add_method("toIsoDate", |_, this, ()| Ok(this.to_iso_date()));
258-
methods.add_method("toRfc3339", |_, this, ()| Ok(this.to_iso_date()));
259-
methods.add_method("toRfc2822", |_, this, ()| Ok(this.to_rfc_2822_date()));
257+
methods.add_method("toIsoDate", |_, this, ()| Ok(this.to_rfc_3339())); // FUTURE: Remove this rfc3339 alias method
258+
methods.add_method("toRfc3339", |_, this, ()| Ok(this.to_rfc_3339()));
259+
methods.add_method("toRfc2822", |_, this, ()| Ok(this.to_rfc_2822()));
260260
methods.add_method(
261261
"formatUniversalTime",
262262
|_, this, (format, locale): (Option<String>, Option<String>)| {

crates/lune-std-datetime/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ pub use self::date_time::DateTime;
1919
*/
2020
pub fn module(lua: Lua) -> LuaResult<LuaTable> {
2121
TableBuilder::new(lua)?
22-
.with_function("fromIsoDate", |_, iso_date: String| {
23-
Ok(DateTime::from_iso_date(iso_date)?)
22+
.with_function("fromIsoDate", |_, date: String| {
23+
Ok(DateTime::from_rfc_3339(date)?) // FUTURE: Remove this rfc3339 alias method
2424
})?
25-
.with_function("fromRfc3339", |_, iso_date: String| {
26-
Ok(DateTime::from_iso_date(iso_date)?)
25+
.with_function("fromRfc3339", |_, date: String| {
26+
Ok(DateTime::from_rfc_3339(date)?)
2727
})?
28-
.with_function("fromRfc2822", |_, rfc_date: String| {
29-
Ok(DateTime::from_rfc_2822_date(rfc_date)?)
28+
.with_function("fromRfc2822", |_, date: String| {
29+
Ok(DateTime::from_rfc_2822(date)?)
3030
})?
3131
.with_function("fromLocalTime", |_, values| {
3232
Ok(DateTime::from_local_time(&values)?)

crates/lune/src/tests.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ create_tests! {
9191
create_tests! {
9292
datetime_format_local_time: "datetime/formatLocalTime",
9393
datetime_format_universal_time: "datetime/formatUniversalTime",
94-
datetime_from_iso_date: "datetime/fromIsoDate",
95-
datetime_from_rfc_2822_date: "datetime/fromRfc2822",
96-
datetime_from_rfc_3339_date: "datetime/fromRfc3339",
94+
datetime_from_rfc_2822: "datetime/fromRfc2822",
95+
datetime_from_rfc_3339: "datetime/fromRfc3339",
9796
datetime_from_local_time: "datetime/fromLocalTime",
9897
datetime_from_universal_time: "datetime/fromUniversalTime",
9998
datetime_from_unix_timestamp: "datetime/fromUnixTimestamp",
10099
datetime_now: "datetime/now",
101-
datetime_to_iso_date: "datetime/toIsoDate",
102-
datetime_to_rfc_2822_date: "datetime/toRfc2822",
100+
datetime_to_rfc_2822: "datetime/toRfc2822",
101+
datetime_to_rfc_3339: "datetime/toRfc3339",
103102
datetime_to_local_time: "datetime/toLocalTime",
104103
datetime_to_universal_time: "datetime/toUniversalTime",
105104
}

tests/datetime/fromIsoDate.luau

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/datetime/fromRfc3339.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ local DateTime = require("@lune/datetime")
22

33
assert(
44
DateTime.fromRfc3339("2023-08-26T16:56:28Z") ~= nil,
5-
"expected DateTime.fromIsoDate() to return DateTime, got nil"
5+
"expected DateTime.fromRfc3339() to return DateTime, got nil"
66
)
77

88
assert(
99
DateTime.fromRfc3339("1929-12-05T23:18:23Z") ~= nil,
10-
"expected DateTime.fromIsoDate() to return DateTime, got nil"
10+
"expected DateTime.fromRfc3339() to return DateTime, got nil"
1111
)

tests/datetime/toLocalTime.luau

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
local DateTime = require("@lune/datetime")
22

3-
local values = DateTime.fromIsoDate("2023-08-27T05:54:19Z"):toLocalTime()
3+
local values = DateTime.fromRfc3339("2023-08-27T05:54:19Z"):toLocalTime()
44

55
local expectedDateTimeValues = os.date("*t", 1693115659)
66

7-
assert(
8-
values.year == expectedDateTimeValues.year,
9-
`expected {values.year} == {expectedDateTimeValues.year}`
10-
)
11-
assert(
12-
values.month == expectedDateTimeValues.month,
13-
`expected {values.month} == {expectedDateTimeValues.month}`
14-
)
15-
assert(
16-
values.day == expectedDateTimeValues.day,
17-
`expected {values.day} == {expectedDateTimeValues.day}`
18-
)
19-
assert(
20-
values.hour == expectedDateTimeValues.hour,
21-
`expected {values.hour} == {expectedDateTimeValues.hour}`
22-
)
23-
assert(
24-
values.minute == expectedDateTimeValues.min,
25-
`expected {values.minute} == {expectedDateTimeValues.min}`
26-
)
27-
assert(
28-
values.second == expectedDateTimeValues.sec,
29-
`expected {values.second} == {expectedDateTimeValues.sec}`
30-
)
7+
assert(values.year == expectedDateTimeValues.year, `expected {values.year} == {expectedDateTimeValues.year}`)
8+
assert(values.month == expectedDateTimeValues.month, `expected {values.month} == {expectedDateTimeValues.month}`)
9+
assert(values.day == expectedDateTimeValues.day, `expected {values.day} == {expectedDateTimeValues.day}`)
10+
assert(values.hour == expectedDateTimeValues.hour, `expected {values.hour} == {expectedDateTimeValues.hour}`)
11+
assert(values.minute == expectedDateTimeValues.min, `expected {values.minute} == {expectedDateTimeValues.min}`)
12+
assert(values.second == expectedDateTimeValues.sec, `expected {values.second} == {expectedDateTimeValues.sec}`)
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
local DateTime = require("@lune/datetime")
22

33
local now = DateTime.now()
4-
local nowIso = now:toIsoDate()
4+
local nowRfc = now:toRfc3339()
55

66
-- Make sure we have separator characters, T to separate date & time, + or Z to separate timezone
77

8-
local dateTimeSplitIdx = string.find(nowIso, "T")
9-
local timezoneSplitIdx = string.find(nowIso, "+")
10-
local timezoneZeroedIdx = string.find(nowIso, "Z")
8+
local dateTimeSplitIdx = string.find(nowRfc, "T")
9+
local timezoneSplitIdx = string.find(nowRfc, "+")
10+
local timezoneZeroedIdx = string.find(nowRfc, "Z")
1111

12-
assert(dateTimeSplitIdx ~= nil, "Missing date & time separator 'T' in iso 8601 string")
13-
assert(
14-
timezoneSplitIdx ~= nil or timezoneZeroedIdx ~= nil,
15-
"Missing timezone separator '+' or 'Z' in iso date string"
16-
)
12+
assert(dateTimeSplitIdx ~= nil, "Missing date & time separator 'T' in RFC 3339 string")
13+
assert(timezoneSplitIdx ~= nil or timezoneZeroedIdx ~= nil, "Missing timezone separator '+' or 'Z' in RFC 3339 string")
1714

1815
-- Split date (before T) by dashes, split time (after T, before + or Z)
1916
-- by colons, we should then get 3 substrings for each of date & time
2017

21-
local dateParts = string.split(string.sub(nowIso, 1, dateTimeSplitIdx - 1), "-")
22-
local timeParts = string.split(
23-
string.sub(
24-
nowIso,
25-
dateTimeSplitIdx + 1,
26-
((timezoneSplitIdx or timezoneZeroedIdx) :: number) - 1
27-
),
28-
":"
29-
)
18+
local dateParts = string.split(string.sub(nowRfc, 1, dateTimeSplitIdx - 1), "-")
19+
local timeParts =
20+
string.split(string.sub(nowRfc, dateTimeSplitIdx + 1, ((timezoneSplitIdx or timezoneZeroedIdx) :: number) - 1), ":")
3021

31-
assert(#dateParts == 3, "Date partial of iso 8601 should consist of 3 substrings, separated by '-'")
32-
assert(#timeParts == 3, "Time partial of iso 8601 should consist of 3 substrings, separated by ':'")
22+
assert(#dateParts == 3, "Date partial of RFC 3339 should consist of 3 substrings, separated by '-'")
23+
assert(#timeParts == 3, "Time partial of RFC 3339 should consist of 3 substrings, separated by ':'")
3324

3425
-- date should be in format YYYY:MM::DD
3526
-- time should be in format HH:MM:SS with optional fraction for seconds
@@ -51,22 +42,13 @@ assert(
5142

5243
if timezoneZeroedIdx ~= nil then
5344
-- No timezone offset
54-
assert(
55-
timezoneZeroedIdx == #nowIso,
56-
"Timezone specifier 'Z' must be at the last character in iso 8601 string"
57-
)
45+
assert(timezoneZeroedIdx == #nowRfc, "Timezone specifier 'Z' must be at the last character in RFC 3339 string")
5846
elseif timezoneSplitIdx ~= nil then
5947
-- Timezone offset
60-
local timezoneParts = string.split(string.sub(nowIso, timezoneSplitIdx + 1), ":")
48+
local timezoneParts = string.split(string.sub(nowRfc, timezoneSplitIdx + 1), ":")
6149
assert(#timezoneParts == 2, "Timezone partial should consist of 2 substings, separated by ':'")
62-
assert(
63-
string.match(timezoneParts[1], "^%d%d$"),
64-
"Timezone partial should have 2 digits for hour"
65-
)
66-
assert(
67-
string.match(timezoneParts[2], "^%d%d$"),
68-
"Timezone partial should have 2 digits for minute"
69-
)
50+
assert(string.match(timezoneParts[1], "^%d%d$"), "Timezone partial should have 2 digits for hour")
51+
assert(string.match(timezoneParts[2], "^%d%d$"), "Timezone partial should have 2 digits for minute")
7052
else
7153
error("unreachable")
7254
end
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
local DateTime = require("@lune/datetime")
22

3-
local values = DateTime.fromIsoDate("2023-08-27T05:54:19Z"):toLocalTime()
3+
local values = DateTime.fromRfc3339("2023-08-27T05:54:19Z"):toLocalTime()
44

55
local expectedDateTimeValues = os.date("*t", 1693115659)
66

7-
assert(
8-
values.year == expectedDateTimeValues.year,
9-
`expected {values.year} == {expectedDateTimeValues.year}`
10-
)
11-
assert(
12-
values.month == expectedDateTimeValues.month,
13-
`expected {values.month} == {expectedDateTimeValues.month}`
14-
)
15-
assert(
16-
values.day == expectedDateTimeValues.day,
17-
`expected {values.day} == {expectedDateTimeValues.day}`
18-
)
19-
assert(
20-
values.hour == expectedDateTimeValues.hour,
21-
`expected {values.hour} == {expectedDateTimeValues.hour}`
22-
)
23-
assert(
24-
values.minute == expectedDateTimeValues.min,
25-
`expected {values.minute} == {expectedDateTimeValues.min}`
26-
)
27-
assert(
28-
values.second == expectedDateTimeValues.sec,
29-
`expected {values.second} == {expectedDateTimeValues.sec}`
30-
)
7+
assert(values.year == expectedDateTimeValues.year, `expected {values.year} == {expectedDateTimeValues.year}`)
8+
assert(values.month == expectedDateTimeValues.month, `expected {values.month} == {expectedDateTimeValues.month}`)
9+
assert(values.day == expectedDateTimeValues.day, `expected {values.day} == {expectedDateTimeValues.day}`)
10+
assert(values.hour == expectedDateTimeValues.hour, `expected {values.hour} == {expectedDateTimeValues.hour}`)
11+
assert(values.minute == expectedDateTimeValues.min, `expected {values.minute} == {expectedDateTimeValues.min}`)
12+
assert(values.second == expectedDateTimeValues.sec, `expected {values.second} == {expectedDateTimeValues.sec}`)

types/datetime.luau

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,16 @@ end
163163
@param locale -- The locale the time should be formatted in
164164
@return string -- The formatting string
165165
]=]
166-
function DateTime.formatUniversalTime(
167-
self: DateTime,
168-
formatString: string?,
169-
locale: Locale?
170-
): string
166+
function DateTime.formatUniversalTime(self: DateTime, formatString: string?, locale: Locale?): string
171167
return nil :: any
172168
end
173169

174170
--[=[
175171
@within DateTime
176172
@tag Method
177173
174+
**DEPRECATED**: Use `DateTime.toRfc3339` instead.
175+
178176
Formats this `DateTime` as an ISO 8601 date-time string.
179177
180178
Some examples of ISO 8601 date-time strings are:
@@ -207,6 +205,24 @@ function DateTime.toRfc2822(self: DateTime): string
207205
return nil :: any
208206
end
209207

208+
--[=[
209+
@within DateTime
210+
@tag Method
211+
212+
Formats this `DateTime` as an RFC 3339 date-time string.
213+
214+
Some examples of RFC 3339 date-time strings are:
215+
216+
- `2020-02-22T18:12:08Z`
217+
- `2000-01-31T12:34:56+05:00`
218+
- `1970-01-01T00:00:00.055Z`
219+
220+
@return string -- The RFC 3339 formatted string
221+
]=]
222+
function DateTime.toRfc3339(self: DateTime): string
223+
return nil :: any
224+
end
225+
210226
--[=[
211227
@within DateTime
212228
@tag Method
@@ -270,8 +286,8 @@ export type DateTime = typeof(DateTime)
270286
-- Creates a DateTime for the current exact moment in time
271287
local now = DateTime.now()
272288
273-
-- Formats the current moment in time as an ISO 8601 string
274-
print(now:toIsoDate())
289+
-- Formats the current moment in time as an RFC 3339 string
290+
print(now:toRfc3339())
275291
276292
-- Formats the current moment in time as an RFC 2822 string
277293
print(now:toRfc2822())
@@ -415,8 +431,9 @@ end
415431
@within DateTime
416432
@tag Constructor
417433
434+
**DEPRECATED**: Use `DateTime.fromRfc3339` instead.
435+
418436
Creates a new `DateTime` from an ISO 8601 date-time string.
419-
This function behaves the same as `fromRfc3339`.
420437
421438
### Errors
422439

0 commit comments

Comments
 (0)