Skip to content

The package provides an easy-to-use date type with some helpers.

License

Notifications You must be signed in to change notification settings

kaatinga/bublyk

Repository files navigation

Tests GitHub release MIT license codecov lint workflow help wanted

bublyk

The package introduces the Date type, specifically designed for instances where only the date in UTC location is required, without the need for time details. In comparison to the time.Time type, Date offers several advantages:

  • It consumes significantly less memory.
  • It eliminates the need for boilerplate code when working with dates.
  • It allows for straightforward comparisons using operators such as >, <, and others.

Additionally, it natively supports the pgx package. This means you can directly scan into the Date type and use Date as an argument in queries.

Usage

package main

import (
    "context"
    "fmt"
    "github.com/kaatinga/bublyk"
    "github.com/jackc/pgx/v4/pgxpool"
)

func main() {
    ctx := context.Background()
    pool, err := pgxpool.Connect(ctx, "postgres://postgres:postgres@localhost:5432/postgres")
    if err != nil {
        panic(err)
    }
    defer pool.Close()

    _, err = pool.Exec(ctx, "CREATE TABLE IF NOT EXISTS test (test_date DATE)")
    if err != nil {
        panic(err)
    }

    inputDate := bublyk.Now()
    var returnedDate bublyk.Date
    err = pool.QueryRow(ctx, "INSERT INTO test(test_date) VALUES($1) RETURNING test_date", inputDate).Scan(&returnedDate)
    if err != nil {
        panic(err)
    }

    fmt.Println(inputDate, returnedDate)
}

Will be happy to everyone who want to participate in the work on the bublyk package.