diff --git a/bin/arm/go-raspberry-pi-timelapse b/bin/arm/go-raspberry-pi-timelapse index 13cdd13..87b873b 100755 Binary files a/bin/arm/go-raspberry-pi-timelapse and b/bin/arm/go-raspberry-pi-timelapse differ diff --git a/timelapse/camera.go b/timelapse/camera.go index 39db41a..6d2fd8d 100644 --- a/timelapse/camera.go +++ b/timelapse/camera.go @@ -4,6 +4,7 @@ package timelapse import ( "errors" + "fmt" "log" "os/exec" "path/filepath" @@ -12,7 +13,6 @@ import ( ) const ( - timeStampFormat = "2006-01-02_15:04:05" commandRaspistill = "raspistill" ) @@ -73,7 +73,10 @@ func (c *Camera) getRaspistillArgs(fullPath string) []string { return append(args, "-o", fullPath) } +func getFileName(t time.Time) string { + return fmt.Sprintf("%4d%02d%02d-%02d%02d%02d.jpg", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) +} + func (c *Camera) getAbsoluteFilepath() string { - fileName := time.Now().Format(timeStampFormat) + ".jpg" - return filepath.Join(c.savePath, fileName) + return filepath.Join(c.savePath, getFileName(time.Now())) } diff --git a/timelapse/camera_test.go b/timelapse/camera_test.go index ff0d304..fb0f22d 100644 --- a/timelapse/camera_test.go +++ b/timelapse/camera_test.go @@ -3,6 +3,7 @@ package timelapse import ( "strings" "testing" + "time" "github.com/facebookgo/ensure" ) @@ -20,9 +21,9 @@ func TestBuildingArguments(t *testing.T) { ensure.False(t, unrotatedCamera.flipHorizontally) p := unrotatedCamera.getAbsoluteFilepath() + // Example: "foo/20210913-184442.jpg" ensure.DeepEqual(t, 0, strings.Index(p, "foo/")) - ensure.DeepEqual(t, 23, strings.Index(p, ".jpg")) - ensure.DeepEqual(t, 27, len(p)) + ensure.DeepEqual(t, strings.Index(p, ".jpg"), 19) } func TestRaspistillArgs(t *testing.T) { @@ -32,7 +33,15 @@ func TestRaspistillArgs(t *testing.T) { } func TestCreateRotatedCamera(t *testing.T) { - rotatedCamera, _ := NewCamera("foo", 200, 100, true, 100) + rotatedCamera, err := NewCamera("foo", 200, 100, true, 100) + ensure.Nil(t, err) ensure.True(t, rotatedCamera.flipVertically) ensure.True(t, rotatedCamera.flipHorizontally) } + +func TestCreateFileName(t *testing.T) { + ensure.DeepEqual(t, getFileName(time.Date(1974, time.January, 1, 1, 0, 0, 0, time.UTC)), "19740101-010000.jpg") + ensure.DeepEqual(t, getFileName(time.Date(1974, time.January, 19, 1, 2, 3, 0, time.UTC)), "19740119-010203.jpg") + ensure.DeepEqual(t, getFileName(time.Date(1974, time.May, 19, 1, 2, 3, 0, time.UTC)), "19740519-010203.jpg") + ensure.DeepEqual(t, getFileName(time.Date(1974, time.December, 19, 1, 2, 3, 4, time.UTC)), "19741219-010203.jpg") +}