From 64928f1a50a1a276a718491ae3eeef63abcdb393 Mon Sep 17 00:00:00 2001 From: sternenseemann Date: Sun, 26 Sep 2021 20:12:08 +0200 Subject: [PATCH] testTzSys: fix test suite for non-glibc when using TZDIR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If TZDIR was set to a directory differing from the system timezone directory, the testTZSys test suite would fail on certain timezones in certain configurations. We discovered this when building the tz packaged in nixpkgs on macOS: We would provide our packaged tzdata via TZDIR, but /usr/share/zoneinfo would still be accessible. Since Apple's tzdata seem to be outdated, the tests fail consistently. This is because the tzset(3) implementation of Apple's libSystem is “only” POSIX-conforming and doesn't support the glibc invention of TZDIR. They are not alone with this -- musl doesn't support it as well. To work around this we can use POSIX behavior, however: If TZ is prefixed with a ':', we can pass an absolute path and rely on the lookup behavior implemented in tz itself. --- tests/testTZSys.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/testTZSys.hs b/tests/testTZSys.hs index 5bda7a0..d3fd044 100644 --- a/tests/testTZSys.hs +++ b/tests/testTZSys.hs @@ -9,6 +9,7 @@ import Data.Int import Data.Time import Data.Time.Clock.POSIX import Data.Time.Zones +import Data.Time.Zones.Read (pathForSystemTZ) import System.Environment import System.IO.Unsafe import Test.Framework.Providers.QuickCheck2 @@ -21,7 +22,13 @@ foreign import ccall safe "time.h tzset" c_tzset :: IO () setupTZ :: String -> IO TZ setupTZ zoneName = do - setEnv "TZ" zoneName + -- tzset(3) doesn't necessarily understand TZDIR, since it is + -- not mandated by POSIX. To avoid trouble with non-glibc + -- libc implementations (musl, libSystem, ...) we set TZ + -- to an absolute path (with a leading ':') which all + -- POSIX-conforming implementations must support. + tzPath <- pathForSystemTZ zoneName + setEnv "TZ" $ ":" ++ tzPath c_tzset loadSystemTZ zoneName