utility: Added function to parse timestamps.

pull/12/head
Dustin Oprea 2019-01-01 22:55:30 -05:00
parent bb29f78ee7
commit d480165b94
2 changed files with 66 additions and 2 deletions

View File

@ -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
}

View File

@ -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)
}
}