Script 'mail_helper' called by obssrc Hello community, here is the log from the commit of package ghc-unix-time for openSUSE:Factory checked in at 2026-11-24 12:36:45 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/ghc-unix-time (Old) and /work/SRC/openSUSE:Factory/.ghc-unix-time.new.1340718 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "ghc-unix-time" Tue Nov 24 12:36:45 2026 rev:27 rq:1399389 version:0.6.0 Changes: -------- --- /work/SRC/openSUSE:Factory/ghc-unix-time/ghc-unix-time.changes 2025-07-02 12:08:29.112170787 +0200 +++ /work/SRC/openSUSE:Factory/.ghc-unix-time.new.1340718/ghc-unix-time.changes 2026-11-24 12:43:34.897049395 +0100 @@ -1,0 +2,42 @@ +Mon Nov 2 08:24:59 UTC 2026 - Peter Simons <[email protected]> + +- Update unix-time to version 0.6.0. + ## v0.6.0 + + This release changes the results of some functions without changing + their types. Please check the following before upgrading. + + * `UnixDiffTime` is normalized so that both fields have the same sign. + For example, -0.5 seconds is now `UnixDiffTime 0 (-500000)` + instead of `UnixDiffTime (-1) 500000`. + Code checking the sign with `udtSeconds d < 0` must use `d < 0` + (or `signum`) instead. + * `parseUnixTime` takes DST into account. Results during DST were + one hour off. + * `parseUnixTime` and `parseUnixTimeGMT` honor the offset of `%z` + on glibc. It was ignored. + * The GMT functions no longer modify the `TZ` environment variable, + which could be corrupted by concurrent calls. So they no longer + call `tzset()` as a side effect. + * `addUnixDiffTime` normalizes `utMicroSeconds` into `[0, 1000000)` + also for times before 1970. + * The `Binary` instance of `UnixTime` always encodes seconds as + `Int64`. Data encoded on 32-bit platforms by older versions cannot + be decoded. + + Other changes: + + * Security: `formatUnixTime` and `formatUnixTimeGMT` read beyond a + heap buffer when the output exceeded 80 bytes. Now the buffer grows. + * New: `parseUnixTimeMaybe` and `parseUnixTimeGMTMaybe`. + * Fix `*`, `negate`, `abs` and `signum` of `UnixDiffTime`. + * Use `strptime_l()`/`strftime_l()` on Linux instead of changing + the process-global locale. + * Require `base` >= 4.9. + + ## v0.5.0 + + * Remove deprecated `old-time` dependency by dropping `fromClockTime` and `toClockTime` + [#68](https://github.com/kazu-yamamoto/unix-time/pull/68) + +------------------------------------------------------------------- Old: ---- unix-time-0.4.17.tar.gz New: ---- unix-time-0.6.0.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ ghc-unix-time.spec ++++++ --- /var/tmp/diff_new_pack.8NWBCx/_old 2026-11-24 12:43:36.981137714 +0100 +++ /var/tmp/diff_new_pack.8NWBCx/_new 2026-11-24 12:43:36.984137841 +0100 @@ -1,7 +1,7 @@ # # spec file for package ghc-unix-time # -# Copyright (c) 2025 SUSE LLC +# Copyright (c) 2026 SUSE LLC and contributors # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -17,38 +17,39 @@ %global pkg_name unix-time -%global pkgver %{pkg_name}-%{version} +%bcond_with ghc_prof %bcond_with tests -Name: ghc-%{pkg_name} -Version: 0.4.17 +Name: ghc-%pkg_name +Version: 0.6.0 Release: 0 +%global pkgver %pkg_name-%{version} Summary: Unix time parser/formatter and utilities License: BSD-3-Clause -URL: https://hackage.haskell.org/package/%{pkg_name} -Source0: https://hackage.haskell.org/package/%{pkg_name}-%{version}/%{pkg_name}-%{version}.tar.gz +URL: https://hackage.haskell.org/package/%pkg_name +Source0: https://hackage.haskell.org/package/%pkg_name-%{version}/%pkg_name-%{version}.tar.gz BuildRequires: ghc-Cabal-devel BuildRequires: ghc-base-devel -BuildRequires: ghc-base-prof BuildRequires: ghc-binary-devel -BuildRequires: ghc-binary-prof BuildRequires: ghc-bytestring-devel -BuildRequires: ghc-bytestring-prof -BuildRequires: ghc-old-time-devel -BuildRequires: ghc-old-time-prof -BuildRequires: ghc-rpm-macros +BuildRequires: ghc-rpm-macros >= 2.9.2 ExcludeArch: %{ix86} +%if %{with ghc_prof} +BuildRequires: ghc-base-prof +BuildRequires: ghc-binary-prof +BuildRequires: ghc-bytestring-prof +%endif %if %{with tests} BuildRequires: ghc-QuickCheck-devel -BuildRequires: ghc-QuickCheck-prof BuildRequires: ghc-hspec-devel -BuildRequires: ghc-hspec-prof -BuildRequires: ghc-old-locale-devel -BuildRequires: ghc-old-locale-prof BuildRequires: ghc-template-haskell-devel -BuildRequires: ghc-template-haskell-prof BuildRequires: ghc-time-devel +%if %{with ghc_prof} +BuildRequires: ghc-QuickCheck-prof +BuildRequires: ghc-hspec-prof +BuildRequires: ghc-template-haskell-prof BuildRequires: ghc-time-prof %endif +%endif %description Fast parser/formatter/utilities for Unix time. @@ -63,24 +64,27 @@ %description devel This package provides the Haskell %{pkg_name} library development files. -%package -n ghc-%{pkg_name}-doc +%package -n ghc-%pkg_name-doc Summary: Haskell %{pkg_name} library documentation Requires: ghc-filesystem BuildArch: noarch -%description -n ghc-%{pkg_name}-doc +%description -n ghc-%pkg_name-doc This package provides the Haskell %{pkg_name} library documentation. -%package -n ghc-%{pkg_name}-prof +%if %{with ghc_prof} +%package -n ghc-%pkg_name-prof Summary: Haskell %{pkg_name} profiling library -Requires: ghc-%{pkg_name}-devel = %{version}-%{release} -Supplements: (ghc-%{pkg_name}-devel and ghc-prof) +Requires: ghc-%pkg_name-devel = %{version}-%{release} +Supplements: (ghc-%pkg_name-devel and ghc-prof) -%description -n ghc-%{pkg_name}-prof +%description -n ghc-%pkg_name-prof This package provides the Haskell %{pkg_name} profiling library. +%endif + %prep -%autosetup -n %{pkg_name}-%{version} +%autosetup -p1 -n %pkg_name-%{version} %build %ghc_lib_build @@ -103,8 +107,10 @@ %files devel -f %{name}-devel.files %doc ChangeLog.md -%files -n ghc-%{pkg_name}-doc -f ghc-%{pkg_name}-doc.files +%files -n ghc-%pkg_name-doc -f ghc-%pkg_name-doc.files %license LICENSE -%files -n ghc-%{pkg_name}-prof -f ghc-%{pkg_name}-prof.files +%if %{with ghc_prof} +%files -n ghc-%pkg_name-prof -f ghc-%pkg_name-prof.files +%endif ++++++ unix-time-0.4.17.tar.gz -> unix-time-0.6.0.tar.gz ++++++ ++++ 6300 lines of diff (skipped) ++++ retrying with extended exclude list diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/ChangeLog.md new/unix-time-0.6.0/ChangeLog.md --- old/unix-time-0.4.17/ChangeLog.md 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/ChangeLog.md 2001-09-09 03:46:40.000000000 +0200 @@ -1,5 +1,43 @@ # ChangeLog for unix-time +## v0.6.0 + +This release changes the results of some functions without changing +their types. Please check the following before upgrading. + +* `UnixDiffTime` is normalized so that both fields have the same sign. + For example, -0.5 seconds is now `UnixDiffTime 0 (-500000)` + instead of `UnixDiffTime (-1) 500000`. + Code checking the sign with `udtSeconds d < 0` must use `d < 0` + (or `signum`) instead. +* `parseUnixTime` takes DST into account. Results during DST were + one hour off. +* `parseUnixTime` and `parseUnixTimeGMT` honor the offset of `%z` + on glibc. It was ignored. +* The GMT functions no longer modify the `TZ` environment variable, + which could be corrupted by concurrent calls. So they no longer + call `tzset()` as a side effect. +* `addUnixDiffTime` normalizes `utMicroSeconds` into `[0, 1000000)` + also for times before 1970. +* The `Binary` instance of `UnixTime` always encodes seconds as + `Int64`. Data encoded on 32-bit platforms by older versions cannot + be decoded. + +Other changes: + +* Security: `formatUnixTime` and `formatUnixTimeGMT` read beyond a + heap buffer when the output exceeded 80 bytes. Now the buffer grows. +* New: `parseUnixTimeMaybe` and `parseUnixTimeGMTMaybe`. +* Fix `*`, `negate`, `abs` and `signum` of `UnixDiffTime`. +* Use `strptime_l()`/`strftime_l()` on Linux instead of changing + the process-global locale. +* Require `base` >= 4.9. + +## v0.5.0 + +* Remove deprecated `old-time` dependency by dropping `fromClockTime` and `toClockTime` + [#68](https://github.com/kazu-yamamoto/unix-time/pull/68) + ## v0.4.17 * Fix MinGW64 linker error diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/Data/UnixTime/Conv.hs new/unix-time-0.6.0/Data/UnixTime/Conv.hs --- old/unix-time-0.4.17/Data/UnixTime/Conv.hs 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/Data/UnixTime/Conv.hs 2001-09-09 03:46:40.000000000 +0200 @@ -6,34 +6,39 @@ formatUnixTimeGMT, parseUnixTime, parseUnixTimeGMT, + parseUnixTimeMaybe, + parseUnixTimeGMTMaybe, webDateFormat, mailDateFormat, fromEpochTime, - toEpochTime, - fromClockTime, - toClockTime, + toEpochTime ) where -import Control.Applicative -import Data.ByteString.Char8 -import Data.ByteString.Unsafe +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as BS +import Data.ByteString.Internal (createAndTrim) +import Data.Char (isDigit) import Data.UnixTime.Types import Foreign.C.String import Foreign.C.Types -import Foreign.Marshal.Alloc +import Foreign.Marshal.Alloc (alloca) +import Foreign.Ptr (Ptr, castPtr) +import Foreign.Storable (peek) import System.IO.Unsafe (unsafePerformIO) import System.Posix.Types (EpochTime) -import System.Time (ClockTime (..)) -- $setup -- >>> import Data.Function (on) -- >>> :set -XOverloadedStrings +foreign import ccall unsafe "c_init_locale" + c_init_locale :: IO () + foreign import ccall unsafe "c_parse_unix_time" - c_parse_unix_time :: CString -> CString -> IO CTime + c_parse_unix_time :: CString -> CString -> Ptr CInt -> IO CTime foreign import ccall unsafe "c_parse_unix_time_gmt" - c_parse_unix_time_gmt :: CString -> CString -> IO CTime + c_parse_unix_time_gmt :: CString -> CString -> Ptr CInt -> IO CTime foreign import ccall unsafe "c_format_unix_time" c_format_unix_time :: CString -> CTime -> CString -> CInt -> IO CSize @@ -41,34 +46,74 @@ foreign import ccall unsafe "c_format_unix_time_gmt" c_format_unix_time_gmt :: CString -> CTime -> CString -> CInt -> IO CSize +-- The C side must initialize its locale exactly once before use. +localeReady :: () +localeReady = unsafePerformIO c_init_locale +{-# NOINLINE localeReady #-} + ---------------------------------------------------------------- +type CParser = CString -> CString -> Ptr CInt -> IO CTime + +parseHelper :: CParser -> Format -> ByteString -> (UnixTime, Bool) +parseHelper parser fmt str = localeReady `seq` unsafePerformIO ( + BS.useAsCString fmt $ \cfmt -> + BS.useAsCString str $ \cstr -> + alloca $ \pok -> do + sec <- parser cfmt cstr pok + ok <- peek pok + -- A NUL byte would silently truncate the C strings. + let complete = BS.notElem '\0' fmt && BS.notElem '\0' str + return (UnixTime sec 0, ok /= 0 && complete)) + +toMaybe :: (UnixTime, Bool) -> Maybe UnixTime +toMaybe (ut, True) = Just ut +toMaybe _ = Nothing + -- | -- Parsing 'ByteString' to 'UnixTime' interpreting as localtime. -- This is a wrapper for strptime_l(). -- Many implementations of strptime_l() do not support %Z and -- some implementations of strptime_l() do not support %z, either. -- 'utMicroSeconds' is always set to 0. +-- +-- The result is unspecified if parsing fails. +-- Use 'parseUnixTimeMaybe' to detect failures. +-- +-- This depends on the TZ environment variable but is a pure function. +-- So, the result may be shared even if TZ is changed later. parseUnixTime :: Format -> ByteString -> UnixTime -parseUnixTime fmt str = unsafePerformIO $ - useAsCString fmt $ \cfmt -> - useAsCString str $ \cstr -> do - sec <- c_parse_unix_time cfmt cstr - return $ UnixTime sec 0 +parseUnixTime fmt str = fst $ parseHelper c_parse_unix_time fmt str -- | -- Parsing 'ByteString' to 'UnixTime' interpreting as GMT. -- This is a wrapper for strptime_l(). -- 'utMicroSeconds' is always set to 0. -- +-- The result is unspecified if parsing fails. +-- Use 'parseUnixTimeGMTMaybe' to detect failures. +-- -- >>> parseUnixTimeGMT webDateFormat "Thu, 01 Jan 1970 00:00:00 GMT" -- UnixTime {utSeconds = 0, utMicroSeconds = 0} parseUnixTimeGMT :: Format -> ByteString -> UnixTime -parseUnixTimeGMT fmt str = unsafePerformIO $ - useAsCString fmt $ \cfmt -> - useAsCString str $ \cstr -> do - sec <- c_parse_unix_time_gmt cfmt cstr - return $ UnixTime sec 0 +parseUnixTimeGMT fmt str = fst $ parseHelper c_parse_unix_time_gmt fmt str + +-- | +-- Like 'parseUnixTime' but returns 'Nothing' unless the entire input +-- matches the format. +parseUnixTimeMaybe :: Format -> ByteString -> Maybe UnixTime +parseUnixTimeMaybe fmt str = toMaybe $ parseHelper c_parse_unix_time fmt str + +-- | +-- Like 'parseUnixTimeGMT' but returns 'Nothing' unless the entire input +-- matches the format. +-- +-- >>> parseUnixTimeGMTMaybe webDateFormat "Thu, 01 Jan 1970 00:00:00 GMT" +-- Just (UnixTime {utSeconds = 0, utMicroSeconds = 0}) +-- >>> parseUnixTimeGMTMaybe webDateFormat "garbage" +-- Nothing +parseUnixTimeGMTMaybe :: Format -> ByteString -> Maybe UnixTime +parseUnixTimeGMTMaybe fmt str = toMaybe $ parseHelper c_parse_unix_time_gmt fmt str ---------------------------------------------------------------- @@ -103,18 +148,28 @@ -- | -- Helper handling memory allocation for formatUnixTime and formatUnixTimeGMT. +-- A format containing a NUL byte is truncated there. formatUnixTimeHelper :: (CString -> CTime -> CString -> CInt -> IO CSize) -> Format -> UnixTime -> IO ByteString formatUnixTimeHelper formatFun fmt (UnixTime sec _) = - useAsCString fmt $ \cfmt -> do - let siz = 80 - ptr <- mallocBytes siz - len <- fromIntegral <$> formatFun cfmt sec ptr (fromIntegral siz) - ptr' <- reallocBytes ptr (len + 1) - unsafePackMallocCString ptr' -- FIXME: Use unsafePackMallocCStringLen from bytestring-0.10.2.0 + localeReady `seq` BS.useAsCString fmt (\cfmt -> loop cfmt 80) + where + -- strftime() returns 0 if the result does not fit in the buffer. + -- Retry with a larger buffer but give up at a bound + -- which also covers field widths such as "%100Y". + limit :: Int + limit = fromInteger $ min (1024 * 1024) $ + 80 + 64 * toInteger (BS.length fmt) + sum (map (read . BS.unpack) widths) + widths = filter (not . BS.null) $ BS.splitWith (not . isDigit) fmt + loop cfmt siz = do + bs <- createAndTrim siz $ \ptr -> + fromIntegral <$> formatFun cfmt sec (castPtr ptr) (fromIntegral siz) + if BS.null bs && siz < limit + then loop cfmt (siz * 2) + else return bs ---------------------------------------------------------------- @@ -143,19 +198,3 @@ -- From 'UnixTime' to 'EpochTime' ignoring 'utMicroSeconds'. toEpochTime :: UnixTime -> EpochTime toEpochTime (UnixTime sec _) = sec - --- | --- From 'ClockTime' to 'UnixTime'. -fromClockTime :: ClockTime -> UnixTime -fromClockTime (TOD sec psec) = UnixTime sec' usec' - where - sec' = fromIntegral sec - usec' = fromIntegral $ psec `div` 1000000 - --- | --- From 'UnixTime' to 'ClockTime'. -toClockTime :: UnixTime -> ClockTime -toClockTime (UnixTime sec usec) = TOD sec' psec' - where - sec' = truncate (toRational sec) - psec' = fromIntegral $ usec * 1000000 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/Data/UnixTime/Diff.hs new/unix-time-0.6.0/Data/UnixTime/Diff.hs --- old/unix-time-0.4.17/Data/UnixTime/Diff.hs 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/Data/UnixTime/Diff.hs 2001-09-09 03:46:40.000000000 +0200 @@ -16,14 +16,37 @@ ---------------------------------------------------------------- -calc :: CTime -> Int32 -> UnixDiffTime -calc sec usec = uncurry UnixDiffTime . adjust sec $ usec +-- 'UnixDiffTime' is normalized so that both fields share the sign of +-- the value and @|udtMicroSeconds| < 1000000@. +normDiff :: CTime -> Int64 -> UnixDiffTime +normDiff sec usec + | s > 0 && u < 0 = UnixDiffTime (s - 1) (fromIntegral (u + micro)) + | s < 0 && u > 0 = UnixDiffTime (s + 1) (fromIntegral (u - micro)) + | otherwise = UnixDiffTime s (fromIntegral u) + where + (q, u) = usec `quotRem` micro + s = sec + fromIntegral q + +-- 'UnixTime' is normalized so that @0 <= utMicroSeconds < 1000000@ +-- as @struct timeval@. +normTime :: CTime -> Int64 -> UnixTime +normTime sec usec = UnixTime (sec + fromIntegral q) (fromIntegral u) + where + (q, u) = usec `divMod` micro -calc' :: CTime -> Int32 -> UnixDiffTime -calc' sec usec = uncurry UnixDiffTime . slowAdjust sec $ usec +normalize :: UnixDiffTime -> UnixDiffTime +normalize (UnixDiffTime s u) = normDiff s (fromIntegral u) -calcU :: CTime -> Int32 -> UnixTime -calcU sec usec = uncurry UnixTime . adjust sec $ usec +toMicro :: UnixDiffTime -> Integer +toMicro (UnixDiffTime (CTime s) u) = toInteger s * micro + toInteger u + +fromMicro :: Integer -> UnixDiffTime +fromMicro n = UnixDiffTime (fromInteger s) (fromInteger u) + where + (s, u) = n `quotRem` micro + +micro :: Num a => a +micro = 1000000 -- | Arithmetic operations where (1::UnixDiffTime) means 1 second. -- @@ -33,16 +56,26 @@ -- UnixDiffTime {udtSeconds = -3, udtMicroSeconds = 0} -- >>> (3 :: UnixDiffTime) * 2 -- UnixDiffTime {udtSeconds = 6, udtMicroSeconds = 0} +-- >>> microSecondsToUnixDiffTime (1500000 :: Int) * microSecondsToUnixDiffTime (1500000 :: Int) +-- UnixDiffTime {udtSeconds = 2, udtMicroSeconds = 250000} +-- >>> negate (microSecondsToUnixDiffTime (500000 :: Int)) +-- UnixDiffTime {udtSeconds = 0, udtMicroSeconds = -500000} instance Num UnixDiffTime where - UnixDiffTime s1 u1 + UnixDiffTime s2 u2 = calc (s1 + s2) (u1 + u2) - UnixDiffTime s1 u1 - UnixDiffTime s2 u2 = calc (s1 - s2) (u1 - u2) - UnixDiffTime s1 u1 * UnixDiffTime s2 u2 = calc' (s1 * s2) (u1 * u2) - negate (UnixDiffTime s u) = UnixDiffTime (-s) (-u) - abs (UnixDiffTime s u) = UnixDiffTime (abs s) (abs u) - signum (UnixDiffTime s u) - | s == 0 && u == 0 = 0 - | s > 0 = 1 - | otherwise = -1 + UnixDiffTime s1 u1 + UnixDiffTime s2 u2 = + normDiff (s1 + s2) (fromIntegral u1 + fromIntegral u2) + UnixDiffTime s1 u1 - UnixDiffTime s2 u2 = + normDiff (s1 - s2) (fromIntegral u1 - fromIntegral u2) + x * y = fromMicro (toMicro x * toMicro y `quot` micro) + negate (UnixDiffTime s u) = normDiff (negate s) (negate (fromIntegral u)) + abs x = UnixDiffTime (abs s) (abs u) + where + UnixDiffTime s u = normalize x + signum x + | s > 0 || u > 0 = 1 + | s < 0 || u < 0 = -1 + | otherwise = 0 + where + UnixDiffTime s u = normalize x fromInteger i = UnixDiffTime (fromInteger i) 0 {-# RULES "Integral->UnixDiffTime" fromIntegral = secondsToUnixDiffTime #-} @@ -59,14 +92,16 @@ -- >>> UnixTime 100 2000 `diffUnixTime` UnixTime 98 2100 -- UnixDiffTime {udtSeconds = 1, udtMicroSeconds = 999900} diffUnixTime :: UnixTime -> UnixTime -> UnixDiffTime -diffUnixTime (UnixTime s1 u1) (UnixTime s2 u2) = calc (s1 - s2) (u1 - u2) +diffUnixTime (UnixTime s1 u1) (UnixTime s2 u2) = + normDiff (s1 - s2) (fromIntegral u1 - fromIntegral u2) -- | Adding difference to 'UnixTime'. -- -- >>> UnixTime 100 2000 `addUnixDiffTime` microSecondsToUnixDiffTime ((-1003000) :: Int) -- UnixTime {utSeconds = 98, utMicroSeconds = 999000} addUnixDiffTime :: UnixTime -> UnixDiffTime -> UnixTime -addUnixDiffTime (UnixTime s1 u1) (UnixDiffTime s2 u2) = calcU (s1 + s2) (u1 + u2) +addUnixDiffTime (UnixTime s1 u1) (UnixDiffTime s2 u2) = + normTime (s1 + s2) (fromIntegral u1 + fromIntegral u2) -- | Creating difference from seconds. -- @@ -83,38 +118,15 @@ -- -- >>> microSecondsToUnixDiffTime ((-12345678) :: Int) -- UnixDiffTime {udtSeconds = -12, udtMicroSeconds = -345678} +-- +-- >>> microSecondsToUnixDiffTime ((-345678) :: Int) +-- UnixDiffTime {udtSeconds = 0, udtMicroSeconds = -345678} microSecondsToUnixDiffTime :: Integral a => a -> UnixDiffTime -microSecondsToUnixDiffTime usec = calc (fromIntegral s) (fromIntegral u) - where - (s, u) = secondMicro usec +microSecondsToUnixDiffTime = fromMicro . toInteger {-# INLINE microSecondsToUnixDiffTime #-} ---------------------------------------------------------------- -adjust :: CTime -> Int32 -> (CTime, Int32) -adjust sec usec - | sec >= 0 = ajp - | otherwise = ajm - where - micro = 1000000 - mmicro = -micro - ajp - | usec >= micro = (sec + 1, usec - micro) - | usec >= 0 = (sec, usec) - | otherwise = (sec - 1, usec + micro) - ajm - | usec <= mmicro = (sec - 1, usec + micro) - | usec <= 0 = (sec, usec) - | otherwise = (sec + 1, usec - micro) - -slowAdjust :: CTime -> Int32 -> (CTime, Int32) -slowAdjust sec usec = (sec + fromIntegral s, usec - u) - where - (s, u) = secondMicro usec - -secondMicro :: Integral a => a -> (a, a) -secondMicro usec = usec `quotRem` 1000000 - toFractional :: Fractional a => UnixDiffTime -> a toFractional (UnixDiffTime s u) = realToFrac s + realToFrac u / 1000000 {-# SPECIALIZE toFractional :: UnixDiffTime -> Double #-} diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/Data/UnixTime/Types.hsc new/unix-time-0.6.0/Data/UnixTime/Types.hsc --- old/unix-time-0.4.17/Data/UnixTime/Types.hsc 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/Data/UnixTime/Types.hsc 2001-09-09 03:46:40.000000000 +0200 @@ -2,7 +2,6 @@ module Data.UnixTime.Types where -import Control.Applicative ((<$>), (<*>)) import Data.ByteString import Data.ByteString.Char8 () import Data.Int @@ -53,6 +52,7 @@ -- we must be careful about the layout of its foreign memory. -- -- Here we try use Word32, rather than Int32, to support as large value as possible. + -- So, times before 1970 cannot be represented. -- For example, we use `4294967295` as utSeconds in testsuite, which already exceeds -- the range of Int32, but not for Word32. peek ptr = do @@ -91,10 +91,13 @@ #if __GLASGOW_HASKELL__ >= 704 instance Binary UnixTime where + -- Seconds are always encoded as Int64 to be portable. put (UnixTime (CTime sec) msec) = do - put sec + put (fromIntegral sec :: Int64) put msec - get = UnixTime <$> (CTime `fmap` get) <*> get + get = do + sec <- get :: Get Int64 + UnixTime (CTime (fromIntegral sec)) <$> get #endif -- | @@ -104,14 +107,11 @@ -- | -- Data structure for UnixTime diff. -- --- It is up to the user to ensure that @'udtMicroSeconds' < 1000000@. --- Helpers such as 'Data.UnixTime.microSecondsToUnixDiffTime' can help --- you to create valid values. For example, it's a mistake to use --- 'Data.Text.addUnixDiffTime' with a value @UnixDiffTime 0 9999999@ --- as it will produce an incorrect value back. You should instead use --- functions such as 'Data.UnixTime.microSecondsToUnixDiffTime' to --- create values that are in-range. This avoids any gotchas when then --- doing comparisons. +-- The functions in this library normalize values so that both fields +-- have the same sign and @|'udtMicroSeconds'| < 1000000@. +-- Use 'Data.UnixTime.microSecondsToUnixDiffTime' to create values. +-- The derived 'Eq' and 'Ord' instances are meaningful only for +-- normalized values. data UnixDiffTime = UnixDiffTime { -- | Seconds udtSeconds :: {-# UNPACK #-} !CTime diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/Data/UnixTime.hs new/unix-time-0.6.0/Data/UnixTime.hs --- old/unix-time-0.4.17/Data/UnixTime.hs 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/Data/UnixTime.hs 2001-09-09 03:46:40.000000000 +0200 @@ -8,6 +8,8 @@ -- * Parsing and formatting time parseUnixTime, parseUnixTimeGMT, + parseUnixTimeMaybe, + parseUnixTimeGMTMaybe, formatUnixTime, formatUnixTimeGMT, @@ -25,9 +27,7 @@ -- * Translating time fromEpochTime, - toEpochTime, - fromClockTime, - toClockTime, + toEpochTime ) where import Data.UnixTime.Conv diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/cbits/config.h.in new/unix-time-0.6.0/cbits/config.h.in --- old/unix-time-0.4.17/cbits/config.h.in 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/cbits/config.h.in 2001-09-09 03:46:40.000000000 +0200 @@ -1,43 +1,52 @@ /* cbits/config.h.in. Generated from configure.ac by autoheader. */ -/* Define to 1 if you have the declaration of `_mkgmtime', and to 0 if you +/* Define to 1 if you have the declaration of '_mkgmtime', and to 0 if you don't. */ #undef HAVE_DECL__MKGMTIME /* Define to 1 if you have the <inttypes.h> header file. */ #undef HAVE_INTTYPES_H -/* Define to 1 if you have the <memory.h> header file. */ -#undef HAVE_MEMORY_H - /* Define to 1 if you have the <stdint.h> header file. */ #undef HAVE_STDINT_H +/* Define to 1 if you have the <stdio.h> header file. */ +#undef HAVE_STDIO_H + /* Define to 1 if you have the <stdlib.h> header file. */ #undef HAVE_STDLIB_H +/* Define to 1 if you have the 'strftime_l' function. */ +#undef HAVE_STRFTIME_L + /* Define to 1 if you have the <strings.h> header file. */ #undef HAVE_STRINGS_H /* Define to 1 if you have the <string.h> header file. */ #undef HAVE_STRING_H -/* Define to 1 if you have the `strptime_l' function. */ +/* Define to 1 if you have the 'strptime_l' function. */ #undef HAVE_STRPTIME_L -/* Define to 1 if you have the `strtoll_l' function. */ +/* Define to 1 if you have the 'strtoll_l' function. */ #undef HAVE_STRTOLL_L -/* Define to 1 if you have the `strtol_l' function. */ +/* Define to 1 if you have the 'strtol_l' function. */ #undef HAVE_STRTOL_L +/* Define to 1 if 'tm_gmtoff' is a member of 'struct tm'. */ +#undef HAVE_STRUCT_TM_TM_GMTOFF + +/* Define to 1 if 'tm_zone' is a member of 'struct tm'. */ +#undef HAVE_STRUCT_TM_TM_ZONE + /* Define to 1 if you have the <sys/stat.h> header file. */ #undef HAVE_SYS_STAT_H /* Define to 1 if you have the <sys/types.h> header file. */ #undef HAVE_SYS_TYPES_H -/* Define to 1 if you have the `timegm' function. */ +/* Define to 1 if you have the 'timegm' function. */ #undef HAVE_TIMEGM /* Define to 1 if you have the <unistd.h> header file. */ @@ -46,27 +55,24 @@ /* Define to 1 if you have the <xlocale.h> header file. */ #undef HAVE_XLOCALE_H -/* Define to 1 if you have the `_create_locale' function. */ +/* Define to 1 if you have the '_create_locale' function. */ #undef HAVE__CREATE_LOCALE -/* Define to 1 if you have the `_get_current_locale' function. */ +/* Define to 1 if you have the '_get_current_locale' function. */ #undef HAVE__GET_CURRENT_LOCALE -/* Define to 1 if you have the `_isblank_l' function. */ +/* Define to 1 if you have the '_isblank_l' function. */ #undef HAVE__ISBLANK_L -/* Define to 1 if you have the `_isdigit_l' function. */ +/* Define to 1 if you have the '_isdigit_l' function. */ #undef HAVE__ISDIGIT_L -/* Define to 1 if you have the `_isspace_l' function. */ +/* Define to 1 if you have the '_isspace_l' function. */ #undef HAVE__ISSPACE_L -/* Define to 1 if you have the `_isupper_l' function. */ +/* Define to 1 if you have the '_isupper_l' function. */ #undef HAVE__ISUPPER_L -/* "Is Linux" */ -#undef IS_LINUX - /* "Is Windows NT 6.1" */ #undef IS_NT61 @@ -88,5 +94,7 @@ /* Define to the version of this package. */ #undef PACKAGE_VERSION -/* Define to 1 if you have the ANSI C header files. */ +/* Define to 1 if all of the C89 standard headers exist (not just the ones + required in a freestanding environment). This macro is provided for + backward compatibility; new code need not use it. */ #undef STDC_HEADERS diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/cbits/conv.c new/unix-time-0.6.0/cbits/conv.c --- old/unix-time-0.4.17/cbits/conv.c 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/cbits/conv.c 2001-09-09 03:46:40.000000000 +0200 @@ -1,18 +1,18 @@ #include "config.h" -#if IS_LINUX -/* Linux cheats AC_CHECK_FUNCS(strptime_l), sigh. */ -#define THREAD_SAFE 0 -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE -#define _BSD_SOURCE -#elif HAVE_STRPTIME_L +#if HAVE_STRPTIME_L && HAVE_STRFTIME_L #define THREAD_SAFE 1 #define _GNU_SOURCE #else #define THREAD_SAFE 0 +#if !defined(_WIN32) +#define _XOPEN_SOURCE +#define _DEFAULT_SOURCE +#define _BSD_SOURCE +#endif #endif +#include <limits.h> #include <string.h> #include <time.h> #include <locale.h> @@ -23,169 +23,171 @@ #include "win_patch.h" #endif // _WIN32 +/* + * c_init_locale() must be called once before the other functions. + * The Haskell side guarantees this. + */ #if THREAD_SAFE #if HAVE_XLOCALE_H #include <xlocale.h> #endif -locale_t c_locale = NULL; +static locale_t c_locale = NULL; -void init_locale() { - if (c_locale == NULL) c_locale = newlocale(LC_TIME_MASK, "C", NULL); +void c_init_locale(void) { + c_locale = newlocale(LC_TIME_MASK, "C", NULL); } -#else -void init_locale() { - static int initialized = 0; - if (initialized == 0) { - setlocale(LC_TIME, "C"); - initialized = 1; - } -} -#endif -/* - * Set the value of the TZ environment variable to UTC - * and return the old value. - */ -char *set_tz_utc() { - char *tz; - tz = getenv("TZ"); -#if defined(_WIN32) - _patch_setenv("TZ", "UTC", 1); +#define STRPTIME(src, fmt, tm) strptime_l(src, fmt, tm, c_locale) +#define STRFTIME(dst, siz, fmt, tm) strftime_l(dst, siz, fmt, tm, c_locale) #else - setenv("TZ", "", 1); -#endif -#if defined(HAVE_TZSET) - tzset(); -#endif - return tz; +void c_init_locale(void) { + setlocale(LC_TIME, "C"); } -/* - * Set the value of the TZ environment variable to tz or - * unset the variable if tz is null; - */ -void set_tz(char *local_tz) { - if (local_tz) { +#define STRPTIME(src, fmt, tm) strptime(src, fmt, tm) #if defined(_WIN32) - _patch_setenv("TZ", local_tz, 1); +#define STRFTIME(dst, siz, fmt, tm) _patch_strftime(dst, siz, fmt, tm) #else - setenv("TZ", local_tz, 1); +#define STRFTIME(dst, siz, fmt, tm) strftime(dst, siz, fmt, tm) +#endif // _WIN32 #endif - } else { + #if defined(_WIN32) - _patch_unsetenv("TZ"); +/* Unlike strptime(), this does not convert the result to local time. */ +#define STRPTIME_UTC(src, fmt, tm) _patch_strptime_utc(src, fmt, tm) #else - unsetenv("TZ"); +#define STRPTIME_UTC(src, fmt, tm) STRPTIME(src, fmt, tm) #endif - } -#if defined(HAVE_TZSET) - tzset(); -#endif -} -time_t c_parse_unix_time(char *fmt, char *src) { - struct tm dst; - init_locale(); - memset(&dst, 0, sizeof(struct tm)); -#if THREAD_SAFE - strptime_l(src, fmt, &dst, c_locale); -#else - strptime(src, fmt, &dst); -#endif - return mktime(&dst); +#if !defined(HAVE_TIMEGM) +/* For systems without timegm(), e.g. Solaris. */ +static time_t unix_time_timegm(struct tm *tm) { + /* Normalize the month first, then count days from 1970-01-01. */ + long long y = (long long)tm->tm_year + 1900 + tm->tm_mon / 12; + int m = tm->tm_mon % 12; + long long era, days; + unsigned yoe, doy, doe; + if (m < 0) { m += 12; y--; } + /* days_from_civil() by Howard Hinnant (March-based years). */ + if (m < 2) y--; + era = (y >= 0 ? y : y - 399) / 400; + yoe = (unsigned)(y - era * 400); + doy = (153 * (m + (m < 2 ? 10 : -2)) + 2) / 5; + doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + days = era * 146097 + (long long)doe - 719468 + tm->tm_mday - 1; + return (time_t)(((days * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + + tm->tm_sec); } +#define timegm unix_time_timegm +#endif /* HAVE_TIMEGM */ -#if !defined(HAVE_TIMEGM) -/* This part is for Solaris that doesn't have timegm. - * The copyright notice of timegm and is_leap is as below: - * - * Copyright (c) 1997 Kungliga Tekniska H.gskolan - * (Royal Institute of Technology, Stockholm, Sweden). - * All rights reserved. +/* + * Parsing. + * None of the functions below touch the process-global TZ. */ -static time_t -timegm (struct tm *tm) -{ - static const unsigned ndays[2][12] ={ - {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, - {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; - time_t res = 0; - unsigned i; - - for (i = 70; i < tm->tm_year; ++i) - res += isleap(i + 1900) ? 366 : 365; - - for (i = 0; i < tm->tm_mon; ++i) - res += ndays[isleap(tm->tm_year + 1900)][i]; - res += tm->tm_mday - 1; - res *= 24; - res += tm->tm_hour; - res *= 60; - res += tm->tm_min; - res *= 60; - res += tm->tm_sec; - return res; -} -#endif /* HAVE_TIMEGM */ +#define NO_GMTOFF LONG_MIN -time_t c_parse_unix_time_gmt(char *fmt, char *src) { - struct tm dst; - char *local_tz; - init_locale(); - memset(&dst, 0, sizeof(struct tm)); - local_tz = set_tz_utc(); -#if THREAD_SAFE - strptime_l(src, fmt, &dst, c_locale); -#else - strptime(src, fmt, &dst); +/* Returns 1 if the entire src matches fmt. */ +static int parse(int utc, const char *fmt, const char *src, struct tm *tm) { + char *end; + memset(tm, 0, sizeof(struct tm)); + /* Let mktime() decide whether DST is in effect. */ + tm->tm_isdst = -1; +#if HAVE_STRUCT_TM_TM_GMTOFF + tm->tm_gmtoff = NO_GMTOFF; #endif - set_tz(local_tz); - return timegm(&dst); + end = utc ? STRPTIME_UTC(src, fmt, tm) : STRPTIME(src, fmt, tm); + return end != NULL && *end == '\0'; } -size_t c_format_unix_time(char *fmt, time_t src, char* dst, int siz) { - struct tm tim; - init_locale(); - localtime_r(&src, &tim); -#if THREAD_SAFE -#if defined(_WIN32) - return _patch_strftime_l(dst, siz, fmt, &tim, c_locale); -#else - return strftime_l(dst, siz, fmt, &tim, c_locale); -#endif // _WIN32 -#else -#if defined(_WIN32) - return _patch_strftime(dst, siz, fmt, &tim); -#else - return strftime(dst, siz, fmt, &tim); -#endif // _WIN32 +/* + * Seconds since the epoch of the parsed time. Fields without an + * explicit zone are local time if utc is 0, or UTC otherwise. + */ +static time_t resolve(int utc, struct tm *tm) { +#if HAVE_STRUCT_TM_TM_GMTOFF && HAVE_STRUCT_TM_TM_ZONE + /* BSD strptime() converts %s, %z and GMT to local time. */ + if (tm->tm_zone != NULL) return mktime(tm); + /* glibc strptime() stores %z in tm_gmtoff only. */ + if (tm->tm_gmtoff != NO_GMTOFF) { + long off = tm->tm_gmtoff; + tm->tm_gmtoff = 0; + return timegm(tm) - off; + } + tm->tm_gmtoff = 0; #endif + return utc ? timegm(tm) : mktime(tm); } -size_t c_format_unix_time_gmt(char *fmt, time_t src, char* dst, int siz) { +time_t c_parse_unix_time(char *fmt, char *src, int *ok) { + struct tm tm; + *ok = parse(0, fmt, src, &tm); + return resolve(0, &tm); +} + +time_t c_parse_unix_time_gmt(char *fmt, char *src, int *ok) { + struct tm tm; + *ok = parse(1, fmt, src, &tm); + return resolve(1, &tm); +} + +/* + * Formatting. + */ + +size_t c_format_unix_time(char *fmt, time_t src, char *dst, int siz) { struct tm tim; - char *local_tz; - size_t dst_size; + if (localtime_r(&src, &tim) == NULL) return 0; + return STRFTIME(dst, siz, fmt, &tim); +} - init_locale(); - gmtime_r(&src, &tim); +/* + * strftime() may compute %s and %z from the local time zone + * (e.g. macOS), so expand them here. So does %Z on Windows. + */ +static char *expand_gmt_format(const char *fmt, time_t src) { + /* Each 2-byte directive expands to at most 21 bytes. */ + char *buf = malloc(strlen(fmt) * 11 + 1); + char *p = buf; + if (buf == NULL) return NULL; + while (*fmt) { + if (fmt[0] != '%' || fmt[1] == '\0') { + *p++ = *fmt++; + continue; + } + switch (fmt[1]) { + case 's': + p += sprintf(p, "%lld", (long long)src); + break; + case 'z': + p += sprintf(p, "+0000"); + break; +#if defined(_WIN32) + case 'Z': + p += sprintf(p, "UTC"); + break; +#endif + default: + *p++ = fmt[0]; + *p++ = fmt[1]; + break; + } + fmt += 2; + } + *p = '\0'; + return buf; +} - local_tz = set_tz_utc(); -#if THREAD_SAFE -#if defined(_WIN32) - dst_size = _patch_strftime_l(dst, siz, fmt, &tim, c_locale); -#else - dst_size = strftime_l(dst, siz, fmt, &tim, c_locale); -#endif // _WIN32 -#else -#if defined(_WIN32) - dst_size = _patch_strftime(dst, siz, fmt, &tim); -#else - dst_size = strftime(dst, siz, fmt, &tim); -#endif // _WIN32 -#endif - set_tz(local_tz); - return dst_size; +size_t c_format_unix_time_gmt(char *fmt, time_t src, char *dst, int siz) { + struct tm tim; + char *gmt_fmt; + size_t len; + if (gmtime_r(&src, &tim) == NULL) return 0; + gmt_fmt = expand_gmt_format(fmt, src); + if (gmt_fmt == NULL) return 0; + len = STRFTIME(dst, siz, gmt_fmt, &tim); + free(gmt_fmt); + return len; } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/cbits/strptime.c new/unix-time-0.6.0/cbits/strptime.c --- old/unix-time-0.4.17/cbits/strptime.c 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/cbits/strptime.c 2001-09-09 03:46:40.000000000 +0200 @@ -706,16 +706,31 @@ return (ret); } -char * -strptime(const char * __restrict buf, const char * __restrict fmt, - struct tm * __restrict tm) +static _locale_t +default_locale(void) { #if HAVE__GET_CURRENT_LOCALE - return strptime_l(buf, fmt, tm, _get_current_locale()); + return _get_current_locale(); #elif HAVE__CREATE_LOCALE && !IS_NT61 - return strptime_l(buf, fmt, tm, _create_locale(LC_TIME, "C")); + return _create_locale(LC_TIME, "C"); #else _locale_t locale; - return strptime_l(buf, fmt, tm, locale); + return locale; #endif } + +char * +strptime(const char * __restrict buf, const char * __restrict fmt, + struct tm * __restrict tm) +{ + return strptime_l(buf, fmt, tm, default_locale()); +} + +/* Like strptime() but leaves %s and %z results in UTC. */ +char * +_patch_strptime_utc(const char * __restrict buf, const char * __restrict fmt, + struct tm * __restrict tm) +{ + int gmt = 0; + return _strptime(buf, fmt, tm, &gmt, default_locale()); +} diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/cbits/win_patch.c new/unix-time-0.6.0/cbits/win_patch.c --- old/unix-time-0.4.17/cbits/win_patch.c 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/cbits/win_patch.c 2001-09-09 03:46:40.000000000 +0200 @@ -99,31 +99,3 @@ "%I:%M:%S %p" }; - -int _patch_setenv(const char *var, const char *val, int _ovr) { - if (val == NULL) { - return _patch_unsetenv(var); - } - int varlen = strlen(var); - int vallen = strlen(val); - int len = varlen + vallen + 2; - char *sname = (char *)malloc(len); - strcpy(sname, var); - sname[varlen] = '='; - strcpy(sname + varlen + 1, val); - sname[varlen + vallen + 1] = '\0'; - int r = _putenv(sname); - free(sname); - return r; -} - -int _patch_unsetenv(const char *name) { - int len = strlen(name) + 2; - char *sname = (char *)malloc(len); - strcpy(sname, name); - sname[len] = '='; - sname[len + 1] = '\0'; - int r = _putenv(sname); - free(sname); - return r; -} diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/cbits/win_patch.h new/unix-time-0.6.0/cbits/win_patch.h --- old/unix-time-0.4.17/cbits/win_patch.h 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/cbits/win_patch.h 2001-09-09 03:46:40.000000000 +0200 @@ -158,10 +158,6 @@ extern const struct lc_time_T _C_time_locale; -int _patch_setenv(const char *var, const char *val, int ovr); - -int _patch_unsetenv(const char *name); - size_t _patch_strftime(char * __restrict s, size_t maxsize, const char * __restrict format, const struct tm * __restrict t); @@ -174,4 +170,7 @@ char *strptime(const char * __restrict buf, const char * __restrict fmt, struct tm * __restrict tm); +char *_patch_strptime_utc(const char * __restrict buf, const char * __restrict fmt, + struct tm * __restrict tm); + #endif // UNIX_TIME_WIN_PATCH_H diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/configure.ac new/unix-time-0.6.0/configure.ac --- old/unix-time-0.4.17/configure.ac 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/configure.ac 2001-09-09 03:46:40.000000000 +0200 @@ -2,11 +2,13 @@ AC_DISABLE_OPTION_CHECKING -AC_CONFIG_HEADER(cbits/config.h) +AC_CONFIG_HEADERS(cbits/config.h) AC_CHECK_HEADERS(xlocale.h) AC_CHECK_FUNCS(strptime_l) +AC_CHECK_FUNCS(strftime_l) +AC_CHECK_MEMBERS([struct tm.tm_gmtoff, struct tm.tm_zone], [], [], [[#include <time.h>]]) AC_CHECK_FUNCS(timegm) AC_CHECK_DECLS([_mkgmtime], [], [], [[#include <time.h>]]) AC_CHECK_FUNCS(_get_current_locale) @@ -20,17 +22,6 @@ host=`uname -a` case $host in - Linux*) - LINUX=1 - ;; - *) - LINUX=0 - ;; -esac - -AC_DEFINE_UNQUOTED(IS_LINUX,$LINUX,"Is Linux") - -case $host in MINGW??_NT-6.1*) NT61=1 ;; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/test/UnixTimeSpec.hs new/unix-time-0.6.0/test/UnixTimeSpec.hs --- old/unix-time-0.4.17/test/UnixTimeSpec.hs 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/test/UnixTimeSpec.hs 2001-09-09 03:46:40.000000000 +0200 @@ -6,8 +6,13 @@ module UnixTimeSpec (main, spec) where +import Control.Concurrent (forkIO) +import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar) +import Control.Monad (forM_, replicateM, unless) +import Data.Binary (decode, encode) import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as BS +import qualified Data.ByteString.Lazy as BL import Data.Function (on) import Data.Time import Data.Time.Clock.POSIX (posixSecondsToUTCTime) @@ -15,6 +20,8 @@ import Foreign.Marshal.Alloc (alloca) import Foreign.Ptr (Ptr) import Foreign.Storable (peek, poke) +import System.Environment (lookupEnv, setEnv, unsetEnv) +import System.Info (os) import qualified Language.Haskell.TH as TH (runIO) import Test.Hspec import Test.Hspec.QuickCheck (prop) @@ -38,8 +45,70 @@ } return ut +-- Normalized values only, as created by the library. +instance Arbitrary UnixDiffTime where + arbitrary = + microSecondsToUnixDiffTime + <$> (choose (-10000000000000, 10000000000000) :: Gen Integer) + spec :: Spec spec = do + describe "parseUnixTime (DST)" $ + it "takes DST into account" $ unless (os == "mingw32") $ do + tz0 <- lookupEnv "TZ" + setEnv "TZ" "EST5EDT" + let fmt = "%Y-%m-%d %H:%M:%S" + utSeconds (parseUnixTime fmt "2024-07-01 12:00:00") `shouldBe` 1719849600 + utSeconds (parseUnixTime fmt "2024-01-01 12:00:00") `shouldBe` 1704128400 + maybe (unsetEnv "TZ") (setEnv "TZ") tz0 + -- mktime() calls tzset() to reload TZ. + utSeconds (parseUnixTime fmt "2000-01-01 00:00:00") `seq` return () + + describe "parse with %z" $ do + it "parseUnixTimeGMT honors the offset" $ + parseUnixTimeGMT mailDateFormat "Thu, 01 Jan 1970 09:00:00 +0900" + `shouldBe` UnixTime 0 0 + it "parseUnixTime honors the offset" $ + parseUnixTime mailDateFormat "Wed, 31 Dec 1969 19:00:00 -0500" + `shouldBe` UnixTime 0 0 + + describe "parseUnixTimeMaybe & parseUnixTimeGMTMaybe" $ do + it "returns Just on success" $ do + parseUnixTimeGMTMaybe webDateFormat "Thu, 01 Jan 1970 00:00:00 GMT" + `shouldBe` Just (UnixTime 0 0) + parseUnixTimeMaybe mailDateFormat "Thu, 01 Jan 1970 09:00:00 +0900" + `shouldBe` Just (UnixTime 0 0) + it "returns Nothing on failure" $ do + parseUnixTimeGMTMaybe webDateFormat "garbage" `shouldBe` Nothing + parseUnixTimeMaybe "%Y-%m-%d" "garbage" `shouldBe` Nothing + it "returns Nothing if the input is not entirely consumed" $ + parseUnixTimeGMTMaybe "%Y-%m-%d" "1970-01-01 junk" `shouldBe` Nothing + it "returns Nothing for NUL bytes" $ + parseUnixTimeGMTMaybe "%Y-%m-%d" "1970-01-01\0junk" `shouldBe` Nothing + + describe "formatUnixTimeGMT (zone)" $ + it "formats %s and %z in UTC" $ + formatUnixTimeGMT "%s %z %H" (UnixTime 1234567890 0) + `shouldBe` "1234567890 +0000 23" + + describe "formatUnixTimeGMT (long output)" $ + it "does not truncate output longer than the initial buffer" $ do + let fmt = BS.concat (replicate 12 "%Y-%m-%d ") + formatUnixTimeGMT fmt (UnixTime 0 0) + `shouldBe` BS.concat (replicate 12 "1970-01-01 ") + + describe "formatUnixTimeGMT (concurrency)" $ + it "does not corrupt TZ when called concurrently" $ do + tz0 <- lookupEnv "TZ" + vs <- replicateM 8 newEmptyMVar + forM_ (zip [0 ..] vs) $ \(i, v) -> forkIO $ do + forM_ [1 .. 2000 :: Int] $ \j -> + BS.length (formatUnixTimeGMT webDateFormat (UnixTime (i * 100000 + fromIntegral j) 0)) + `seq` return () + putMVar v () + mapM_ takeMVar vs + lookupEnv "TZ" `shouldReturn` tz0 + describe "formatUnixTime" $ prop "behaves like the model" $ \ut -> do let ours = formatUnixTime mailDateFormat ut @@ -66,6 +135,31 @@ ut1' = addUnixDiffTime ut0 $ diffUnixTime ut1 ut0 in ut0' == ut0 && ut1' == ut1 + describe "UnixDiffTime Num instance" $ do + let r :: UnixDiffTime -> Rational + r = toRational + normalized (UnixDiffTime sec usec) = + abs usec < 1000000 + && not (sec > 0 && usec < 0) + && not (sec < 0 && usec > 0) + same x v = normalized x && r x == v + prop "+" $ \x y -> same (x + y) (r x + r y) + prop "-" $ \x y -> same (x - y) (r x - r y) + prop "*" $ \x y -> + same (x * y) (fromInteger (truncate (r x * r y * 1000000)) / 1000000) + prop "negate" $ \x -> same (negate x) (negate (r x)) + prop "abs" $ \x -> same (abs x) (abs (r x)) + prop "signum" $ \x -> same (signum x) (signum (r x)) + prop "compare" $ \x y -> compare x y == compare (r x) (r y) + prop "diffUnixTime" $ \ut0 ut1 -> + same (diffUnixTime ut0 ut1) (utR ut0 - utR ut1) + + describe "UnixTime Binary instance" $ do + prop "decode . encode = id" $ \ut -> + decode (encode ut) == (ut :: UnixTime) + it "encodes seconds as Int64" $ + BL.length (encode (UnixTime 0 0)) `shouldBe` 12 + describe "UnixTime Storable instance" $ prop "peek . poke = id" $ \ut -> let pokePeek :: Ptr UnixTime -> IO UnixTime @@ -90,5 +184,8 @@ zt = utcToZonedTime zone ut fmt = BS.unpack mailDateFormat +utR :: UnixTime -> Rational +utR (UnixTime sec usec) = toRational sec + toRational usec / 1000000 + toUTCTime :: UnixTime -> UTCTime toUTCTime = posixSecondsToUTCTime . realToFrac . toEpochTime diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' --exclude Makefile.in --exclude configure --exclude config.guess --exclude '*.pot' --exclude mkinstalldirs --exclude aclocal.m4 --exclude config.sub --exclude depcomp --exclude install-sh --exclude ltmain.sh old/unix-time-0.4.17/unix-time.cabal new/unix-time-0.6.0/unix-time.cabal --- old/unix-time-0.4.17/unix-time.cabal 2001-09-09 03:46:40.000000000 +0200 +++ new/unix-time-0.6.0/unix-time.cabal 2001-09-09 03:46:40.000000000 +0200 @@ -1,6 +1,6 @@ cabal-version: 1.18 name: unix-time -version: 0.4.17 +version: 0.6.0 license: BSD3 license-file: LICENSE maintainer: Kazu Yamamoto <[email protected]> @@ -45,9 +45,8 @@ include-dirs: cbits ghc-options: -Wall build-depends: - base >=4.4 && <5, + base >=4.9 && <5, bytestring, - old-time, binary if impl(ghc >=7.8) @@ -70,12 +69,11 @@ hs-source-dirs: test other-modules: UnixTimeSpec default-language: Haskell2010 - ghc-options: -Wall + ghc-options: -Wall -threaded -with-rtsopts=-N build-depends: base, + binary, bytestring, - old-locale, - old-time, QuickCheck, template-haskell, time,
