mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
chore(tests): include local formatTime test with custom locale
This commit is contained in:
parent
829494e304
commit
a5efedcaad
2 changed files with 41 additions and 18 deletions
|
@ -1,4 +1,5 @@
|
|||
local DateTime = require("@lune/DateTime")
|
||||
local process = require("@lune/Process")
|
||||
|
||||
-- UTC Timezone
|
||||
assert(
|
||||
|
@ -10,20 +11,42 @@ assert(
|
|||
assert(
|
||||
DateTime.fromUnixTimestamp(1693068988):formatTime("utc", "%A, %d %B %Y", "fr")
|
||||
== "samedi, 26 août 2023",
|
||||
"expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr'"
|
||||
"expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr' (UTC)"
|
||||
)
|
||||
|
||||
local expectedTimeString = os.date("%Y-%m-%dT%H:%M:%SZ", 1694078954)
|
||||
|
||||
print(
|
||||
expectedTimeString,
|
||||
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
|
||||
)
|
||||
|
||||
assert(
|
||||
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
|
||||
== expectedTimeString,
|
||||
"invalid ISO 8601 formatting for DateTime.formatTime() (local)"
|
||||
)
|
||||
|
||||
-- TODO: Locale tests, by using os.date()
|
||||
-- This test requires 'fr_FR.UTF-8 UTF-8' to be in /etc/locale.gen to pass
|
||||
-- Local Timezone
|
||||
|
||||
assert(
|
||||
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
|
||||
== expectedTimeString,
|
||||
"invalid ISO 8601 formatting for DateTime.formatTime() (local)"
|
||||
)
|
||||
|
||||
local expectedLocalizedString
|
||||
|
||||
local dateCmd = process.spawn("bash", { "-c", "date +\"%A, %d %B %Y\" --date='@1693068988'" }, {
|
||||
env = {
|
||||
LC_ALL = "fr_FR.UTF-8 ",
|
||||
},
|
||||
})
|
||||
|
||||
if dateCmd.ok then
|
||||
expectedLocalizedString = dateCmd.stdout
|
||||
else
|
||||
error("Failed to execute date command")
|
||||
end
|
||||
|
||||
assert(
|
||||
DateTime.fromUnixTimestamp(1693068988):formatTime("local", "%A, %d %B %Y", "fr")
|
||||
== expectedLocalizedString:gsub("\n", ""),
|
||||
"expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr' (local)"
|
||||
)
|
||||
|
|
|
@ -42,11 +42,6 @@ export type DateTimeValues = {
|
|||
millisecond: number,
|
||||
}
|
||||
|
||||
export type DateTime = {
|
||||
unixTimestamp: number,
|
||||
unixTimestampMillis: number,
|
||||
}
|
||||
|
||||
--[=[
|
||||
@class DateTime
|
||||
|
||||
|
@ -85,7 +80,10 @@ export type DateTime = {
|
|||
DateTime.now():toUniversalTime()
|
||||
```
|
||||
]=]
|
||||
local dateTime = {}
|
||||
local dateTime = {
|
||||
unixTimestamp = 0,
|
||||
unixTimestampMillis = 0,
|
||||
}
|
||||
|
||||
--[=[
|
||||
@within DateTime
|
||||
|
@ -94,7 +92,7 @@ local dateTime = {}
|
|||
|
||||
@return A DateTime instance
|
||||
]=]
|
||||
function dateTime.now(): DateTime
|
||||
function dateTime.now(): typeof(dateTime)
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -107,7 +105,7 @@ end
|
|||
@param unixTimestamp The number of seconds or milliseconds (or both) since the Unix epoch. The fraction part of a float denotes the milliseconds.
|
||||
@return A DateTime instance
|
||||
]=]
|
||||
function dateTime.fromUnixTimestamp(unixTimestamp: number?): DateTime
|
||||
function dateTime.fromUnixTimestamp(unixTimestamp: number?): typeof(dateTime)
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -126,7 +124,7 @@ end
|
|||
@param dateTime Optional values for the dateTime instance, defaults to the current time
|
||||
@return A DateTime instance
|
||||
]=]
|
||||
function dateTime.fromUniversalTime(dateTime: DateTimeValues?): DateTime
|
||||
function dateTime.fromUniversalTime(dateTime: DateTimeValues?): typeof(dateTime)
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -145,7 +143,7 @@ end
|
|||
@param dateTime Optional values for the dateTime instance, defaults to the current time
|
||||
@return A DateTime instance
|
||||
]=]
|
||||
function dateTime.fromLocalTime(dateTime: DateTimeValues?): DateTime
|
||||
function dateTime.fromLocalTime(dateTime: DateTimeValues?): typeof(dateTime)
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -162,7 +160,7 @@ end
|
|||
@param isoDate An ISO 8601 formatted string
|
||||
@return A DateTime instance
|
||||
]=]
|
||||
function dateTime.fromIsoDate(iso_date: string): DateTime?
|
||||
function dateTime.fromIsoDate(iso_date: string): typeof(dateTime)?
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -225,4 +223,6 @@ function dateTime:formatTime(timezone: Timezone, formatString: string, locale: L
|
|||
return nil :: any
|
||||
end
|
||||
|
||||
export type DateTime = typeof(dateTime)
|
||||
|
||||
return dateTime
|
||||
|
|
Loading…
Add table
Reference in a new issue