Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jsumners/go-rfc3339
A utility library to make dealing with RFC 3339 date-time and full-date representations easier.
https://github.com/jsumners/go-rfc3339
Last synced: about 1 month ago
JSON representation
A utility library to make dealing with RFC 3339 date-time and full-date representations easier.
- Host: GitHub
- URL: https://github.com/jsumners/go-rfc3339
- Owner: jsumners
- License: mit
- Created: 2023-04-04T16:47:36.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-06T15:43:47.000Z (7 months ago)
- Last Synced: 2024-10-14T21:57:16.703Z (about 1 month ago)
- Language: Go
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# RFC3339
A library that provides simplicity around parsing [RFC3339][3339] `date-time`
and `full-date` representations. The standard `time.Time` library is a bit
rough in that formats must be known ahead of time, and supplied when serializing
to strings. The types provided by this library extend the standard `time.Time`
type.The `date-time` and `full-date` types implement the [Scanner][scanner] and
[Valuer][valuer] interfaces so that they can be stored as strings in a
database.[3339]: https://www.rfc-editor.org/rfc/rfc3339
[scanner]: https://pkg.go.dev/database/sql#Scanner
[valuer]: https://pkg.go.dev/database/sql/driver#Valuer## Install
```sh
$ go get github.com/jsumners/go-rfc3339
```## Example
```go
package mainimport (
"fmt"
"github.com/jsumners/go-rfc3339"
)func main() {
dt, err := rfc3339.NewDateTimeFromString("2023-04-04T12:45:00-04:00")
if err != nil {
panic(err)
}fmt.Printf("timestamp: %s", dt.ToString())
// Output:
// timestamp: 2023-04-04T12:45:00-04:00
}
```## Example (sql)
```go
package mainimport (
"database/sql"
"fmt""github.com/jsumners/go-rfc3339"
_ "modernc.org/sqlite"
)func main() {
db, _ := sql.Open("sqlite", "./db.sqlite")
defer db.Close()dt, _ := rfc3339.NewDateTimeFromString("2023-09-28T08:30:00-04:00")
db.Exec(
"insert into example (date_time) values (@date_time)",
sql.Named("date_time", dt),
)rows, _ := db.Query("select date_time from values")
for rows.Next() {
var dt rfc3339.DateTime
rows.Scan(&dt)
fmt.Printf("dt = %+v\n", dt)
}
}
```