From a13b7d2a82eb604b49c3d4d61e76b877dacf2f51 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 22 Apr 2025 16:59:57 -1000 Subject: [PATCH] lib: Hledger.Data.Dates.parsedate: also accept YYYYMMDD format; document - Added support for unseparated dates, for convenience in ghci and for parseHledgerVersion - Noted two user-facing uses: --value's argument, and import's .latest files. YYYYMMDD dates will now also work there. Left this undocumented. - Noted this is now more permissive, parsing many integers successfully; not expecting problems. --- hledger-lib/Hledger/Data/Dates.hs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Data/Dates.hs b/hledger-lib/Hledger/Data/Dates.hs index 42c078459..03960054a 100644 --- a/hledger-lib/Hledger/Data/Dates.hs +++ b/hledger-lib/Hledger/Data/Dates.hs @@ -784,20 +784,41 @@ advanceToNthWeekday n wd s = -- parseTimeM TruedefaultTimeLocale "%Y-%m-%d %H:%M:%S" s -- ] --- | Try to parse a couple of date string formats: --- `YYYY-MM-DD`, `YYYY/MM/DD` or `YYYY.MM.DD`, with leading zeros required. --- For internal use, not quite the same as the journal's "simple dates". +-- | A simple date parsing helper: parses these YMD date string formats: +-- `YYYY-MM-DD`, `YYYY/MM/DD`, `YYYY.MM.DD` or `YYYYMMDD`, +-- where the month and day each have two digits and the year has one or more. +-- +-- This is different from the Smart Dates of the CLI and period expressions ("smartdate", below) +-- and not quite the same as the Simple Dates of the journal ("datep", in Hledger.Read.Common). +-- It's mainly for internal or interactive use, eg when debugging - +-- but currently is also used in a few user-facing places, such as: +-- parsing --value's argument, +-- parsing .latest files, +-- and parsing hledger's --version output (which uses unseparated dates). +-- +-- Unseparated dates were added in 2025 for convenience. +-- Note it means many integers will now parse successfully. +-- -- >>> parsedate "2008/02/03" -- Just 2008-02-03 -- >>> parsedate "2008/02/03/" -- Nothing -- >>> parsedate "2008/02/30" -- Nothing +-- >>> parsedate "2025-01-01" +-- Just 2025-01-01 +-- >>> parsedate "2025.01.01" +-- Just 2025-01-01 +-- >>> parsedate "20250101" +-- Just 2025-01-01 +-- >>> parsedate "00101" +-- Just 0000-01-01 parsedate :: String -> Maybe Day parsedate s = asum [ parseTimeM True defaultTimeLocale "%Y-%m-%d" s, parseTimeM True defaultTimeLocale "%Y/%m/%d" s, - parseTimeM True defaultTimeLocale "%Y.%m.%d" s + parseTimeM True defaultTimeLocale "%Y.%m.%d" s, + parseTimeM True defaultTimeLocale "%Y%m%d" s ] {-|