Browse Source

fixed since/before date inaccuracy with timezones

Drn 2 years ago
parent
commit
c23ffff4ab
4 changed files with 10 additions and 15 deletions
  1. 2 2
      commands.go
  2. 0 9
      common.go
  3. 2 2
      discord.go
  4. 6 2
      main.go

+ 2 - 2
commands.go

@@ -267,7 +267,7 @@ func handleCommands() *exrouter.Route {
 				} else if strings.Contains(strings.ToLower(argValue), "--before=") { // before key
 					before = strings.ReplaceAll(strings.ToLower(argValue), "--before=", "")
 					if isDate(before) {
-						beforeID = discordTimestampToSnowflake("2006-01-02", dateLocalToUTC(before))
+						beforeID = discordTimestampToSnowflake("2006-01-02", before)
 					} else if isNumeric(before) {
 						beforeID = before
 					}
@@ -278,7 +278,7 @@ func handleCommands() *exrouter.Route {
 				} else if strings.Contains(strings.ToLower(argValue), "--since=") { //  since key
 					since = strings.ReplaceAll(strings.ToLower(argValue), "--since=", "")
 					if isDate(since) {
-						sinceID = discordTimestampToSnowflake("2006-01-02", dateLocalToUTC(since))
+						sinceID = discordTimestampToSnowflake("2006-01-02", since)
 					} else if isNumeric(since) {
 						sinceID = since
 					}

+ 0 - 9
common.go

@@ -165,15 +165,6 @@ func isDate(s string) bool {
 	return err == nil
 }
 
-func dateLocalToUTC(s string) string {
-	if s == "" || !isDate(s) {
-		return ""
-	}
-	rawDate, _ := time.Parse("2006-01-02", s)
-	localDate := time.Date(rawDate.Year(), rawDate.Month(), rawDate.Day(), 0, 0, 0, 0, time.Local)
-	return fmt.Sprintf("%04d-%02d-%02d", localDate.In(time.UTC).Year(), localDate.In(time.UTC).Month(), localDate.In(time.UTC).Day())
-}
-
 /*func condenseString(input string, length int) string {
 	filler := "....."
 	ret := input

+ 2 - 2
discord.go

@@ -99,8 +99,8 @@ const (
 //TODO: Clean these two
 
 func discordTimestampToSnowflake(format string, timestamp string) string {
-	if t, err := time.Parse(format, timestamp); err == nil {
-		return fmt.Sprint(((t.Local().UnixNano() / int64(time.Millisecond)) - discordEpoch) << 22)
+	if t, err := time.ParseInLocation(format, timestamp, time.Local); err == nil {
+		return fmt.Sprint(((t.UnixNano() / int64(time.Millisecond)) - discordEpoch) << 22)
 	}
 	log.Println(lg("Main", "", color.HiRedString,
 		"Failed to convert timestamp to discord snowflake... Format: '%s', Timestamp: '%s' - Error:\t%s",

+ 6 - 2
main.go

@@ -251,8 +251,8 @@ func main() {
 			job.OriginUser = "AUTORUN"
 			job.TargetCommandingMessage = nil
 			job.TargetChannelID = arh.channel
-			job.TargetBefore = dateLocalToUTC(arh.before)
-			job.TargetSince = dateLocalToUTC(arh.since)
+			job.TargetBefore = arh.before
+			job.TargetSince = arh.since
 			job.Updated = time.Now()
 			job.Added = time.Now()
 			historyJobs[arh.channel] = job
@@ -406,6 +406,10 @@ func main() {
 
 	// ~~~ RUNNING
 
+	//#region ----------- TEST ENV / main
+
+	//#endregion ------------------------
+
 	//#region Exit...
 	signal.Notify(loop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt, os.Kill)
 	<-loop