Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values of millisecond lost when using Mongo Driver to read time and Unmarshalling int into time.Time #37293

Closed
Abhaykumar1 opened this issue Feb 19, 2020 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@Abhaykumar1
Copy link

Version : go 1.13
Mongo Driver Version : go.mongodb.org/mongo-driver v1.2.1

I'm fetching the Date stored in MongoDB. The value stored in DB is
reservedDate: 2020-02-19T07:11:23.890+00:00

After Unmarshalling the response into time.Time field the value becomes
"reservedDate": "2020-02-19T07:11:23.89Z"

890 MilliSecond become 89, similarlly 100 becomes 1. The trailing Zeros are getting truncated.

@mvdan
Copy link
Member

mvdan commented Feb 19, 2020

Why are you filing this issue here? You should probably file it with the mongo driver you're using.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 19, 2020
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@Abhaykumar1
Copy link
Author

Version : go 1.13

When the defined time has trailing zero after the millisecond
trailingZero = "2020-02-19T07:11:23.890+00:00"
noTrailingZero = "2020-02-19T07:11:23.891+00:00"

GO removes the trailing zero. The time becomes

2020-02-19 07:11:23.89 +0000 UTC
2020-02-19 07:11:23.891 +0000 UTC

This is an issue when other consuming application tries to consume this by defining a date formatter to get time from String value. The formatter does the strict matching.

for example in java
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

It will throw an exception while parsing 2020-02-19 07:11:23.89 +0000 UTC while will perfectly work for 2020-02-19 07:11:23.891 +0000 UTC

Code To Replicate
const (
trailingZero = "2020-02-19T07:11:23.890+00:00"
noTrailingZero = "2020-02-19T07:11:23.891+00:00"
)

func main() {
parseAndPrint(trailingZero)
parseAndPrint(noTrailingZero)
}

func parseAndPrint(timeString string) {
parsed, err := time.Parse(time.RFC3339, timeString)
if err != nil {
panic(err)
}
fmt.Println(parsed)
}

@golang golang locked and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants