Skip to content

Commit

Permalink
add time formatting options to cron describe
Browse files Browse the repository at this point in the history
  • Loading branch information
illegalprime committed Jan 31, 2024
1 parent d919330 commit 6d4a16d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/System/Cron/Describe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module System.Cron.Describe
defaultOpts
, twentyFourHourFormat
, twelveHourFormat
, customFormat
, verbose
, notVerbose
, OptionBuilder
Expand Down
8 changes: 8 additions & 0 deletions src/System/Cron/Internal/Describe/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Default.Class
import Data.Semigroup as Semigroup
-------------------------------------------------------------------------------
import System.Cron.Internal.Describe.Types
import Data.Time (TimeZone, TimeLocale)
-------------------------------------------------------------------------------


Expand Down Expand Up @@ -46,6 +47,13 @@ twelveHourFormat :: OptionBuilder
twelveHourFormat = Builder (\o -> o {timeFormat = Hour12} )


-- | Return a builder that sets the options to use a custom time format.
-- This takes in a time zone, a time locale, and a time formatting string defined by:
-- https://hackage.haskell.org/package/time-1.12.2/docs/Data-Time-Format.html#t:FormatTime
customFormat :: TimeZone -> TimeLocale -> String -> OptionBuilder
customFormat z l f = Builder (\o -> o {timeFormat = CustomTimeFormat z l f} )


-- | Return a builder that sets the options to be verbose. A verbose description
-- doesn't eliminate unnecessary information. The only caveat being that month
-- information is only ever displayed if it isn't "*".
Expand Down
21 changes: 9 additions & 12 deletions src/System/Cron/Internal/Describe/Time.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
module System.Cron.Internal.Describe.Time where

import System.Cron.Internal.Describe.Types
import Data.Time (formatTime, TimeOfDay (TimeOfDay), defaultTimeLocale, TimeLocale, TimeZone, utc, utcToLocalTimeOfDay)

newtype Minute = Minute Int
newtype Hour = Hour Int

format :: TimeFormat -> Minute -> Hour -> String
format t (Minute m) (Hour h) = leftPad (hour t) ++ ":" ++ leftPad m ++ suffix t
where leftPad n
| n < 10 = "0" ++ show n
| otherwise = show n
suffix Hour24 = ""
suffix Hour12
| h < 12 = " AM"
| otherwise = " PM"
hour Hour24 = h
hour Hour12
| h > 12 = h `mod` 12
| otherwise = h
format Hour24 = fmtTime utc defaultTimeLocale "%R"
format Hour12 = fmtTime utc defaultTimeLocale "%I:%M %p"
format (CustomTimeFormat zone locale pattern) = fmtTime zone locale pattern

fmtTime :: TimeZone -> TimeLocale -> String -> Minute -> Hour -> String
fmtTime zone locale pattern (Minute m) (Hour h) = formatTime locale pattern tod
where
tod = snd . utcToLocalTimeOfDay zone $ TimeOfDay h m 0
3 changes: 2 additions & 1 deletion src/System/Cron/Internal/Describe/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module System.Cron.Internal.Describe.Types where

import Data.List (intercalate)
import Data.Maybe (catMaybes)
import Data.Time (TimeLocale, TimeZone)


data Descriptor = Descriptor {
Expand Down Expand Up @@ -44,7 +45,7 @@ safeIntToWeekDay n
data Verbosity = Verbose | NotVerbose


data TimeFormat = Hour24 | Hour12
data TimeFormat = Hour24 | Hour12 | CustomTimeFormat TimeZone TimeLocale String


data DescribedValue = Concrete String
Expand Down

0 comments on commit 6d4a16d

Please sign in to comment.