Convenience functions to make your life easier when using with Golang's time
package.
In your Golang project, please run:
go get github.com/bartmika/timekit
All documentation can be found here.
The following usage section will show a few interesting solutions that can be solved by this library.
import (
"fmt"
"github.com/bartmika/timekit"
)
startOfYearDate := timekit.FirstDayOfThisYear(time.Now)
fmt.Println(startOfYearDate)
If you have interest in finding out more functions for getting different date/times then checkout timekit.go file.
In your browser console, try writing this:
// EXAMPLE JAVASCRIPT CODE
var dt = new Date()
console.log(dt.getTime()) // 1643082322380
console.log(dt) // Mon Jan 24 2022 22:45:22 GMT-0500 (Eastern Standard Time)
Then try this in your go source file:
import (
"fmt"
"github.com/bartmika/timekit"
)
jsTime := int64(1643082322380)
goTime := ParseJavaScriptTime(jsTime)
fmt.Println(goTime)
import (
"fmt"
"github.com/bartmika/timekit"
)
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
dts := RangeFromTimeStepper(start, end, 0, 0, 1, 0, 0, 0) // Step by day.
fmt.Println(dts) // Output:
// Jan 7th 2022
// Jan 8th 2022
// Jan 9th 2022
// Jan 10th 2022
If you have interest in finding out more range functions then checkout range.go and timestepper.go files.
import (
"fmt"
"github.com/bartmika/timekit"
)
loc := time.UTC // closure can be used if necessary
start := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th 2022
end := time.Date(2022, 1, 10, 1, 0, 0, 0, loc) // Jan 10th 2022
ts := NewTimeStepper(start, end, 0, 0, 1, 0, 0, 0)
var actual time.Time
running := true
for running {
// Get the value we are on in the timestepper.
actual = ts.Get()
log.Println(actual) // For debugging purposes only.
// Run our timestepper to get our next value.
ts.Next()
running = ts.Done() == false
}
If you have interest in finding out iterating between two date/times then checkout timestepper.go file.
Found a bug? Want a feature to improve your developer experience when dealing with the time
package? Please create an issue.
Made with ❤️ by Bartlomiej Mika.
The project is licensed under the ISC License.
Resource used:
- Stubbing Time.Now() in golang was a tremendous help in getting me to understand how to unit test with the
time.Time
package. - dannav/hhmmss package is used for the Golang
hh:mm:ss
string to duration conversation method. - relvacode/iso8601 package is used for dealing with ISO8601 formatted strings.