Golang parsing the isodate type in MongoDB

Source: Internet
Author: User
Tags cst time
This is a creation in Article, where the information may have evolved or changed.

You can use time in Golang. The time data type to hold isodate times in MongoDB.

G

Type listen to the model listen to the struct listen {Listen to hear uploaddate listen to Time.time hear ' Bson: "uploaddate" '}m listen: = Listen to model{}if listen to err listen: = Listen to C. Find (nil). Select ({"_id": Listen to 0, listen to "uploaddate": Listen 1}). One (&m); listen to err hear! = Listen to nil Listen {Listen and listen to the FMT. Printf ("Failed listen to find listen to the date, listen to the error%s\n", listen to err) listen to listen to the OS. Exit (1)}fmt. Printf ("%+v\n", listen m)

From the output you can see that the time format of the Golang output is the CST time zone, Golang in the process of converting the ISO time to CST times, from the time panel is also more than 8 hours in MongoDB, this is normal.


So Golang do you need to convert the timestamp when you do the insert operation or time comparison operation? The answer is no need, look at the insert example.

Reinsert a record with the time field of the current period, which can be passed in Golang. Now to get the current time, view the output to see the time format as CST.

Now listen: = Listen to time. Now () Fmt. Printf ("%+v\n", listen now)//listen to output: Listen to the 2016-05-12 14:34:00.998011694 listen to the +0800 listen to csterr listen = listen to C. Insert (model{time: Listen now}) if listen to err listen! = Listen Nil Listen {Listen to listen to panic (err)}

You can see the automatic conversion of time stored into MongoDB for ISO time, 8 hours less time. A summary is that the time interaction in Golang and MongoDB does not need to be considered for additional things because the drivers are converted.


Sometimes we will take time. Time is stored as a string, so you need to convert it when you interact with MongoDB. Time format

TimeString Listen: = Listen to "2016-05-12 listen to 14:34:00.998011694 +0800 listen to CST" T, listen to err listen: = Listen to time. Parse ("2006-01-02 Listen 15:04:05.999999999-0700 Listen to MST", listen to timestring) If listen to err hear! = Listen to nil Listen {Listen to listen to panic (err)}fmt. Printf ("%+v\n", listen T)

What's hard to understand in the code is time. The first argument to parse, which is actually a definition in Golang, looks at time in detail. The source of the time.string () will understand.

This article from the "Linux related" blog, reproduced please contact the author!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.