github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/format_test_data_test.go (about) 1 package chrono_test 2 3 import ( 4 "testing" 5 6 "github.com/go-chrono/chrono" 7 ) 8 9 const ( 10 formatYear = 807 11 formatCentury = 8 12 formatMonth = chrono.February 13 formatDay = 9 14 formatHour = 1 15 formatMin = 5 16 formatSec = 2 17 formatMillis = 123 18 formatMicros = 123457 19 formatNanos = 123456789 20 formatOffsetHours = 0 21 formatOffsetMins = 0 22 formatOffset = 0 23 ) 24 25 func setupCenturyParsing() { 26 chrono.SetupCenturyParsing(800) 27 } 28 29 func tearDownCenturyParsing() { 30 chrono.TearDownCenturyParsing() 31 } 32 33 type date interface { 34 Date() (year int, month chrono.Month, day int) 35 YearDay() int 36 ISOWeek() (isoYear, isoWeek int) 37 } 38 39 func checkYear(t *testing.T, date date) { 40 if y, _, _ := date.Date(); y != formatYear { 41 t.Errorf("date.Date() year = %d, want %d", y, formatYear) 42 } 43 } 44 45 func checkCentury(t *testing.T, date date) { 46 if y, _, _ := date.Date(); y/100 != formatCentury { 47 t.Errorf("date.Date() year/100 = %d, want %d", y/100, formatCentury) 48 } 49 } 50 51 func checkYearDay(t *testing.T, date date) { 52 if d := date.YearDay(); d != 40 { 53 t.Errorf("date.YearDay() = %d, want %d", d, 40) 54 } 55 } 56 57 func checkMonth(t *testing.T, date date) { 58 if _, m, _ := date.Date(); m != formatMonth { 59 t.Errorf("date.Date() month = %d, want %d", m, formatMonth) 60 } 61 } 62 63 func checkDay(t *testing.T, date date) { 64 if _, _, d := date.Date(); d != formatDay { 65 t.Errorf("date.Date() day = %d, want %d", d, formatDay) 66 } 67 } 68 69 func checkWeekday(_ *testing.T, _ date) { 70 // A parsed weekday is only checked for correctness - it does not affect the resulting LocalDate. 71 // See note (3). 72 } 73 74 func checkISOYear(t *testing.T, date date) { 75 if y, _ := date.ISOWeek(); y != 807 { 76 t.Errorf("date.ISOWeek() year = %d, want %d", y, 807) 77 } 78 } 79 80 func checkISOWeek(t *testing.T, date date) { 81 if _, w := date.ISOWeek(); w != 6 { 82 t.Errorf("date.ISOWeek() week = %d, want %d", w, 6) 83 } 84 } 85 86 type time interface { 87 Clock() (hour, min, sec int) 88 Nanosecond() int 89 } 90 91 func checkTimeOfDay(_ *testing.T, _ time) { 92 // Time of day is checked implicitly by checking the hour. 93 } 94 95 func checkHour12HourClock(t *testing.T, time time) { 96 if h, _, _ := time.Clock(); h != formatHour { 97 t.Errorf("time.Clock() hour = %d, want %d", h, formatHour) 98 } 99 } 100 101 func checkHour(t *testing.T, time time) { 102 if h, _, _ := time.Clock(); h != formatHour { 103 t.Errorf("time.Clock() hour = %d, want %d", h, formatHour) 104 } 105 } 106 107 func checkMinute(t *testing.T, time time) { 108 if _, m, _ := time.Clock(); m != formatMin { 109 t.Errorf("time.Clock() min = %d, want %d", m, formatMin) 110 } 111 } 112 113 func checkSecond(t *testing.T, time time) { 114 if _, _, s := time.Clock(); s != formatSec { 115 t.Errorf("time.Clock() sec = %d, want %d", s, formatSec) 116 } 117 } 118 119 func checkMillis(t *testing.T, time time) { 120 if nanos := time.Nanosecond(); nanos != formatMillis*1000000 { 121 t.Errorf("time.Nanosecond() = %d, want %d", nanos, formatMillis*1000000) 122 } 123 } 124 125 func checkMicros(t *testing.T, time time) { 126 if nanos := time.Nanosecond(); nanos != formatMicros*1000 { 127 t.Errorf("time.Nanosecond() = %d, want %d", nanos, formatMillis*1000) 128 } 129 } 130 131 func checkNanos(t *testing.T, time time) { 132 if nanos := time.Nanosecond(); nanos != formatNanos { 133 t.Errorf("time.Nanosecond() = %d, want %d", nanos, formatNanos) 134 } 135 } 136 137 func checkOffset(t *testing.T, time chrono.OffsetTime) { 138 if offset := time.Offset(); offset != formatOffset { 139 t.Errorf("time.Offset() = %d, want %d", offset, formatOffset) 140 } 141 } 142 143 var ( 144 dateSpecifiers = []struct { 145 specifier string 146 textToParse string 147 checkParse func(*testing.T, date) 148 expectedFormatted string 149 }{ 150 {"%Y", "0807", checkYear, "0807"}, 151 {"%-Y", "807", checkYear, "807"}, 152 {"%EY", "0807", checkYear, "0807"}, 153 {"%-EY", "807", checkYear, "807"}, 154 {"%y", "07", checkYear, "07"}, 155 {"%-y", "7", checkYear, "7"}, 156 {"%Ey", "07", checkYear, "07"}, 157 {"%-Ey", "7", checkYear, "7"}, 158 {"%C", "08", checkCentury, "08"}, 159 {"%j", "040", checkYearDay, "040"}, 160 {"%-j", "40", checkYearDay, "40"}, 161 {"%m", "02", checkMonth, "02"}, 162 {"%-m", "2", checkMonth, "2"}, 163 {"%B", "February", checkMonth, "February"}, 164 {"%B", "february", checkMonth, "February"}, 165 {"%b", "Feb", checkMonth, "Feb"}, 166 {"%b", "feb", checkMonth, "Feb"}, 167 {"%d", "09", checkDay, "09"}, 168 {"%-d", "9", checkDay, "9"}, 169 {"%u", "5", checkWeekday, "5"}, 170 {"%-u", "5", checkWeekday, "5"}, 171 {"%A", "Friday", checkWeekday, "Friday"}, 172 {"%A", "friday", checkWeekday, "Friday"}, 173 {"%a", "Fri", checkWeekday, "Fri"}, 174 {"%a", "fri", checkWeekday, "Fri"}, 175 {"%G", "0807", checkISOYear, "0807"}, 176 {"%-G", "807", checkISOYear, "807"}, 177 {"%V", "06", checkISOWeek, "06"}, 178 {"%-V", "6", checkISOWeek, "6"}, 179 } 180 181 timeSpecifiers = []struct { 182 specifier string 183 text string 184 checkParse func(*testing.T, time) 185 }{ 186 {"%P", "am", checkTimeOfDay}, 187 {"%p", "AM", checkTimeOfDay}, 188 {"%I", "01", checkHour12HourClock}, 189 {"%-I", "1", checkHour12HourClock}, 190 {"%H", "01", checkHour}, 191 {"%-H", "1", checkHour}, 192 {"%M", "05", checkMinute}, 193 {"%-M", "5", checkMinute}, 194 {"%S", "02", checkSecond}, 195 {"%-S", "2", checkSecond}, 196 {"%3f", "123", checkMillis}, 197 {"%6f", "123457", checkMicros}, 198 {"%9f", "123456789", checkNanos}, 199 {"%f", "123457", checkMicros}, 200 } 201 202 offsetTimeSpecifiersUTC = []struct { 203 specifier string 204 text string 205 checkParse func(*testing.T, chrono.OffsetTime) 206 expectedFormatted string 207 }{ 208 {"%z", "+0000", checkOffset, "Z"}, 209 {"%Ez", "Z", checkOffset, "Z"}, 210 } 211 212 offsetTimeSpecifiers = []struct { 213 specifier string 214 text string 215 expected chrono.Extent 216 expectedFormatted string 217 expectErr bool 218 }{ 219 {"%z", "Z", 0 * chrono.Hour, "Z", false}, 220 {"%z", "z", 0, "Z", true}, 221 {"%z", "+00", 0, "Z", false}, 222 {"%z", "-00", 0, "Z", false}, 223 {"%z", "+0000", 0 * chrono.Hour, "Z", false}, 224 {"%z", "-0000", 0 * chrono.Hour, "Z", false}, 225 {"%z", "+00:00", 0, "Z", true}, 226 {"%z", "-00:00", 0, "Z", true}, 227 {"%z", "+02", 2 * chrono.Hour, "+0200", false}, 228 {"%z", "-02", -2 * chrono.Hour, "-0200", false}, 229 {"%z", "+0230", 2*chrono.Hour + 30*chrono.Minute, "+0230", false}, 230 {"%z", "-0230", -2*chrono.Hour - 30*chrono.Minute, "-0230", false}, 231 {"%z", "+02:", 0, "Z", true}, 232 {"%z", "-02:", 0, "Z", true}, 233 {"%z", "+02:30", 0, "Z", true}, 234 {"%z", "-02:30", 0, "Z", true}, 235 {"%Ez", "Z", 0 * chrono.Hour, "Z", false}, 236 {"%Ez", "z", 0, "Z", true}, 237 {"%Ez", "+00", 0, "Z", false}, 238 {"%Ez", "-00", 0, "Z", false}, 239 {"%Ez", "+0000", 0, "Z", true}, 240 {"%Ez", "-0000", 0, "Z", true}, 241 {"%Ez", "+00:00", 0 * chrono.Hour, "Z", false}, 242 {"%Ez", "-00:00", 0 * chrono.Hour, "Z", false}, 243 {"%Ez", "+02", 2 * chrono.Hour, "+02:00", false}, 244 {"%Ez", "-02", -2 * chrono.Hour, "-02:00", false}, 245 {"%Ez", "+0230", 0, "Z", true}, 246 {"%Ez", "-0230", 0, "Z", true}, 247 {"%Ez", "+02:", 0, "Z", true}, 248 {"%Ez", "-02:", 0, "Z", true}, 249 {"%Ez", "+02:30", 2*chrono.Hour + 30*chrono.Minute, "+02:30", false}, 250 {"%Ez", "-02:30", -2*chrono.Hour - 30*chrono.Minute, "-02:30", false}, 251 } 252 253 predefinedLayouts = []struct { 254 layout string 255 textToParse string 256 expectedFormattedLocal string 257 datetime chrono.OffsetDateTime 258 }{ 259 {chrono.ISO8601DateSimple, "08070209", "08070209", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 260 {chrono.ISO8601DateExtended, "0807-02-09", "0807-02-09", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 261 {chrono.ISO8601DateTruncated, "0807-02", "0807-02", chrono.OffsetDateTimeOf(formatYear, formatMonth, 1, 0, 0, 0, 0, 0, 0)}, 262 {chrono.ISO8601TimeSimple, "T010502", "T010502", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, formatSec, 0, 0, 0)}, 263 {chrono.ISO8601TimeExtended, "T01:05:02", "T01:05:02", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, formatSec, 0, 0, 0)}, 264 {chrono.ISO8601TimeMillisSimple, "T010502.123", "T010502.123", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, formatSec, 123000000, 0, 0)}, 265 {chrono.ISO8601TimeMillisExtended, "T01:05:02.123", "T01:05:02.123", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, formatSec, 123000000, 0, 0)}, 266 {chrono.ISO8601TimeTruncatedMinsSimple, "T0105", "T0105", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, 0, 0, 0, 0)}, 267 {chrono.ISO8601TimeTruncatedMinsExtended, "T01:05", "T01:05", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, 0, 0, 0, 0)}, 268 {chrono.ISO8601TimeTruncatedHours, "T01", "T01", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, 0, 0, 0, 0, 0)}, 269 {chrono.ISO8601DateTimeSimple, "08070209T010502Z", "08070209T010502", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, formatHour, formatMin, formatSec, 0, formatOffsetHours, formatOffsetMins)}, 270 {chrono.ISO8601DateTimeExtended, "0807-02-09T01:05:02Z", "0807-02-09T01:05:02", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, formatHour, formatMin, formatSec, 0, formatOffsetHours, formatOffsetMins)}, 271 {chrono.ISO8601WeekSimple, "0807W06", "0807W06", chrono.OffsetDateTimeOf(formatYear, formatMonth, 5, 0, 0, 0, 0, 0, 0)}, 272 {chrono.ISO8601WeekExtended, "0807-W06", "0807-W06", chrono.OffsetDateTimeOf(formatYear, formatMonth, 5, 0, 0, 0, 0, 0, 0)}, 273 {chrono.ISO8601WeekDaySimple, "0807W065", "0807W065", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 274 {chrono.ISO8601WeekDayExtended, "0807-W06-5", "0807-W06-5", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 275 {chrono.ISO8601OrdinalDateSimple, "0807040", "0807040", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 276 {chrono.ISO8601OrdinalDateExtended, "0807-040", "0807-040", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, 0, 0, 0, 0, 0, 0)}, 277 {chrono.ANSIC, "Fri Feb 09 01:05:02 0807", "Fri Feb 09 01:05:02 0807", chrono.OffsetDateTimeOf(formatYear, formatMonth, formatDay, formatHour, formatMin, formatSec, 0, 0, 0)}, 278 {chrono.Kitchen, "01:05AM", "01:05AM", chrono.OffsetDateTimeOf(1970, chrono.January, 1, formatHour, formatMin, 0, 0, 0, 0)}, 279 } 280 )