Parse multiple different time formates

This commit is contained in:
2026-02-06 17:15:42 +01:00
parent b75697a39f
commit a925e4ab78
2 changed files with 27 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"sheetless-server/database" "sheetless-server/database"
"sheetless-server/models" "sheetless-server/models"
@@ -17,6 +18,27 @@ type AnnotationRequest struct {
Annotations string `json:"annotations" binding:"required"` Annotations string `json:"annotations" binding:"required"`
} }
// parseFlexibleTime parses time strings in various ISO8601/RFC3339 formats
func parseFlexibleTime(s string) (time.Time, error) {
// Try multiple formats that Dart/Flutter might send
formats := []string{
time.RFC3339,
time.RFC3339Nano,
"2006-01-02T15:04:05.999999999", // Dart ISO8601 without timezone
"2006-01-02T15:04:05.999999", // Dart ISO8601 with microseconds
"2006-01-02T15:04:05.999", // Dart ISO8601 with milliseconds
"2006-01-02T15:04:05", // Dart ISO8601 without fractional seconds
}
for _, format := range formats {
if t, err := time.Parse(format, s); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unable to parse time: %s", s)
}
// GetAnnotations returns all annotations for a sheet // GetAnnotations returns all annotations for a sheet
// GET /api/sheets/:uuid/annotations // GET /api/sheets/:uuid/annotations
func GetAnnotations(c *gin.Context) { func GetAnnotations(c *gin.Context) {
@@ -65,10 +87,10 @@ func UpdateAnnotation(c *gin.Context) {
return return
} }
// Parse lastModified timestamp // Parse lastModified timestamp (flexible format to support Dart/Flutter)
lastModified, err := time.Parse(time.RFC3339, req.LastModified) lastModified, err := parseFlexibleTime(req.LastModified)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid lastModified format, expected RFC3339"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid lastModified format: " + err.Error()})
return return
} }

View File

@@ -4,7 +4,6 @@ import (
"net/http" "net/http"
"sheetless-server/database" "sheetless-server/database"
"sheetless-server/models" "sheetless-server/models"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
@@ -59,8 +58,8 @@ func SyncChanges(c *gin.Context) {
continue // Skip invalid UUIDs continue // Skip invalid UUIDs
} }
// Parse createdAt timestamp // Parse createdAt timestamp (flexible format to support Dart/Flutter)
createdAt, err := time.Parse(time.RFC3339, change.CreatedAt) createdAt, err := parseFlexibleTime(change.CreatedAt)
if err != nil { if err != nil {
continue // Skip invalid timestamps continue // Skip invalid timestamps
} }