Skip to content

Inconsistent time conversion #165

@mimol91

Description

@mimol91

Look on this small go code

package main

import (
	"fmt"
	"time"

	"github.com/mitchellh/mapstructure"
)

type Record struct {
	Date   time.Time    `json:"date"`
	DateP  *time.Time   `json:"datep"`
	Dates  []time.Time  `json:"dates"`
	DatesP []*time.Time `json:"datesp"`
}

func main() {
	now := time.Now()
	record := Record{
		Date:   now,
		DateP:  &now,
		Dates:  []time.Time{now},
		DatesP: []*time.Time{&now},
	}

	var result map[string]any
	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		Result:  &result,
		TagName: "json",
	})
	if err != nil {
		panic(err)
	}
	if err := decoder.Decode(record); err != nil {
		panic(err)
	}
	fmt.Println(result)
}

Result map has all fields except (date) correctly set.

map[
date:map[] 
datep:2026-01-12 15:07:35.44162 +0400 +04 m=+0.000086376 
dates:[2026-01-12 15:07:35.44162 +0400 +04 m=+0.000086376] 
datesp:[2026-01-12 15:07:35.44162 +0400 +04 m=+0.000086376]
]

It seems to be counter intuitive

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions