WIP WIP, nae nae Exists, IsAmbiguous
This commit is contained in:
		
							parent
							
								
									e54a8c6241
								
							
						
					
					
						commit
						e09133ae54
					
				
							
								
								
									
										95
									
								
								time.go
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								time.go
									
									
									
									
									
								
							| @ -43,36 +43,28 @@ func main() { | |||||||
| 	/* funky times */ | 	/* funky times */ | ||||||
| 	tz := "America/Denver" | 	tz := "America/Denver" | ||||||
| 	fmt.Println("funky") | 	fmt.Println("funky") | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 	for _, st := range [][]int{ | ||||||
| 		01, 59, 00, 0}, tz) | 		[]int{2019, 3, 10, 01, 59, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 		[]int{2019, 3, 10, 02, 00, 00, 0}, | ||||||
| 		02, 00, 00, 0}, tz) | 		[]int{2019, 3, 10, 02, 01, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 		[]int{2019, 3, 10, 02, 59, 00, 0}, | ||||||
| 		02, 01, 00, 0}, tz) | 		[]int{2019, 3, 10, 03, 00, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 		[]int{2019, 3, 10, 03, 01, 00, 0}, | ||||||
| 		02, 59, 00, 0}, tz) | 		[]int{2019, 11, 03, 0, 59, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 		[]int{2019, 11, 03, 01, 59, 00, 0}, | ||||||
| 		03, 00, 00, 0}, tz) | 		[]int{2019, 11, 03, 02, 00, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 3, 10, | 		[]int{2019, 11, 03, 02, 01, 00, 0}, | ||||||
| 		03, 01, 00, 0}, tz) | 		[]int{2019, 11, 03, 02, 59, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 11, 03, | 		[]int{2019, 11, 03, 03, 00, 00, 0}, | ||||||
| 		0, 59, 00, 0}, tz) | 		[]int{2019, 11, 03, 03, 01, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 11, 03, | 		[]int{2019, 11, 03, 04, 01, 00, 0}, | ||||||
| 		01, 59, 00, 0}, tz) | 		[]int{2019, 11, 03, 05, 01, 00, 0}, | ||||||
| 	CheckInput2([]int{2019, 11, 03, | 	} { | ||||||
| 		02, 00, 00, 0}, tz) | 		err := IsAmbiguous(st, tz) | ||||||
| 	CheckInput2([]int{2019, 11, 03, | 		if nil != err { | ||||||
| 		02, 01, 00, 0}, tz) | 			fmt.Println(err) | ||||||
| 	CheckInput2([]int{2019, 11, 03, | 		} | ||||||
| 		02, 59, 00, 0}, tz) | 	} | ||||||
| 	CheckInput2([]int{2019, 11, 03, |  | ||||||
| 		03, 00, 00, 0}, tz) |  | ||||||
| 	CheckInput2([]int{2019, 11, 03, |  | ||||||
| 		03, 01, 00, 0}, tz) |  | ||||||
| 	CheckInput2([]int{2019, 11, 03, |  | ||||||
| 		04, 01, 00, 0}, tz) |  | ||||||
| 	CheckInput2([]int{2019, 11, 03, |  | ||||||
| 		05, 01, 00, 0}, tz) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type ErrNoExist struct { | type ErrNoExist struct { | ||||||
| @ -87,9 +79,9 @@ func (err ErrNoExist) Error() string { | |||||||
| 
 | 
 | ||||||
| // Check if the time is a real time in the given timezone. | // Check if the time is a real time in the given timezone. | ||||||
| // | // | ||||||
| // For example: 2:30am doesn't happen on 2019 Nov 3rd according | // For example: 2:30am doesn't happen on 2019 March 10th according | ||||||
| // to America/Denver local time, due to the end of Daylight Savings Time, | // to America/Denver local time, due to the end of Daylight Savings Time, | ||||||
| // but it did happen in America/Phoenix. | // but it does happen in America/Phoenix. | ||||||
| // | // | ||||||
| // Also rejects times that are parsable and return a valid date object, | // Also rejects times that are parsable and return a valid date object, | ||||||
| // but are not canonical, such as 24:60:75 November 31st, 2020. | // but are not canonical, such as 24:60:75 November 31st, 2020. | ||||||
| @ -97,13 +89,20 @@ func (err ErrNoExist) Error() string { | |||||||
| // | // | ||||||
| // Example of parsable, but non-canonical time: | // Example of parsable, but non-canonical time: | ||||||
| // | // | ||||||
| //     fmt.Println(time.Date(2020, time.November, 31, 25, 60, 0, 0, time.UTC)) | //     var loc *time.Location | ||||||
| //     "2020-12-02 02:01:15 +0000 UTC" | //     loc, _ = time.LoadLocation("America/Denver") | ||||||
|  | //     t := time.Date(2019, time.March, 10, 1, 30, 0, 0, loc) | ||||||
|  | //     fmt.Println(t, "==", t.UTC()) | ||||||
|  | //     // 2019-03-10 01:30:00 -0700 MST == 2019-03-10 08:30:00 +0000 UTC | ||||||
|  | //     t = t.Add(time.Duration(1) * time.Hour) | ||||||
|  | //     fmt.Println(t, "==", t.UTC()) | ||||||
|  | //     // 2019-03-10 03:30:00 -0600 MDT == 2019-03-10 09:30:00 +0000 UTC | ||||||
| // | // | ||||||
| // Example of a canonical, but non-parsable time (the 2016 leap second): | // Example of a canonical, but non-parsable time (the 2016 leap second): | ||||||
| // | // | ||||||
| //     fmt.Println(time.Date(2016, time.December, 31, 23, 59, 60, 0, time.UTC)) | //     fmt.Println(time.Date(2016, time.December, 31, 23, 59, 60, 0, time.UTC)) | ||||||
| //     "2020-12-02 02:00:00 +0000 UTC" // should be "2016-12-31 23:59:60 +0000 UTC" | //     "2020-12-02 02:00:00 +0000 UTC" // should be "2016-12-31 23:59:60 +0000 UTC" | ||||||
|  | // | ||||||
| func Exists(st []int, tzstr string) error { | func Exists(st []int, tzstr string) error { | ||||||
| 	tz, err := time.LoadLocation(tzstr) | 	tz, err := time.LoadLocation(tzstr) | ||||||
| 	if nil != err { | 	if nil != err { | ||||||
| @ -158,11 +157,27 @@ func Exists(st []int, tzstr string) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func IsAmbigious(st []int, tzstr string) error { | // Check if the time happens more than once in a given timezone. | ||||||
| 	return fmt.Errorf("Not Implemented") | // | ||||||
| } | // For example: 1:30am happens only once on 2019 Nov 3rd according to | ||||||
| 
 | // America/Phoenix time but due to the start of Daylight Savings Time, | ||||||
| func CheckInput2(st []int, tzstr string) error { | // it happens twice in America/Denver. | ||||||
|  | // | ||||||
|  | // Example of duplicate, non-canonical time: | ||||||
|  | // | ||||||
|  | //     var loc *time.Location | ||||||
|  | //     loc, _ = time.LoadLocation("America/Denver") | ||||||
|  | //     t = time.Date(2019, time.November, 3, 1, 30, 0, 0, loc) | ||||||
|  | //     fmt.Println(t, "==", t.UTC()) | ||||||
|  | //     // 2019-11-03 01:30:00 -0600 MDT == 2019-11-03 07:30:00 +0000 UTC | ||||||
|  | //     t = t.Add(time.Duration(1) * time.Hour) | ||||||
|  | //     fmt.Println(t, "==", t.UTC()) | ||||||
|  | //     // 2019-11-03 01:30:00 -0700 MST == 2019-11-03 08:30:00 +0000 UTC | ||||||
|  | //     t = t.Add(time.Duration(1) * time.Hour) | ||||||
|  | //     fmt.Println(t, "==", t.UTC()) | ||||||
|  | //     // 2019-11-03 02:30:00 -0700 MST == 2019-11-03 09:30:00 +0000 UTC | ||||||
|  | // | ||||||
|  | func IsAmbiguous(st []int, tzstr string) error { | ||||||
| 	// Does the time exist twice? | 	// Does the time exist twice? | ||||||
| 	// (if I change the time in UTC, do I still get the same time) | 	// (if I change the time in UTC, do I still get the same time) | ||||||
| 	// Note: Some timezones change by half or quarter hour | 	// Note: Some timezones change by half or quarter hour | ||||||
| @ -190,7 +205,7 @@ func CheckInput2(st []int, tzstr string) error { | |||||||
| 			fmt.Println("Ambiguous Time") | 			fmt.Println("Ambiguous Time") | ||||||
| 			fmt.Printf("%s, %s, %+d\n", t1, u1, n) | 			fmt.Printf("%s, %s, %+d\n", t1, u1, n) | ||||||
| 			fmt.Printf("%s, %s, %+d\n", t2, u2, n) | 			fmt.Printf("%s, %s, %+d\n", t2, u2, n) | ||||||
| 			return nil | 			return fmt.Errorf("Ambiguous") | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	//ta := | 	//ta := | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user