github.com/zerjioang/time32@v0.0.0-20211102104504-b756043b9843/README.md (about) 1 # `uint32` time for Golang 2 3 > Note: this package is based on `Golang` official time implementation. 4 5 ## Features 6 7 * Get current time as `uint32` epoch (millis). 8 * Same performance as `time` package from standard Go library. 9 * Less GC pressure by removing timezone location data related fields. 10 * Time cache for high speed time requests. Default update frequency: 0.1s 11 12 ## Differences from `time.Time` package 13 14 * `*loc` pointer usage has been removed from `Time` struct to avoid pointer pressure on GC cycles. 15 * Included a method `Epoch()` that returns current epoch time as `uint32` instead of `int64`. This means, we can store our time data in **4 bytes**. 16 17 ## Usage 18 19 ```go 20 21 package main 22 23 import ( 24 "fmt" 25 "github.com/zerjioang/time32" 26 ) 27 28 func main(){ 29 tt := time32.Epoch() 30 fmt.Println(tt) 31 } 32 ``` 33 34 ```bash 35 # prints current time in epoch format 36 Example: 1601452800 37 ``` 38 39 An example is provided at ./example/main.go 40 41 ## Cached timing 42 43 For application that require a constant access to system time, a cached mechanism is included that keeps last time on a **0.1s** refresh rate. 44 To use this caching features, available methods are: 45 46 * `ReuseTime` 47 * `ReuseUnix` 48 * `ReuseUnixNano` 49 50 Previous method will return last value within a 0.1s window. Note that this feature might be useful for adding a timestamp to logs, expiration check, etc. 51 52 ## Performance 53 54 ```bash 55 goos: linux 56 goarch: amd64 57 pkg: github.com/zerjioang/time32 58 59 BenchmarkNow/epoch-custom-12 356 3314537 ns/op 0.00 MB/s 1 B/op 0 allocs/op 60 BenchmarkNow/epoch-standard-go-12 345 3301546 ns/op 0.00 MB/s 1 B/op 0 allocs/op 61 BenchmarkNow/custom-12 37442151 33.07 ns/op 30.24 MB/s 0 B/op 0 allocs/op 62 BenchmarkNow/standard-go-time-12 38039060 32.16 ns/op 31.09 MB/s 0 B/op 0 allocs/op 63 BenchmarkNow/custom-ref-12 36875410 32.14 ns/op 31.11 MB/s 0 B/op 0 allocs/op 64 BenchmarkNow/standard-go-time-ref-12 36313864 32.98 ns/op 30.32 MB/s 0 B/op 0 allocs/op 65 BenchmarkNow/reuse-time-12 932550987 1.270 ns/op 787.44 MB/s 0 B/op 0 allocs/op 66 BenchmarkNow/reuse-unix-12 920748625 1.271 ns/op 786.86 MB/s 0 B/op 0 allocs/op 67 BenchmarkNow/reuse-unixnano-12 888746485 1.309 ns/op 764.14 MB/s 0 B/op 0 allocs/op 68 BenchmarkNow/reuse-time-unixnano-12 529924074 2.292 ns/op 436.22 MB/s 0 B/op 0 allocs/op 69 ``` 70 71 ## License 72 73 All rights reserved to **@zerjioang** under **GNU GPL v3** license 74 75 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 76 77 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 78 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 79 * Uses GPLv3 license described below 80 81 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.