Multiple implementations of the same back-end application. The aim is to provide quick, side-by-side comparisons of different technologies (languages, frameworks, libraries) while preserving consistent business logic across all implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.1 KiB

package dto
import (
"encoding/json"
"fmt"
"time"
)
// JSONTime is a custom time type that can unmarshal from JSON strings
type JSONTime struct {
time.Time
}
// UnmarshalJSON implements json.Unmarshaler interface
func (jt *JSONTime) UnmarshalJSON(data []byte) error {
// Remove quotes from JSON string
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
// Try to parse with different formats
formats := []string{
time.RFC3339,
"2006-01-02T15:04:05",
"2006-01-02T15:04:05Z",
"2006-01-02T15:04:05.000Z",
"2006-01-02T15:04:05.000000Z",
"2006-01-02T15:04:05.000000000Z",
}
for _, format := range formats {
if t, err := time.Parse(format, str); err == nil {
jt.Time = t
return nil
}
}
return fmt.Errorf("unable to parse time: %s", str)
}
// MarshalJSON implements json.Marshaler interface
func (jt JSONTime) MarshalJSON() ([]byte, error) {
return json.Marshal(jt.Time.Format(time.RFC3339))
}
// String returns the time in RFC3339 format
func (jt JSONTime) String() string {
return jt.Time.Format(time.RFC3339)
}