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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// this program was created in 3BBE.81[sft].
// usage:
// to convert to sfttime:
//
// sfttime [c date] [digitcount] [nodate]
// if date is given, converts the given date to sfttime.
// if date is not given, converts the current date to sfttime.
// digitcount specifies the accuracy for the time part.
// nodate hides the date part.
//
// to convert from sfttime:
//
// sfttime r sfttime [unix]
// converts the given sfttime to 'standard' time.
// if 'unix' is provided, the output will be in unix time.
//
// to show info about sfttime units:
//
// sfttime i [[sft]]$num
// displays name of unit [sft]$num, as well as it's value
// in both days and 'standard' units.
package main
import (
"fmt"
"math"
"math/big"
"os"
"regexp"
"strconv"
"strings"
"time"
)
// SFT_EPOCH_UNIX is the unix timestamp of [sft]epoch (1970-01-01 13:37:00 UTC).
const SFT_EPOCH_UNIX = 49020
// secondsPerDay is the number of seconds in a day.
const secondsPerDay = 86400
// unitNames maps [sft] exponent to alternative name.
var unitNames = map[int]string{
-4: "[sft]tick",
-3: "[sft]tentacle",
-2: "[sft]schinken",
-1: "[sft]major",
0: "day",
1: "[sft]vergil",
2: "[sft]stallman",
3: "[sft]odin",
}
// toSfttime converts a unix timestamp (as float64 seconds) to a sfttime string
// with the given number of fractional hex digits. If noDate is true, omits the
// integer (date) part.
func toSfttime(unixtime float64, digits int, noDate bool) string {
// sfttime = (unixtime - epoch) / secondsPerDay, in base 16
sftDays := (unixtime - SFT_EPOCH_UNIX) / secondsPerDay
intPart := int64(sftDays)
fracPart := sftDays - float64(intPart)
// format integer part as uppercase hex
datePart := strings.ToUpper(fmt.Sprintf("%X", intPart))
// format fractional part: multiply by 16^digits and take integer
scale := math.Pow(16, float64(digits))
fracHex := strings.ToUpper(fmt.Sprintf("%0*X", digits, int64(fracPart*scale)))
// trim to exactly digits characters
if len(fracHex) > digits {
fracHex = fracHex[len(fracHex)-digits:]
}
switch {
case digits == 0:
return datePart + "[sft]"
case noDate:
return "." + fracHex + "[sft]"
default:
return datePart + "." + fracHex + "[sft]"
}
}
// fromSfttime converts a sfttime string to a unix timestamp (seconds since epoch).
func fromSfttime(sftStr string) (float64, error) {
// parse hex float: integer and optional fractional part
parts := strings.SplitN(sftStr, ".", 2)
intVal, ok := new(big.Int).SetString(parts[0], 16)
if !ok {
return 0, fmt.Errorf("invalid sfttime integer part: %s", parts[0])
}
days := float64(intVal.Int64())
if len(parts) == 2 {
frac := parts[1]
fracVal, ok2 := new(big.Int).SetString(frac, 16)
if !ok2 {
return 0, fmt.Errorf("invalid sfttime fractional part: %s", frac)
}
scale := math.Pow(16, float64(len(frac)))
days += float64(fracVal.Int64()) / scale
}
unixtime := days*secondsPerDay + SFT_EPOCH_UNIX
return unixtime, nil
}
// printInfo prints the name and time equivalents of [sft]$exp.
func printInfo(exp int) {
name := fmt.Sprintf("[sft]%d", exp)
if alt, ok := unitNames[exp]; ok {
fmt.Printf("alternative name for %s: %s\n", name, alt)
name = alt
}
// value in days: 16^exp
days := math.Pow(16, float64(exp))
seconds := days * secondsPerDay
sftStr := strings.ToUpper(fmt.Sprintf("%X", int64(math.Pow(16, float64(exp))))) + "[sft]"
fmt.Printf("1 %s after [sft]epoch:\n", name)
fmt.Println(sftStr)
fmt.Printf("time equivalent of 1 %s:\n", name)
// caesium periods: 9192631770 per second
caesium := new(big.Float).SetPrec(128).SetFloat64(794243384928000)
expVal := new(big.Float).SetPrec(128).SetFloat64(math.Pow(16, float64(exp)))
caesium.Mul(caesium, expVal)
fmt.Printf("the duration of %s periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium 133 atom\n", caesium.Text('f', 0))
fmt.Println("standard time units equivalent:")
switch {
case seconds < 60:
fmt.Printf("%g seconds\n", seconds)
case seconds < 3600:
fmt.Printf("%g minutes\n", seconds/60)
case seconds < secondsPerDay:
fmt.Printf("%g hours\n", seconds/3600)
case seconds < secondsPerDay*365.2425:
fmt.Printf("%g days\n", days)
default:
fmt.Printf("%g years\n", days/365.2425)
}
}
var sftRe = regexp.MustCompile(`(?i)^([0-9A-F]*(?:\.[0-9A-F]+)?)(?:\[[sS][fF][tT]\])?$`)
var infoRe = regexp.MustCompile(`(?i)^(?:\[[sS][fF][tT]\])?(-?[0-9]+)$`)
var epochRe = regexp.MustCompile(`(?i)^(?:\[[sS][fF][tT]\])?[eE][pP][oO][cC][hH]$`)
func main() {
args := os.Args[1:]
if len(args) == 0 {
// default: convert current time to sfttime with 3 fractional digits
unixtime := float64(time.Now().UnixNano()) / 1e9
fmt.Println(toSfttime(unixtime, 3, false))
return
}
switch args[0] {
case "c":
// convert given date to sfttime
args = args[1:]
var unixtime float64
if len(args) > 0 {
t, err := time.Parse("2006-01-02 15:04:05", args[0])
if err != nil {
// try unix timestamp
ts, err2 := strconv.ParseFloat(args[0], 64)
if err2 != nil {
fmt.Fprintln(os.Stderr, "error parsing date:", err)
os.Exit(1)
}
unixtime = ts
} else {
unixtime = float64(t.Unix())
}
args = args[1:]
} else {
unixtime = float64(time.Now().UnixNano()) / 1e9
}
digits := 3
noDate := false
if len(args) > 0 {
if d, err := strconv.Atoi(args[0]); err == nil {
digits = d
args = args[1:]
}
}
if len(args) > 0 && args[0] == "nodate" {
noDate = true
}
fmt.Println(toSfttime(unixtime, digits, noDate))
case "r":
// convert sfttime to standard time
args = args[1:]
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "error")
os.Exit(1)
}
m := sftRe.FindStringSubmatch(args[0])
if m == nil || m[1] == "" {
fmt.Fprintln(os.Stderr, "error")
os.Exit(1)
}
sftStr := strings.ToUpper(m[1])
args = args[1:]
unixtime, err := fromSfttime(sftStr)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
if len(args) > 0 && args[0] == "unix" {
fmt.Println(unixtime)
} else {
t := time.Unix(int64(unixtime), 0).UTC()
fmt.Println(t.Format("Mon Jan 2 15:04:05 MST 2006"))
}
case "i":
// show info about a sfttime unit
args = args[1:]
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "error")
os.Exit(1)
}
if epochRe.MatchString(args[0]) {
fmt.Println("[sft]epoch:")
fmt.Printf("unix time %d\n", SFT_EPOCH_UNIX)
fmt.Println("1970-01-01 13:37:00 UTC")
return
}
m := infoRe.FindStringSubmatch(args[0])
if m == nil {
fmt.Fprintln(os.Stderr, "error")
os.Exit(1)
}
exp, _ := strconv.Atoi(m[1])
printInfo(exp)
default:
// treat as: [digitcount] [nodate]
digits := 3
noDate := false
if d, err := strconv.Atoi(args[0]); err == nil {
digits = d
args = args[1:]
}
if len(args) > 0 && args[0] == "nodate" {
noDate = true
}
unixtime := float64(time.Now().UnixNano()) / 1e9
fmt.Println(toSfttime(unixtime, digits, noDate))
}
}
|