Skip to content

Commit

Permalink
0.3.2 - Add {quarter} format parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre von Houck committed Oct 16, 2024
1 parent 39ad691 commit f46160d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion chrono.nimble
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/chrono/calendars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
9 changes: 9 additions & 0 deletions tests/test_calendars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f46160d

Please sign in to comment.