mirror of
https://github.com/dsoprea/go-exif.git
synced 2025-04-27 13:12:39 +00:00
utility: Added function to parse timestamps.
This commit is contained in:
parent
bb29f78ee7
commit
d480165b94
51
utility.go
51
utility.go
@ -3,6 +3,9 @@ package exif
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dsoprea/go-logging"
|
"github.com/dsoprea/go-logging"
|
||||||
)
|
)
|
||||||
@ -63,3 +66,51 @@ func DumpBytesClauseToString(data []byte) string {
|
|||||||
|
|
||||||
return b.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
|
||||||
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package exif
|
package exif
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/dsoprea/go-logging"
|
"github.com/dsoprea/go-logging"
|
||||||
)
|
)
|
||||||
@ -72,3 +73,15 @@ func TestDumpBytesClauseToString(t *testing.T) {
|
|||||||
t.Fatalf("result not expected")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user