Skip to content

Commit 206ef31

Browse files
committed
Fix lua-tz tests using examples from luatz github repository
1 parent dfc81c4 commit 206ef31

1 file changed

Lines changed: 59 additions & 70 deletions

File tree

tests/packaging/lua-tz.lua

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,67 @@
11
#!/usr/bin/env lua
22

3-
-- Check if the module can be loaded
4-
local status, luatz = pcall(require, 'luatz')
3+
-- Examples from the luatz repository
54

6-
if not status then
7-
print("ERROR: Unable to load luatz module")
8-
print(luatz)
9-
os.exit(1)
10-
end
11-
12-
print("✓ luatz module loaded successfully")
13-
14-
-- Test basic functionality: get current time
15-
local ok, now = pcall(function()
16-
return luatz.time()
17-
end)
18-
19-
if not ok then
20-
print("ERROR: Unable to get current time")
21-
print(now)
22-
os.exit(1)
23-
end
24-
25-
print("✓ Current time retrieved successfully: " .. now)
26-
27-
-- Test timezone parsing
28-
local ok, tz = pcall(function()
29-
return luatz.time_in(nil)
30-
end)
31-
32-
if not ok then
33-
print("ERROR: Unable to create time_in object")
34-
print(tz)
35-
os.exit(1)
36-
end
37-
38-
print("✓ time_in object created successfully")
39-
40-
-- Test timestamp conversion
41-
local ok, ts = pcall(function()
42-
local t = luatz.timetable()
43-
t.year = 2024
44-
t.month = 1
45-
t.day = 1
46-
t.hour = 0
47-
t.min = 0
48-
t.sec = 0
49-
return t:timestamp()
50-
end)
51-
52-
if not ok then
53-
print("ERROR: Unable to convert timetable to timestamp")
54-
print(ts)
55-
os.exit(1)
56-
end
57-
58-
print("✓ Timetable to timestamp conversion successful: " .. ts)
5+
local luatz = require "luatz"
596

60-
-- Test timestamp parsing
61-
local ok, tt = pcall(function()
62-
return luatz.timetable.new_from_timestamp(ts)
63-
end)
64-
65-
if not ok then
66-
print("ERROR: Unable to parse timestamp")
67-
print(tt)
68-
os.exit(1)
7+
-- We do this a few times ==> Convert a timestamp to timetable and normalise
8+
local function ts2tt(ts)
9+
return luatz.timetable.new_from_timestamp(ts)
6910
end
7011

71-
if tt.year ~= 2024 or tt.month ~= 1 or tt.day ~= 1 then
72-
print("ERROR: Parsed values do not match")
73-
os.exit(1)
12+
-- Get the current time in UTC
13+
local utcnow = luatz.time()
14+
local now = ts2tt(utcnow)
15+
print(now, "now (UTC)")
16+
17+
-- Get a new time object 6 months from now
18+
local x = now:clone()
19+
x.month = x.month + 6
20+
x:normalise()
21+
print(x, "6 months from now")
22+
23+
-- Find out what time it is in Melbourne at the moment
24+
local melbourne = luatz.get_tz("Australia/Melbourne")
25+
local now_in_melbourne = ts2tt(melbourne:localise(utcnow))
26+
print(now_in_melbourne, "Melbourne")
27+
28+
-- Six months from now in melbourne (so month is incremented; but still the same time)
29+
local m = now_in_melbourne:clone()
30+
m.month = m.month + 6
31+
m:normalise()
32+
print(m, "6 months from now in melbourne")
33+
34+
-- Convert time back to utc; a daylight savings transition may have taken place!
35+
-- There may be 2 results, but for we'll ignore the second possibility
36+
local c, _ = melbourne:utctime(m:timestamp())
37+
print(ts2tt(c), "6 months from now in melbourne converted to utc")
38+
39+
40+
--[[
41+
Re-implementation of `os.date` from the standard lua library
42+
]]
43+
44+
local gettime = require "luatz.gettime".gettime
45+
local new_from_timestamp = require "luatz.timetable".new_from_timestamp
46+
local get_tz = require "luatz.tzcache".get_tz
47+
48+
local function os_date(format_string, timestamp)
49+
format_string = format_string or "%c"
50+
timestamp = timestamp or gettime()
51+
if format_string:sub(1, 1) == "!" then -- UTC
52+
format_string = format_string:sub(2)
53+
else -- Localtime
54+
timestamp = get_tz():localise(timestamp)
55+
end
56+
local tt = new_from_timestamp(timestamp)
57+
if format_string == "*t" then
58+
return tt
59+
else
60+
return tt:strftime(format_string)
61+
end
7462
end
7563

76-
print("✓ Timestamp parsing successful")
77-
78-
print("\nAll tests passed - lua-tz is working correctly!")
64+
print(os_date())
65+
print(os_date("%Y-%m-%d %H:%M:%S"))
66+
print(os_date("!%Y-%m-%d %H:%M:%S", utcnow))
67+
print(os_date("*t", utcnow + 3600 * 24 * 30))

0 commit comments

Comments
 (0)