Skip to content

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

Closed

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?

Activity

  1. Repository owner locked and limited conversation to collaborators on Jul 9, 2025
  2. converted this issue into a discussion #290 on Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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