Skip to content

Commit 0422557

Browse files
authored
Support AD/BC (#74)
* Formatting. Signed-off-by: joe-mann <[email protected]> * Support AD/BC. Signed-off-by: joe-mann <[email protected]> --------- Signed-off-by: joe-mann <[email protected]>
1 parent 7618ced commit 0422557

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

format.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
// These are predefined layouts used for the parsing and formatting of dates, times and date-times.
1010
// Additional layouts can be composed using the specifiers detailed below:
1111
//
12-
// - %Y: The ISO 8601 year as a decimal number, padded to 4 digits with leading 0s.
12+
// - %Y: The ISO 8601 year as a decimal number, padded to 4 digits with leading 0s.
1313
// - %EY: The year in the era as a decimal number, padded to 4 digits with leading 0s.
14-
// - %y: The ISO 8601 year without a century as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 99. See note (1).
14+
// - %y: The ISO 8601 year without a century as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 99. See note (1).
1515
// - %Ey: The year in the era without a century as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 99. See notes (1) and (9).
16-
// - %C: The century as a decimal number, padded to 2 digits with a leading 0, e.g. 19 for 1980. See note (9).
16+
// - %C: The century as a decimal number, padded to 2 digits with a leading 0, e.g. 19 for 1980. See note (9).
1717
// - %EC: The name of the era, either "CE" (for Common Era) "BCE" (for Before the Common Era).
18-
// - %j: The day of the year as a decimal number, padded to 3 digits with leading 0s, in the range 001 to 366. See note (2).
19-
// - %m: The month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 12.
20-
// - %B: The full month name, e.g. January, February, etc.
21-
// - %b: The abbreviated month name, e.g. Jan, Feb, etc.
22-
// - %d: The day of the month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 31.
18+
// - %j: The day of the year as a decimal number, padded to 3 digits with leading 0s, in the range 001 to 366. See note (2).
19+
// - %m: The month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 12.
20+
// - %B: The full month name, e.g. January, February, etc.
21+
// - %b: The abbreviated month name, e.g. Jan, Feb, etc.
22+
// - %d: The day of the month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 31.
2323
//
2424
// Days of week:
2525
//
@@ -46,14 +46,14 @@ import (
4646
//
4747
// Millisecond precisions:
4848
//
49-
// - %f: Equivalent to %6f.
49+
// - %f: Equivalent to %6f.
5050
// - %3f: The millisecond offset within the represented second, rounded either up or down and padded to 3 digits with leading 0s.
5151
// - %6f: The microsecond offset within the represented second, rounded either up or down and padded to 6 digits with leading 0s.
5252
// - %9f: The nanosecond offset within the represented second, padded to 9 digits with leading 0s.
5353
//
5454
// Time offsets:
5555
//
56-
// - %z: The UTC offset in the format ±HHMM, preceded always by the sign ('+' or '-'), and padded to 4 digits with leading zeros. See notes (6), (7), and (8).
56+
// - %z: The UTC offset in the format ±HHMM, preceded always by the sign ('+' or '-'), and padded to 4 digits with leading zeros. See notes (6), (7), and (8).
5757
// - %Ez: Equivalent to %z, except that an offset of +0000 is formatted at 'Z', and other offsets as ±HH:MM. See notes (6) and (7).
5858
//
5959
// When formatting using specifiers that represent padded decimals, leading 0s can be omitted using the '-' character after the '%'.
@@ -99,6 +99,8 @@ import (
9999
// 8. When %z is used for parsing a UTC offset, 'Z' can be used to represent an offset of +0000.
100100
// 9. When parsing partial years (%Ey and %C) in combination with a full year (%Y or %EY),
101101
// an error will be returned if the represented years to not match.
102+
// 10. When parsing era names (%EC), 'AD' and 'BC' are accepted in place of 'CE' and 'BCE',
103+
// although only the latter are used to format.
102104
const (
103105
// ISO 8601.
104106
ISO8601 = ISO8601DateTimeExtended
@@ -529,8 +531,9 @@ func parseDateAndTime(layout, value string, date, time, offset *int64) error {
529531
parts.haveGregorianYear = true
530532
lower, original := alphas(3)
531533
switch lower {
532-
case "ce":
533-
case "bce":
534+
case "ce", "ad":
535+
parts.isBCE = false
536+
case "bce", "bc":
534537
parts.isBCE = true
535538
default:
536539
return fmt.Errorf("unrecognized era %q", original)

format_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,28 @@ func TestLocalDate_Parse_eras(t *testing.T) {
678678
}
679679
})
680680

681+
t.Run("AD", func(t *testing.T) {
682+
var date chrono.LocalDate
683+
if err := date.Parse("%EY %EC", "2022 AD"); err != nil {
684+
t.Errorf("failed to parse date: %v", err)
685+
}
686+
687+
if year, _, _ := date.Date(); year != 2022 {
688+
t.Errorf("got %d, want 2022", year)
689+
}
690+
})
691+
692+
t.Run("BC", func(t *testing.T) {
693+
var date chrono.LocalDate
694+
if err := date.Parse("%EY %EC", "2022 BC"); err != nil {
695+
t.Errorf("failed to parse date: %v", err)
696+
}
697+
698+
if year, _, _ := date.Date(); year != -2021 {
699+
t.Errorf("got %d, want -2021", year)
700+
}
701+
})
702+
681703
t.Run("zero", func(t *testing.T) {
682704
var date chrono.LocalDate
683705
if err := date.Parse("%EY %EC", "1 BCE"); err != nil {

0 commit comments

Comments
 (0)