-
Notifications
You must be signed in to change notification settings - Fork 3
/
feedx.go
57 lines (46 loc) · 1.29 KB
/
feedx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package feedx
import (
"context"
"errors"
"strconv"
"time"
"github.com/bsm/bfs"
)
// ErrNotModified is used to signal that something has not been modified.
var ErrNotModified = errors.New("feedx: not modified")
const (
metaLastModified = "X-Feedx-Last-Modified"
metaPusherLastModified = "X-Feedx-Pusher-Last-Modified"
)
// Timestamp with millisecond resolution
type timestamp int64
func timestampFromTime(t time.Time) timestamp {
if n := t.Unix()*1000 + int64(t.Nanosecond()/1e6); n > 0 {
return timestamp(n)
}
return 0
}
func remoteLastModified(ctx context.Context, obj *bfs.Object) (timestamp, error) {
info, err := obj.Head(ctx)
if err == bfs.ErrNotFound {
return 0, nil
} else if err != nil {
return 0, err
}
millis, _ := strconv.ParseInt(info.Metadata.Get(metaLastModified), 10, 64)
if millis == 0 {
millis, _ = strconv.ParseInt(info.Metadata.Get(metaPusherLastModified), 10, 64)
}
return timestamp(millis), nil
}
// Millis returns the number of milliseconds since epoch.
func (t timestamp) Millis() int64 { return int64(t) }
// Time returns the time at t.
func (t timestamp) Time() time.Time {
n := t.Millis()
return time.Unix(n/1000, n%1000*1e6)
}
// String returns a string of milliseconds.
func (t timestamp) String() string {
return strconv.FormatInt(int64(t), 10)
}