Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mchirico/date
Go Dateparse
https://github.com/mchirico/date
dateparser
Last synced: about 2 months ago
JSON representation
Go Dateparse
- Host: GitHub
- URL: https://github.com/mchirico/date
- Owner: mchirico
- License: mit
- Created: 2018-09-08T20:15:59.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2022-05-30T12:33:52.000Z (over 2 years ago)
- Last Synced: 2024-04-21T02:18:25.466Z (9 months ago)
- Topics: dateparser
- Language: Go
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go](https://github.com/mchirico/date/actions/workflows/go.yml/badge.svg?branch=develop)](https://github.com/mchirico/date/actions/workflows/go.yml)
# date
Go Dateparse## Install
```bash
go get -u github.com/mchirico/date/parse```
### Usage
[playground](https://go.dev/play/p/K0rBJrvVjRn)
```gopackage main
import (
"fmt"
"github.com/mchirico/date/parse"
)func main() {
s := "Sep 8 13:24:18 "
tt, err := parse.DateTimeParse(s).NewYork()
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Printf("tt: %v\n", tt)
// tt: 2018-09-08 13:24:18 -0400 EDT}
```
Here's another example:
```go
package mainimport (
"fmt"
"github.com/mchirico/date/parse"
)func main() {
s := "1554934858234"
tt, err := parse.DateTimeParse(s).NewYork()
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 22:20:58.234 -0400 EDT}
```
Works with timezones
[playground](https://go.dev/play/p/6uPD1gedJNh)
```go
package mainimport (
"fmt"
"github.com/mchirico/date/parse"
)func main() {
s := "1554934858234"
tt, err := parse.DateTimeParse(s).TimeIn("America/New_York")
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 18:20:58.234 -0400 EDTtt, _ = parse.DateTimeParse(s).TimeIn("America/Chicago")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 17:20:58.234 -0500 CDTtt, _ = parse.DateTimeParse(s).TimeIn("America/Detroit")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 18:20:58.234 -0400 EDTtt, _ = parse.DateTimeParse(s).TimeIn("America/Denver")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 16:20:58.234 -0600 MDTtt, _ = parse.DateTimeParse(s).TimeIn("America/Los_Angeles")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 15:20:58.234 -0700 PDTtt, _ = parse.DateTimeParse(s).TimeIn("UTC")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-10 22:20:58.234 +0000 UTCtt, _ = parse.DateTimeParse(s).TimeIn("Asia/Shanghai")
fmt.Printf("tt: %v\n", tt)
// tt: 2019-04-11 06:20:58.234 +0800 CST}
```
Example of rounding down.
```go
package mainimport (
"fmt"
"github.com/mchirico/date/parse"
"time"
)func main() {
s := "Thu Mar 21 19:07:52 UTC 2019"
tt, err := parse.DateTimeParse(s).TimeIn("America/New_York")fmt.Printf("tt: %s err: %v\n", tt, err)
// tt: 2019-03-21 15:07:52 -0400 EDT err:fmt.Printf("tt: %s err: %v\n", tt.Round(60*time.Minute), err)
// tt: 2019-03-21 15:00:00 -0400 EDT err:}
```