Skip to content

Commit 4fa6bcc

Browse files
lib/advisories: add time-aware "get next HSEC ID"
Add a time-aware function to get the "successor" HSEC ID based on the current time and a given `HsecId` (typically the largest already-assigned ID). ```haskell getNextHsecId :: HsecId -> IO HsecId ```
1 parent 3a5b16c commit 4fa6bcc

File tree

1 file changed

+16
-0
lines changed
  • code/hsec-tools/src/Security/Advisories

1 file changed

+16
-0
lines changed

code/hsec-tools/src/Security/Advisories/HsecId.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ module Security.Advisories.HsecId
99
, parseHsecId
1010
, printHsecId
1111
, nextHsecId
12+
, getNextHsecId
1213
) where
1314

1415
import Control.Monad (guard, join)
1516

17+
import Data.Time (getCurrentTime, utctDay)
18+
import Data.Time.Calendar.OrdinalDate (toOrdinalDate)
19+
1620
import Safe (readMay)
1721

1822
data HsecId = HsecId Integer Integer
@@ -75,3 +79,15 @@ nextHsecId
7579
nextHsecId curYear (HsecId idYear n)
7680
| curYear > idYear = HsecId curYear 1
7781
| otherwise = HsecId idYear (n + 1)
82+
83+
-- | Get the current time, and return an HSEC ID greater than the
84+
-- given HSEC ID. The year of the returned HSEC ID is the current
85+
-- year.
86+
--
87+
getNextHsecId
88+
:: HsecId
89+
-> IO HsecId
90+
getNextHsecId oldId = do
91+
t <- getCurrentTime
92+
let (year, _dayOfYear) = toOrdinalDate (utctDay t)
93+
pure $ nextHsecId year oldId

0 commit comments

Comments
 (0)