@@ -2,6 +2,7 @@ package iso8601
22
33import (
44 "testing"
5+ "time"
56)
67
78type TestCase struct {
@@ -57,6 +58,41 @@ func (tc TestCase) CheckError(err error, t *testing.T) bool {
5758 return false
5859}
5960
61+ func (tc TestCase ) Check (d time.Time , t * testing.T ) {
62+ if y := d .Year (); y != tc .Year {
63+ t .Errorf ("Year = %d; want %d" , y , tc .Year )
64+ }
65+ if m := int (d .Month ()); m != tc .Month {
66+ t .Errorf ("Month = %d; want %d" , m , tc .Month )
67+ }
68+ if d := d .Day (); d != tc .Day {
69+ t .Errorf ("Day = %d; want %d" , d , tc .Day )
70+ }
71+ if h := d .Hour (); h != tc .Hour {
72+ t .Errorf ("Hour = %d; want %d" , h , tc .Hour )
73+ }
74+ if m := d .Minute (); m != tc .Minute {
75+ t .Errorf ("Minute = %d; want %d" , m , tc .Minute )
76+ }
77+ if s := d .Second (); s != tc .Second {
78+ t .Errorf ("Second = %d; want %d" , s , tc .Second )
79+ }
80+
81+ if ms := d .Nanosecond () / 1000000 ; ms != tc .MilliSecond {
82+ t .Errorf (
83+ "Millisecond = %d; want %d (%d nanoseconds)" ,
84+ ms ,
85+ tc .MilliSecond ,
86+ d .Nanosecond (),
87+ )
88+ }
89+
90+ _ , z := d .Zone ()
91+ if offset := float64 (z ) / 3600 ; offset != tc .Zone {
92+ t .Errorf ("Zone = %.2f (%d); want %.2f" , offset , z , tc .Zone )
93+ }
94+ }
95+
6096var cases = []TestCase {
6197 {
6298 Using : "2017-04-24T09:41:34.502+0100" ,
@@ -340,83 +376,32 @@ var cases = []TestCase{
340376
341377func TestParse (t * testing.T ) {
342378 for _ , c := range cases {
343- t .Run (c .Using , func (t * testing.T ) {
344- d , err := Parse ([]byte (c .Using ))
345- if c .CheckError (err , t ) {
346- return
347- }
348- t .Log (d )
349-
350- if y := d .Year (); y != c .Year {
351- t .Errorf ("Year = %d; want %d" , y , c .Year )
352- }
353- if m := int (d .Month ()); m != c .Month {
354- t .Errorf ("Month = %d; want %d" , m , c .Month )
355- }
356- if d := d .Day (); d != c .Day {
357- t .Errorf ("Day = %d; want %d" , d , c .Day )
358- }
359- if h := d .Hour (); h != c .Hour {
360- t .Errorf ("Hour = %d; want %d" , h , c .Hour )
361- }
362- if m := d .Minute (); m != c .Minute {
363- t .Errorf ("Minute = %d; want %d" , m , c .Minute )
364- }
365- if s := d .Second (); s != c .Second {
366- t .Errorf ("Second = %d; want %d" , s , c .Second )
367- }
368-
369- if ms := d .Nanosecond () / 1000000 ; ms != c .MilliSecond {
370- t .Errorf ("Millisecond = %d; want %d (%d nanoseconds)" , ms , c .MilliSecond , d .Nanosecond ())
371- }
372-
373- _ , z := d .Zone ()
374- if offset := float64 (z ) / 3600 ; offset != c .Zone {
375- t .Errorf ("Zone = %.2f (%d); want %.2f" , offset , z , c .Zone )
376- }
377- })
379+ t .Run (
380+ c .Using , func (t * testing.T ) {
381+ d , err := Parse ([]byte (c .Using ))
382+ if c .CheckError (err , t ) {
383+ return
384+ }
385+ t .Log (d )
386+ c .Check (d , t )
387+ },
388+ )
378389
379390 }
380391}
381392
382393func TestParseString (t * testing.T ) {
383394 for _ , c := range cases {
384- t .Run (c .Using , func (t * testing.T ) {
385- d , err := ParseString (c .Using )
386- if c .CheckError (err , t ) {
387- return
388- }
389- t .Log (d )
390-
391- if y := d .Year (); y != c .Year {
392- t .Errorf ("Year = %d; want %d" , y , c .Year )
393- }
394- if m := int (d .Month ()); m != c .Month {
395- t .Errorf ("Month = %d; want %d" , m , c .Month )
396- }
397- if d := d .Day (); d != c .Day {
398- t .Errorf ("Day = %d; want %d" , d , c .Day )
399- }
400- if h := d .Hour (); h != c .Hour {
401- t .Errorf ("Hour = %d; want %d" , h , c .Hour )
402- }
403- if m := d .Minute (); m != c .Minute {
404- t .Errorf ("Minute = %d; want %d" , m , c .Minute )
405- }
406- if s := d .Second (); s != c .Second {
407- t .Errorf ("Second = %d; want %d" , s , c .Second )
408- }
409-
410- if ms := d .Nanosecond () / 1000000 ; ms != c .MilliSecond {
411- t .Errorf ("Millisecond = %d; want %d (%d nanoseconds)" , ms , c .MilliSecond , d .Nanosecond ())
412- }
413-
414- _ , z := d .Zone ()
415- if offset := float64 (z ) / 3600 ; offset != c .Zone {
416- t .Errorf ("Zone = %.2f (%d); want %.2f" , offset , z , c .Zone )
417- }
418- })
419-
395+ t .Run (
396+ c .Using , func (t * testing.T ) {
397+ d , err := ParseString (c .Using )
398+ if c .CheckError (err , t ) {
399+ return
400+ }
401+ t .Log (d )
402+ c .Check (d , t )
403+ },
404+ )
420405 }
421406}
422407
@@ -429,3 +414,44 @@ func BenchmarkParse(b *testing.B) {
429414 }
430415 }
431416}
417+
418+ func TestParseStringInLocation (t * testing.T ) {
419+ cases := []TestCase {
420+ {
421+ Using : "2017-04-24T09:41:34.502+05:45" ,
422+ Year : 2017 , Month : 4 , Day : 24 ,
423+ Hour : 9 , Minute : 41 , Second : 34 ,
424+ MilliSecond : 502 ,
425+ Zone : 5.75 ,
426+ },
427+ {
428+ Using : "2017-04-24T09:41:34.502" ,
429+ Year : 2017 , Month : 4 , Day : 24 ,
430+ Hour : 9 , Minute : 41 , Second : 34 ,
431+ MilliSecond : 502 ,
432+ Zone : 5 ,
433+ },
434+ {
435+ Using : "2017-04-24T09:41:34.502Z" ,
436+ Year : 2017 , Month : 4 , Day : 24 ,
437+ Hour : 9 , Minute : 41 , Second : 34 ,
438+ MilliSecond : 502 ,
439+ Zone : 0 ,
440+ },
441+ }
442+
443+ loc := time .FixedZone ("UTC+5" , 5 * 60 * 60 )
444+
445+ for _ , c := range cases {
446+ t .Run (
447+ c .Using , func (t * testing.T ) {
448+ d , err := ParseStringInLocation (c .Using , loc )
449+ if c .CheckError (err , t ) {
450+ return
451+ }
452+ t .Log (d )
453+ c .Check (d , t )
454+ },
455+ )
456+ }
457+ }
0 commit comments