diff --git a/chrono.nimble b/chrono.nimble index 840638d..bf8b9c0 100644 --- a/chrono.nimble +++ b/chrono.nimble @@ -1,5 +1,5 @@ # Package -version = "0.3.1" +version = "0.3.2" author = "Andre von Houck" description = "Calendars, Timestamps and Timezones utilities." license = "MIT" diff --git a/src/chrono/calendars.nim b/src/chrono/calendars.nim index 561cd52..4701cc8 100644 --- a/src/chrono/calendars.nim +++ b/src/chrono/calendars.nim @@ -8,6 +8,7 @@ ## {year} Year in as many digits as needed. Can be negative. ``12012/9/3 -> 12012`` ## {year/2} Two digit year, 0-30 represents 2000-2030 while 30-99 is 1930 to 1999. ``2012/9/3 -> 12`` ## {year/4} Four digits of the year. Years 0 - 9999. ``2012/9/3 -> 2012`` +## {quarter} Always 1 digit. 1-4 ``2012/9/4 -> 3`` ## {month} Month in digits 1-12 ``2012/9/3 -> 9`` ## {month/2} Month in two digits 01-12 ``2012/9/3 -> 09`` ## {month/n} Full name of month ``September -> September`` @@ -613,6 +614,9 @@ proc parseCalendar*(format: string, value: string): Calendar = of "year/4": result.year = getNumber(4) + of "quarter": + result.month = (getNumber(1) - 1) * 3 + 1 + of "month": result.month = getNumber() of "month/2": @@ -747,6 +751,9 @@ proc format*(cal: Calendar, format: string): string = raise newException(ValueError, "Can't format year as 4 digits") putNumber(cal.year, 4) + of "quarter": + putNumber((cal.month-1) div 3 + 1) + of "month": putNumber(cal.month) of "month/2": diff --git a/tests/test_calendars.nim b/tests/test_calendars.nim index 711a6b4..1f0965b 100644 --- a/tests/test_calendars.nim +++ b/tests/test_calendars.nim @@ -60,6 +60,11 @@ suite "calendars": testParse("1988-02-09T03:34:12Z", "{year/4}-{month/2}-{day/2}T{hour/2}:{minute/2}:{second/2}Z", "1988-02-09T03:34:12Z") testParse("1988-02-09T03:34:12Z", "{year/4}{month/2}{day/2}{hour/2}{minute/2}{second/2}", "19880209033412") + testParse("2023-01-01T00:00:00Z", "{year/4}-Q{quarter}", "2023-Q1") + testParse("2023-04-01T00:00:00Z", "{year/4}-Q{quarter}", "2023-Q2") + testParse("2023-07-01T00:00:00Z", "{year/4}-Q{quarter}", "2023-Q3") + testParse("2023-10-01T00:00:00Z", "{year/4}-Q{quarter}", "2023-Q4") + testParse("1970-01-01T09:08:00Z", "{hour/2/ap}:{minute/2} {am/pm}", "09:08 am") testParse("1970-01-01T21:08:00Z", "{hour/2/ap}:{minute/2} {am/pm}", "09:08 pm") testParse("1970-01-01T00:08:00Z", "{hour/2/ap}:{minute/2} {am/pm}", "12:08 am") @@ -116,6 +121,10 @@ suite "calendars": testFormat("1970-01-01T12:08:00Z", "{month/n}", "January") testFormat("1970-02-09T12:08:00Z", "{month/n/3}", "Feb") + testFormat("1970-02-09T12:08:00Z", "{year/4}-Q{quarter}", "1970-Q1") + testFormat("1970-04-09T12:08:00Z", "{year/4}-Q{quarter}", "1970-Q2") + testFormat("1970-07-09T12:08:00Z", "{year/4}-Q{quarter}", "1970-Q3") + testFormat("1970-10-09T12:08:00Z", "{year/4}-Q{quarter}", "1970-Q4") doAssert Calendar().format("{secondFraction}") == "0" doAssert Calendar(secondFraction: 0.25).format("{secondFraction}") == "25"