Repository navigation
Expand file tree
/
Copy pathtiming.go
More file actions
113 lines (104 loc) · 3.15 KB
/
Copy pathtiming.go
File metadata and controls
113 lines (104 loc) · 3.15 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
// When somebody posts, and what that says about where they are.
//
// Everybody sleeps, and almost nobody posts while they do. The longest
// quiet stretch in a few hundred timestamps is a night, and the middle of a
// night is about four in the morning wherever you are. That puts an account
// in a band of timezones without it ever naming a place, and it is the one
// thing in this program that works on an account that has said nothing at
// all about itself.
import (
"fmt"
"math"
)
type band struct {
known bool
// lo and hi are UTC offsets in minutes.
lo, hi int
// quietFrom and quietHours are the stretch in UTC the band was read
// from, for the report to show its working.
quietFrom, quietHours int
posts int
}
func (b band) covers(off int) bool { return b.known && off >= b.lo && off <= b.hi }
// share is the part of a country living inside the band.
func (b band) share(zones map[int]float64) float64 {
total := 0.0
for off, s := range zones {
if b.covers(off) {
total += s
}
}
return total
}
func (b band) String() string {
return fmt.Sprintf("%s to %s", offsetName(b.lo), offsetName(b.hi))
}
func offsetName(minutes int) string {
sign := "+"
if minutes < 0 {
sign = "-"
minutes = -minutes
}
if minutes%60 == 0 {
return fmt.Sprintf("UTC%s%d", sign, minutes/60)
}
return fmt.Sprintf("UTC%s%d:%02d", sign, minutes/60, minutes%60)
}
const (
// enoughTimes is the fewest timestamps this reads a night from. Below
// it a quiet stretch is as likely to be a small sample as a bedtime.
enoughTimes = 60
// midSleep is the hour a night is centred on. Surveys of sleep on free
// days put the middle of it between three and five for most adults.
midSleep = 4.0
// bandHours is how far either side of the reading the band runs. Two
// hours covers the early risers and the late ones; it does not cover
// night shifts, and the README says so.
bandHours = 2
)
// readClock finds the quiet stretch in the hours an account posts at. An
// hour counts as quiet if it holds under a sixth of an ordinary hour's
// posts: anybody with a few thousand of them has the odd one at four in
// the morning, and requiring silence would find no night at all.
func readClock(posts []Post) band {
var byHour [24]float64
n := 0
for _, p := range posts {
if p.When.IsZero() || p.Kind == Draft {
continue
}
byHour[p.When.UTC().Hour()]++
n++
}
b := band{posts: n}
if n < enoughTimes {
return b
}
quiet := float64(n) / 24 / 6
bestStart, bestLen := 0, 0
for start := range 24 {
if byHour[start] > quiet || byHour[(start+23)%24] <= quiet {
continue
}
length := 0
for length < 24 && byHour[(start+length)%24] <= quiet {
length++
}
if length > bestLen {
bestStart, bestLen = start, length
}
}
// A night is somewhere between four hours and twelve. Less is a lull,
// more is somebody who only posts from work.
if bestLen < 4 || bestLen > 12 {
return b
}
middle := float64(bestStart) + float64(bestLen)/2
offset := math.Mod(midSleep-middle+36, 24) - 12
b.known = true
b.lo = int((offset - bandHours) * 60)
b.hi = int((offset + bandHours) * 60)
b.quietFrom, b.quietHours = bestStart, bestLen
return b
}