Skip to content

Instantly share code, notes, and snippets.

@oliverkwebb
Last active September 8, 2024 18:20
Print the bounds of the epoch at various resolution and (signed) integer width
package main
import "fmt"
import "math"
import "time"
func main() {
tz, err := time.LoadLocation("UTC")
if err != nil { // Always check errors even if they should not happen.
panic(err)
}
a := [4]int64{1<<23, 1<<31, 1<<52, math.MaxInt64};
fmt.Println("Type/Resolution, float, int, double, long/x87 long double");
fmt.Print("Width");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(math.Log2((float64)(a[x])));
}
fmt.Print("\n");
fmt.Print("1 ns");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.Unix(0, a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("-1 ns");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.Unix(0, -a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("10 ns");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMicro(a[x]/100).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("-10 ns");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMicro(-(a[x]/100)).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("1 us");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMicro(a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("-1 us");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMicro(-a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("1 ms");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMilli(a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("-1 ms");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.UnixMilli(-a[x]).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("1 s");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.Unix(a[x], 0).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
fmt.Print("-1 s");
for x := 0; x < len(a); x++ {
fmt.Print(", ");
fmt.Print(time.Unix(-a[x], 0).In(tz).Format("2006-01-02T15:04"));
}
fmt.Print("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment