{- arch-tag: Time utilities main file
Copyright (C) 2004-2005 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- |
Module : System.Time.Utils
Copyright : Copyright (C) 2004-2005 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen <jgoerzen@complete.org>
Stability : provisional
Portability: portable
This module provides various Haskell utilities for dealing with times and
dates.
Written by John Goerzen, jgoerzen\@complete.org
-}moduleSystem.Time.Utils(timelocal,timegm,timeDiffToSecs,epoch,epochToClockTime,clockTimeToEpoch,renderSecs,renderTD)whereimportSystem.TimeimportData.Ratio{- | January 1, 1970, midnight, UTC, represented as a CalendarTime. -}epoch::CalendarTimeepoch=CalendarTime{ctYear=1970,ctMonth=January,ctDay=1,ctHour=0,ctMin=0,ctSec=0,ctPicosec=0,ctWDay=Thursday,ctYDay=0,ctTZName="UTC",ctTZ=0,ctIsDST=False}{- | Converts the specified CalendarTime (see System.Time) to seconds-since-epoch time.
This conversion does respect the timezone specified on the input object.
If you want a conversion from UTC, specify ctTZ = 0 and ctIsDST = False.
When called like that, the behavior is equivolent to the GNU C function
timegm(). Unlike the C library, Haskell's CalendarTime supports
timezone information, so if such information is specified, it will impact
the result.
-}timegm::CalendarTime->Integertimegmct=timeDiffToSecs(diffClockTimes(toClockTimect)(toClockTimeepoch)){- | Converts the specified CalendarTime (see System.Time) to
seconds-since-epoch format.
The input CalendarTime is assumed to be the time as given in your local
timezone. All timezone and DST fields in the object are ignored.
This behavior is equivolent to the timelocal() and mktime() functions that
C programmers are accustomed to.
Please note that the behavior for this function during the hour immediately
before or after a DST switchover may produce a result with a different hour
than you expect.
-}timelocal::CalendarTime->IOIntegertimelocalct=doguessct<-toCalendarTimeguessclletnewct=ct{ctTZ=ctTZguessct}return$timegmnewctwhereguesscl=toClockTimect{- | Converts the given timeDiff to the number of seconds it represents.
Uses the same algorithm as normalizeTimeDiff in GHC. -}timeDiffToSecs::TimeDiff->IntegertimeDiffToSecstd=(fromIntegral$tdSectd)+60*((fromIntegral$tdMintd)+60*((fromIntegral$tdHourtd)+24*((fromIntegral$tdDaytd)+30*((fromIntegral$tdMonthtd)+365*(fromIntegral$tdYeartd))))){- | Converts an Epoch time represented with an arbitrary Real to a ClockTime.
This input could be a CTime from Foreign.C.Types or an EpochTime from
System.Posix.Types. -}epochToClockTime::Reala=>a->ClockTimeepochToClockTimex=TODsecondssecfracwhereratval=toRationalxseconds=floorratvalsecfrac=floor$(ratval-(seconds%1))*picosecondfactorpicosecondfactor=10^12{- | Converts a ClockTime to something represented with an arbitrary Real.
The result could be treated as a CTime from Foreign.C.Types or EpochTime from
System.Posix.Types. The inverse of 'epochToClockTime'.
Fractions of a second are not preserved by this function. -}clockTimeToEpoch::Numa=>ClockTime->aclockTimeToEpoch(TODsec_)=fromIntegersec{- | Render a number of seconds as a human-readable amount. Shows the two
most significant places. For instance:
>renderSecs 121 = "2m1s"
See also 'renderTD' for a function that works on a TimeDiff.
-}renderSecs::Integer->StringrenderSecsi=renderTD$diffClockTimes(TODi0)(TOD00){- | Like 'renderSecs', but takes a TimeDiff instead of an integer second
count. -}renderTD::TimeDiff->StringrenderTDitd=caseworkinglistof[]->"0s"_->concat.map(\(q,s)->showq++[s])$workinglistwheretd=normalizeTimeDiffitdsuffixlist="yMdhms"quantlist=(\(TimeDiffymodhms_)->[y,mo,d,h,m,s])tdzippedlist=zipquantlistsuffixlist-- Drop all leading elements that are 0, then take at most 2workinglist=take2.dropWhile(\(q,_)->q==0)$zippedlist