| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | package tools | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | const day = time.Hour * 24 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | func TestParseDate(t *testing.T) { | 
					
						
							|  |  |  | 	now := time.Now().Local() | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location() | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	sec := now.Truncate(time.Second) | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	today := time.Date(y, mon, d, 0, 0, 0, 0, loc) | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	tomorrow := today.Add(day) | 
					
						
							|  |  |  | 	yesterday := today.Add(-day) | 
					
						
							|  |  |  | 	twoMin := now.Add(2 * time.Minute).Truncate(time.Minute) | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc) | 
					
						
							|  |  |  | 	firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) | 
					
						
							|  |  |  | 	firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) | 
					
						
							|  |  |  | 	exact := "2075-02-12T12:13:54.536-02:00" | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | 	exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200)) | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	var ts int64 = 1708876012 | 
					
						
							|  |  |  | 	tsd := time.Unix(ts, 0) | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | 	ent := "February 25, 2024 at 04:00:13AM +0230" | 
					
						
							|  |  |  | 	entd, _ := time.Parse(DateFormat, ent) | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name string | 
					
						
							|  |  |  | 		exp  time.Time | 
					
						
							|  |  |  | 		err  string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"now", sec, ""}, | 
					
						
							|  |  |  | 		{"today", today, ""}, | 
					
						
							|  |  |  | 		{"tomorrow", tomorrow, ""}, | 
					
						
							|  |  |  | 		{"yesterday", yesterday, ""}, | 
					
						
							|  |  |  | 		{"in two minutes", twoMin, ""}, | 
					
						
							|  |  |  | 		{"in two hours", twoHour, ""}, | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		{"this month", firstMonth, ""}, | 
					
						
							|  |  |  | 		{"this year", firstYear, ""}, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 		{"min", MinTime, ""}, | 
					
						
							|  |  |  | 		{"max", MaxTime, ""}, | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		{exact, exactd, ""}, | 
					
						
							|  |  |  | 		{fmt.Sprint(ts), tsd, ""}, | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | 		{ent, entd, ""}, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 		{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		t.Run(tt.name, getDateTest(tt.name, tt.exp, tt.err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getDateTest(in string, exp time.Time, err string) func(t *testing.T) { | 
					
						
							|  |  |  | 	return func(t *testing.T) { | 
					
						
							|  |  |  | 		out, er := ParseDate(in) | 
					
						
							|  |  |  | 		if err != "" { | 
					
						
							|  |  |  | 			assert.ErrorContains(t, er, err) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			require.NoError(t, er) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, exp, out) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |