| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | package tools | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-10-07 15:50:33 -05:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dp "github.com/markusmobius/go-dateparser" | 
					
						
							|  |  |  | 	"github.com/markusmobius/go-dateparser/date" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | const DateFormat = "January 02, 2006 at 03:04:05PM -0700" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | // These are somewhat arbitrary, but reasonably useful min and max times | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 | 
					
						
							|  |  |  | 	MaxTime = MinTime.Add(1<<63 - 1) | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ParseDate(in string) (t time.Time, err error) { | 
					
						
							|  |  |  | 	if in == "min" { | 
					
						
							|  |  |  | 		return MinTime, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if in == "max" { | 
					
						
							|  |  |  | 		return MaxTime, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-07 15:50:33 -05:00
										 |  |  | 	var er error | 
					
						
							|  |  |  | 	for _, format := range []string{time.RFC3339, DateFormat} { | 
					
						
							|  |  |  | 		if t, er = time.ParseInLocation(format, in, nil); er == nil { | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ts int64 | 
					
						
							|  |  |  | 	if ts, er = strconv.ParseInt(in, 10, 0); er == nil { | 
					
						
							|  |  |  | 		t = time.Unix(ts, 0) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | 	conf := dp.Configuration{ | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		CurrentTime:        time.Now().Local(), | 
					
						
							|  |  |  | 		ReturnTimeAsPeriod: true, | 
					
						
							| 
									
										
										
										
											2024-02-25 13:12:13 -06:00
										 |  |  | 		Languages:          []string{"en"}, | 
					
						
							| 
									
										
										
										
											2024-02-25 12:36:43 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d, err := dp.Parse(&conf, in) | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	t = d.Time | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location() | 
					
						
							| 
									
										
										
										
											2024-10-07 15:50:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	switch d.Period { | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 	case date.Second: | 
					
						
							|  |  |  | 		t = t.Truncate(time.Second) | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	case date.Minute: | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		t = t.Truncate(time.Minute) | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	case date.Hour: | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		t = time.Date(y, mon, day, h, 0, 0, 0, loc) | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	case date.Day: | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 		t = time.Date(y, mon, day, 0, 0, 0, 0, loc) | 
					
						
							|  |  |  | 	case date.Month: | 
					
						
							|  |  |  | 		t = time.Date(y, mon, 1, 0, 0, 0, 0, loc) | 
					
						
							|  |  |  | 	case date.Year: | 
					
						
							|  |  |  | 		t = time.Date(y, 1, 1, 0, 0, 0, 0, loc) | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-25 09:53:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:38:27 -06:00
										 |  |  | 	return | 
					
						
							|  |  |  | } |