Skip to content

Commit

Permalink
fix: take DST into account in augend.date["%H:%M"]
Browse files Browse the repository at this point in the history
When trying to increase/decrease a time from a Europe/Paris time zone,
dial cannot find the time pattern because `os.date` will take DST into
account and will return a time increased by 1 hour.

By preserving DST value from the host, consistency is ensured between
the calls to `os.date`.

Run the date_spec.lua file with different time zones in order to
correctly test DST during the whole year.

Link: https://en.wikipedia.org/wiki/Daylight_saving_time_by_country
Signed-off-by: Thomas Faivre <[email protected]>
  • Loading branch information
ThomasFaivre committed Aug 24, 2023
1 parent c6657df commit 2ac35b6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions lua/dial/augend/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ function DateFormat:find(line, cursor)
local now = os.date("*t", os.time()) --[[@as osdate]]
dt_info.month = now.month
dt_info.year = now.year
dt_info.isdst = now.isdst
end

local datekind = self.default_kind
Expand Down
44 changes: 42 additions & 2 deletions run
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
#!/usr/bin/env bash

TESTS_INIT=tests/minimal_init.lua
TESTS_DIR=tests/
TESTS_DIR=tests

function _test_tz {
# DST always in effect
echo "Checking with $1 time zone (DST always enable)"
TZ=$1
export TZ
nvim \
--headless \
--noplugin \
-u ${TESTS_INIT} \
-c "PlenaryBustedDirectory ${TESTS_DIR}/dial/augend/date_spec.lua \
{ minimal_init = '${TESTS_INIT}' }"
}

function test {

# Force TimeZone to ensure consistent behavior of os.date regarding Daylight
# Saving Time

# Standard case (No DST)
# Full test case
TZ='GMT+0'
export TZ
nvim \
--headless \
--noplugin \
-u ${TESTS_INIT} \
-c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }"
-c "PlenaryBustedDirectory ${TESTS_DIR} \
{ minimal_init = '${TESTS_INIT}' }" ||
return 1

# permanent DST: os.date always returns `isdst == false`
_test_tz "Asia/Tokyo" || return 1

# Check standard DST. Since the date is not controlled, we might have
# periods of time without any timezone with `isdst == true`.
# Therefore, select timezones that overlap so that DST is actually
# tested.

# DST in Northern hemisphere
# Start: Second Sunday in March at 2:00
# End: First Sunday in November at 2:00
_test_tz "America/New_York" || return 1

# DST in Southern hemisphere
# Start: First Sunday in October at 2:00
# End: First Sunday in April at 3:00
_test_tz "Australia/Sidney" || return 1

}


Expand Down
16 changes: 16 additions & 0 deletions tests/dial/augend/date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ describe([[Test of date with format "%Y年%-m月%-d日(%ja)":]], function()
end)
end)

describe([[Test of date with format %H:%M:]], function()
local augend = date.alias["%H:%M"]
describe("find function", function()
it("can find dates in a given format with Time Zones", function()
assert.are.same(augend:find_stateful("date: 11:50", 1), { from = 7, to = 11 })
assert.are.same(augend.kind, "min")
assert.are.same(augend:find_stateful("date: 11:50", 6), { from = 7, to = 11 })
assert.are.same(augend.kind, "min")
assert.are.same(augend:find_stateful("date: 11:50", 7), { from = 7, to = 11 })
assert.are.same(augend.kind, "hour")
assert.are.same(augend:find_stateful("date: 11:50", 10), { from = 7, to = 11 })
assert.are.same(augend.kind, "min")
end)
end)
end)

describe([[Test of clamp & end_sensitive option:]], function()
describe("{clamp = false and end_sensitive = false}", function()
local augend = date.new {
Expand Down

0 comments on commit 2ac35b6

Please sign in to comment.