summaryrefslogtreecommitdiff
path: root/internal/timestamp
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-10 10:34:26 +0300
committerPaul Buetow <paul@buetow.org>2024-10-10 10:34:26 +0300
commit2f006df7a6aff97bb69bb2b6ffd607890863ebf9 (patch)
treeecbe059b685d94cafb0b6e0e3b01b1d5bb36bd44 /internal/timestamp
parentbe3abe2462bca511ede21cacd1e5a3690b5aa745 (diff)
initial timestamp package
Diffstat (limited to 'internal/timestamp')
-rw-r--r--internal/timestamp/timestamp.go41
-rw-r--r--internal/timestamp/timestamp_test.go23
2 files changed, 64 insertions, 0 deletions
diff --git a/internal/timestamp/timestamp.go b/internal/timestamp/timestamp.go
new file mode 100644
index 0000000..8c048df
--- /dev/null
+++ b/internal/timestamp/timestamp.go
@@ -0,0 +1,41 @@
+package timestamp
+
+import (
+ "strings"
+ "time"
+)
+
+const (
+ Format = "20060102-150405"
+)
+
+func Now() string {
+ return time.Now().Format(Format)
+}
+
+func NowTime() time.Time {
+ t, err := Parse(Now())
+ if err != nil {
+ panic(err)
+ }
+ return t
+}
+
+func Parse(timeStr string) (time.Time, error) {
+ return time.Parse(Format, timeStr)
+}
+func OldestValidTime() time.Time {
+ // The time this code was written a:round, actually.
+ oldestValidTime, err := Parse("20240912-102800")
+ if err != nil {
+ // Never expected
+ panic(err)
+ }
+ return oldestValidTime
+}
+
+func UpdateInFilename(filename string, rIndex int) string {
+ parts := strings.Split(filename, ".")
+ parts[len(parts)-rIndex] = Now()
+ return strings.Join(parts, "-")
+}
diff --git a/internal/timestamp/timestamp_test.go b/internal/timestamp/timestamp_test.go
new file mode 100644
index 0000000..27d0fd4
--- /dev/null
+++ b/internal/timestamp/timestamp_test.go
@@ -0,0 +1,23 @@
+package timestamp
+
+import (
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestUpdateInFilename(t *testing.T) {
+ var (
+ filePath = "gosdir/db/platforms/mastodon/1728240487.txt.20241009-232530.queued"
+ nowTime = time.Now()
+ updatedFilePath = UpdateInFilename(filePath, -2)
+ parts = strings.Split(updatedFilePath, ".")
+ )
+
+ updatedTime, err := Parse(parts[len(parts)-2])
+ if err != nil {
+ t.Error(err)
+ }
+
+ t.Log(filePath, updatedFilePath, nowTime.Sub(updatedTime))
+}