diff --git a/utility.go b/utility.go index 9032127..f09a5b3 100644 --- a/utility.go +++ b/utility.go @@ -3,6 +3,9 @@ package exif import ( "fmt" "bytes" + "strings" + "strconv" + "time" "github.com/dsoprea/go-logging" ) @@ -63,3 +66,51 @@ func DumpBytesClauseToString(data []byte) string { return b.String() } + +func ParseExifFullTimestamp(fullTimestampPhrase string) (timestamp time.Time, err error) { + defer func() { + if state := recover(); state != nil { + err = log.Wrap(state.(error)) + } + }() + + parts := strings.Split(fullTimestampPhrase, " ") + datestampValue, timestampValue := parts[0], parts[1] + + dateParts := strings.Split(datestampValue, ":") + + year, err := strconv.ParseUint(dateParts[0], 10, 16) + if err != nil { + log.Panicf("could not parse year") + } + + month, err := strconv.ParseUint(dateParts[1], 10, 8) + if err != nil { + log.Panicf("could not parse month") + } + + day, err := strconv.ParseUint(dateParts[2], 10, 8) + if err != nil { + log.Panicf("could not parse day") + } + + timeParts := strings.Split(timestampValue, ":") + + hour, err := strconv.ParseUint(timeParts[0], 10, 8) + if err != nil { + log.Panicf("could not parse hour") + } + + minute, err := strconv.ParseUint(timeParts[1], 10, 8) + if err != nil { + log.Panicf("could not parse minute") + } + + second, err := strconv.ParseUint(timeParts[2], 10, 8) + if err != nil { + log.Panicf("could not parse second") + } + + timestamp = time.Date(int(year), time.Month(month), int(day), int(hour), int(minute), int(second), 0, time.UTC) + return timestamp, nil +} diff --git a/utility_test.go b/utility_test.go index 98bbca7..7274c82 100644 --- a/utility_test.go +++ b/utility_test.go @@ -1,10 +1,11 @@ package exif import ( + "io/ioutil" + "testing" "os" - - "io/ioutil" + "time" "github.com/dsoprea/go-logging" ) @@ -72,3 +73,15 @@ func TestDumpBytesClauseToString(t *testing.T) { t.Fatalf("result not expected") } } + +func TestParseExifFullTimestamp(t *testing.T) { + timestamp, err := ParseExifFullTimestamp("2018:11:30 13:01:49") + log.PanicIf(err) + + actual := timestamp.Format(time.RFC3339) + expected := "2018-11-30T13:01:49Z" + + if actual != expected { + t.Fatalf("time not formatted correctly: [%s] != [%s]", actual, expected) + } +}