Skip to content

columns defined as TIMESTAMP DEFAULT 0 scan to the year -4713 #289

@john-floren-gravwell

Description

@john-floren-gravwell

Consider the following code:

package main

import (
	"database/sql"
	_ "github.com/mattn/go-sqlite3"
	// _ "github.com/ncruces/go-sqlite3/driver"
	// _ "github.com/ncruces/go-sqlite3/embed"
	"log"
	"os"
	"path/filepath"
	"time"
)

func main() {
	dir, _ := os.MkdirTemp("", "foo")
	defer os.RemoveAll(dir)

	db, err := sql.Open("sqlite3", filepath.Join(dir, "test.db"))
	if err != nil {
		log.Fatal(err)
	}

	if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS test ( x TEXT, ts TIMESTAMP DEFAULT 0 );`); err != nil {
		log.Fatal(err)
	}

	if _, err := db.Exec(`INSERT INTO test ( x ) VALUES ( 'foo' );`); err != nil {
		log.Fatal(err)
	}

	row := db.QueryRow(`SELECT * from test;`)

	var val string
	var t time.Time
	if err := row.Scan(&val, &t); err != nil {
		log.Fatal(err)
	}

	log.Println(t)
}

When run as shown, using the github.com/mattn/go-sqlite3 driver, my TIMESTAMP DEFAULT 0 column comes back as the Unix epoch date (jan 1 1970).

If I switch around the imports to use ncruces/go-sqlite3 instead, the TIMESTAMP column comes back as -4713-11-24 12:00:00 +0000 UTC, which makes encoding/json very unhappy among other things.

Is there a way to tell the library to treat a 0 value as the epoch zero, the way mattn does?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions