Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

| Benchmark Name | Description | Data.Time Library | Fast-time |
| ----- | ----- | ----- | ----- |
| UTCTime | Converts the text time to UTCTime format. | 8.382 μs (8.359 μs .. 8.425 μs) | 734.4 ns (731.7 ns .. 736.4 ns) |
| UTCTime Milliseconds | Converts the text time which includes millisecond to UTCTime format. | 11.28 μs (11.24 μs .. 11.33 μs) | 806.6 ns (804.4 ns .. 808.8 ns) |
| LocalTime | Converts the text time to Local time covering case of millisecond parsing as well. | 11.67 μs (11.53 μs .. 11.81 μs | 1.200 μs (1.180 μs .. 1.220 μs) |
| Date Parsing | Converts the text date in the given format. | 4.710 μs (4.296 μs .. 5.211 μs) | 440.3 ns (437.4 ns .. 443.6 ns) |
| Date Parsing ("%d%m%Y") | Converts the text date to the given format using isolate method of flatparse library. | 12.08 μs (11.81 μs .. 12.29 μs) | 368.1 ns (359.7 ns .. 378.0 ns) |
| UTCTime | Converts the text time to UTCTime format. | 8.382 μs (8.359 μs .. 8.425 μs) | 648.4 ns (647.7 ns .. 649.4 ns) |
| UTCTime Milliseconds | Converts the text time which includes millisecond to UTCTime format. | 12.89 μs (12.88 μs .. 12.93 μs) | 669.4 ns (668.8 ns .. 670.5 ns) |
| LocalTime | Converts the text time to Local time covering case of millisecond parsing as well. | 12.72 μs (12.71 μs .. 12.74 μs) | 1.027 μs (1.026 μs .. 1.027 μs) |
| Date Parsing | Converts the text date in the given format. | 8.511 μs (8.501 μs .. 8.535 μs) | 409.1 ns (408.8 ns .. 410.5 ns) |
| Date Parsing ("%d%m%Y") | Converts the text date to the given format using isolate method of flatparse library. | 14.78 μs (14.77 μs .. 14.80 μs) | 371.9 ns (371.8 ns .. 372.1 ns) |

12 changes: 6 additions & 6 deletions src/Time.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Time
, parseTime
, parseLTime
) where

import Data.Text (Text)
import Data.Time (UTCTime, LocalTime)
import FlatParse.Basic (Result(..), runParserS)
import qualified FlatParse.Basic as FB
import Time.Parser as X

parseTime :: Text -> String -> Maybe UTCTime
parseTime format stringifiedDate = go $ runParserS parser stringifiedDate
parseTime format stringifiedDate = go $ FB.runParser parser $ FB.strToUtf8 stringifiedDate
where
parser
| format == "%Y-%m-%dT%k:%M:%SZ" ||
Expand All @@ -24,15 +24,15 @@ parseTime format stringifiedDate = go $ runParserS parser stringifiedDate
dayParser
| format == "%d%m%Y" = dayParser'
| otherwise = parserUTCTime
go (OK r _) = Just r
go (FB.OK r _) = Just r
go _ = Nothing

parseLTime :: Text -> String -> Maybe LocalTime
parseLTime format stringifiedDate = go $ runParserS parser stringifiedDate
parseLTime format stringifiedDate = go $ FB.runParser parser $ FB.strToUtf8 stringifiedDate
where
parser
| format == "%Y-%m-%dT%k:%M:%S%Q%Ez" =
parserLocalTime
| otherwise = parserLocalTime
go (OK r _) = Just r
go (FB.OK r _) = Just r
go _ = Nothing
52 changes: 26 additions & 26 deletions src/Time/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module Time.Parser
) where

import Data.Time
import FlatParse.Basic
import qualified FlatParse.Basic as FB
import Data.Fixed (Pico)

-- | Custom parser for the format
-- %Y-%m-%dT%k:%M:%SZ
-- %Y-%m-%dT%k:%M:%S%QZ
-- %Y-%m-%dT%k:%M:%S%Q%Ez

parserLocalTime :: Parser e LocalTime
parserLocalTime :: FB.Parser e LocalTime
parserLocalTime = do
(year, month, day, hr, minutes, sec, msec, curLen) <- basicParserUtil
let curLen' = if curLen < 0 then 0
Expand All @@ -26,15 +26,15 @@ parserLocalTime = do
(fromGregorian year month day)
(TimeOfDay (fromIntegral hr :: Int) (fromIntegral minutes :: Int) sec')

parserUTCTime :: Parser e UTCTime
parserUTCTime :: FB.Parser e UTCTime
parserUTCTime = do
(year, month, day, hr, minutes, sec, msec, curLen) <- basicParserUtil
pure $
UTCTime
(fromGregorian year month day)
(picosecondsToDiffTime (((hr * 60 * 60) + (minutes * 60) + sec) * (10 ^ 12) + (msec * (10 ^ (12 - curLen)))))

dayParser :: Parser e UTCTime
dayParser :: FB.Parser e UTCTime
dayParser = do
(year, month, day) <- dateParser
pure $
Expand All @@ -43,39 +43,39 @@ dayParser = do
(secondsToDiffTime 0)

-- Added parser for handling date for this format --> "%d%m%Y"
dayParser' :: Parser e UTCTime
dayParser' :: FB.Parser e UTCTime
dayParser' = do
day <- isolate 2 readInt
month <- isolate 2 readInt
year <- isolate 4 readInteger
day <- FB.isolate 2 FB.anyAsciiDecimalInt
month <- FB.isolate 2 FB.anyAsciiDecimalInt
year <- FB.isolate 4 FB.anyAsciiDecimalInteger
pure $
UTCTime
(fromGregorian year month day)
(secondsToDiffTime 0)

-- | utils
dateParser :: Parser e (Integer, Int, Int)
dateParser :: FB.Parser e (Integer, Int, Int)
dateParser = do
year <- readInteger
satisfy_ (\x -> x == '-' || x == ' ' || x == '/')
month <- readInt
satisfy_ (\x -> x == '-' || x == ' ' || x == '/')
day <- readInt
year <- FB.anyAsciiDecimalInteger
_ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == '/')
month <- FB.anyAsciiDecimalInt
_ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == '/')
day <- FB.anyAsciiDecimalInt
pure (year, month, day)

-- common function for parsing time
basicParserUtil :: Parser e (Integer, Int, Int, Integer, Integer, Integer, Integer, Int)
basicParserUtil :: FB.Parser e (Integer, Int, Int, Integer, Integer, Integer, Integer, Int)
basicParserUtil = do
(year, month, day) <- dateParser
satisfy_ (\x -> x == '-' || x == ' ' || x == 'T')
many_ $(char ' ')
hr <- readInteger
$(char ':')
minutes <- readInteger
$(char ':')
sec <- readInteger
Pos curS <- getPos
msec <- ($(char '.') *> readInteger) <|> pure 0
Pos curE <- getPos
try $ $(char 'Z') <|> pure ()
_ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == 'T')
_ <- FB.many $(FB.char ' ')
hr <- FB.anyAsciiDecimalInteger
$(FB.char ':')
minutes <- FB.anyAsciiDecimalInteger
$(FB.char ':')
sec <- FB.anyAsciiDecimalInteger
FB.Pos curS <- FB.getPos
msec <- ($(FB.char '.') *> FB.anyAsciiDecimalInteger) FB.<|> pure 0
FB.Pos curE <- FB.getPos
FB.try $ $(FB.char 'Z') FB.<|> pure ()
pure (year, month, day, hr, minutes, sec, msec, curS - curE -1)
4 changes: 3 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ packages:
# forks / in-progress versions pinned to a git hash. For example:
#
extra-deps:
- flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936
# - flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936
- flatparse-0.4.1.0
- utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
Expand Down
17 changes: 12 additions & 5 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@

packages:
- completed:
hackage: flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936
hackage: flatparse-0.4.1.0@sha256:b3cc2e58a63d9010ceb34596c97ff1e5efd40d9d429d16a1673be92212903b15,4537
pantry-tree:
size: 1062
sha256: e0d48ff528737de7351bd3d491ad6913bbd4aca0b9957386512820a0f09eedd5
sha256: 281402986d640a07f104f017bf3b87ffa38548a69e2e481c8fcac638fe107d06
size: 2341
original:
hackage: flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936
hackage: flatparse-0.4.1.0
- completed:
hackage: utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538
pantry-tree:
sha256: 0863c3c9b6ee24dd38873c62e38ac46dd50aff2ea6b362beadf7f9b4a273c75f
size: 601
original:
hackage: utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538
snapshots:
- completed:
sha256: d6b004b095fe2a0b8b14fbc30014ee97e58843b9c9362ddb9244273dda62649e
size: 532380
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/16.yaml
sha256: d6b004b095fe2a0b8b14fbc30014ee97e58843b9c9362ddb9244273dda62649e
original: lts-16.16