summaryrefslogtreecommitdiff
path: root/internal
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
parentbe3abe2462bca511ede21cacd1e5a3690b5aa745 (diff)
initial timestamp package
Diffstat (limited to 'internal')
-rw-r--r--internal/entry/entry.go17
-rw-r--r--internal/entry/entry_test.go5
-rw-r--r--internal/format/time.go3
-rw-r--r--internal/queue/queue.go6
-rw-r--r--internal/schedule/stats.go15
-rw-r--r--internal/timestamp/timestamp.go41
-rw-r--r--internal/timestamp/timestamp_test.go23
7 files changed, 76 insertions, 34 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 0874e64..5a9c0fb 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -7,7 +7,7 @@ import (
"strings"
"time"
- "codeberg.org/snonux/gos/internal/format"
+ "codeberg.org/snonux/gos/internal/timestamp"
)
type State int
@@ -31,9 +31,6 @@ func (s State) String() string {
}
}
-// The time this code was written a:round, actually.
-const oldestValidTime = "20240912-102800"
-
type Entry struct {
Path string
Time time.Time
@@ -42,7 +39,7 @@ type Entry struct {
func (e Entry) String() string {
return fmt.Sprintf("Path:%s;Stamp:%s,State:%s",
- e.Path, e.Time.Format(format.Time), e.State)
+ e.Path, e.Time.Format(timestamp.Format), e.State)
}
var Zero = Entry{}
@@ -67,16 +64,11 @@ func New(filePath string) (Entry, error) {
}
var err error
- if e.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil {
+ if e.Time, err = timestamp.Parse(parts[len(parts)-2]); err != nil {
return e, err
}
- oldestValid, err := time.Parse(format.Time, oldestValidTime)
- if err != nil {
- panic(err)
- }
-
- if e.Time.Before(oldestValid) {
+ if e.Time.Before(timestamp.OldestValidTime()) {
return e, fmt.Errorf("entry time does not seem legit, it is too old: %v", e.Time)
}
@@ -96,6 +88,7 @@ func (e *Entry) MarkPosted() error {
return errors.New("entry is already posted")
}
// TODO: Also update the timestamp to reflect the posting time in the file path.
+ // Write a timestamp.Update() function for this.
if err := os.Rename(e.Path, strings.TrimSuffix(e.Path, ".queued")+".posted"); err != nil {
return err
}
diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go
index 5e3a5d3..0ec8166 100644
--- a/internal/entry/entry_test.go
+++ b/internal/entry/entry_test.go
@@ -3,9 +3,8 @@ package entry
import (
"fmt"
"testing"
- "time"
- "codeberg.org/snonux/gos/internal/format"
+ "codeberg.org/snonux/gos/internal/timestamp"
)
func TestEntry(t *testing.T) {
@@ -27,7 +26,7 @@ func TestEntry(t *testing.T) {
t.Errorf("expected state %s but got %s", state, ent.State)
}
- expectedTime, err := time.Parse(format.Time, stamp)
+ expectedTime, err := timestamp.Parse(stamp)
if err != nil {
t.Error(err)
}
diff --git a/internal/format/time.go b/internal/format/time.go
deleted file mode 100644
index cf35722..0000000
--- a/internal/format/time.go
+++ /dev/null
@@ -1,3 +0,0 @@
-package format
-
-const Time = "20060102-150405"
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index 98a30ac..fcef897 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -7,11 +7,10 @@ import (
"path/filepath"
"slices"
"strings"
- "time"
"codeberg.org/snonux/gos/internal/config"
- "codeberg.org/snonux/gos/internal/format"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/timestamp"
)
// Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files.
@@ -38,10 +37,9 @@ func queueEntries(args config.Args) error {
return err
}
- now := time.Now()
for filePath := range ch {
destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir,
- filepath.Base(filePath), now.Format(format.Time))
+ filepath.Base(filePath), timestamp.Now())
if args.DryRun {
log.Println("Not queueing entry", filePath, "to", destPath, "as dry-run mode enabled")
continue
diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go
index 847aead..172272f 100644
--- a/internal/schedule/stats.go
+++ b/internal/schedule/stats.go
@@ -7,8 +7,8 @@ import (
"time"
"codeberg.org/snonux/gos/internal/entry"
- "codeberg.org/snonux/gos/internal/format"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/timestamp"
)
// Posting stats
@@ -45,7 +45,7 @@ func (s stats) targetHit() bool {
func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error {
var (
- now time.Time = nowTime()
+ now time.Time = timestamp.NowTime()
oldest time.Time = now
)
@@ -95,15 +95,6 @@ func (s *stats) gatherQueuedStats(dir string) error {
return err
}
-// Make a simpler "now" time which gets rid of any extra information like offsets etc.
-func nowTime() time.Time {
- simplerNow, err := time.Parse(format.Time, time.Now().Format(format.Time))
- if err != nil {
- panic(err)
- }
- return simplerNow
-}
-
func pastTime(duration time.Duration) time.Time {
- return nowTime().Add(-duration)
+ return timestamp.NowTime().Add(-duration)
}
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))
+}