When serializing a datetime to/from xml how do I make it use a custom time format?
How to serialize a custom formatted time to/from xml in Go?
920 Views Asked by matt.s At
2
There are 2 best solutions below
0
c4pone
On
It depends how your format looks like, but a good starting point should be the time library. http://golang.org/pkg/time/
layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
Related Questions in XML
- Impose component restriction to a series of parsys-CQ
- Wrong xml being inflated android
- Shorten the XSD
- Writing/Overwriting to specific XML file from ASP.NET code behind
- Magento custom block. Can't get block's file
- Layout not shifting up when keyboard is open
- CSV to XML XSLT: How to quote excape
- Getting deeply embedded XML element values
- Saving FileSystemInfo Array to File
- how to apply templates within xsl:for-each
- Spring - configure Jboss Intros for xml with java config?
- Problems with implementing custom actionbar android
- Can Apache Ant be told to cache its XML files?
- Is Log4j2 xml configuration case sensitive?
- How to get a specific node value in XML Pull Parser
Related Questions in GO
- How do I get all the attributes of an XML element using Go?
- Type cast custom types to base types
- Why are Revel optional func parameters in controller not working? CRUD code redundancy
- Streaming commands output progress
- single ampersand between 2 expressions
- golang goroutine use SSHAgent auth doesn't work well and throw some unexpect panic
- How do I do a literal *int64 in Go?
- Emulating `docker run` using the golang docker API
- How to print contents of channel without changing it
- Golang time zone parsing not returning the correct zone on ubuntu server
- Is os.File's Write() threadsafe?
- How to get the pointer of return value from function call?
- How do I represent an Optional String in Go?
- Fibonacci in Go using channels
- Boltdb-key-Value Data Store purely in Go
Related Questions in FORMATTING
- C++: Re-use line printed to console
- NSAttributeString - How to remove auto formatting for a date or time item?
- Bifurcate a string, removing the middle of the string instead of end
- Use DateTime format in a class but restrict time tokens
- Doesn't change the format of columns to date
- How do I convert a double into an n-character string using exponential notation?
- Java Integer Pyramid
- proper way to get nice string from exception
- How to edit text in columns?
- Simple and clean java float to string conversion
- Having trouble with list post-processing
- Formatting equations?
- Looking to export the content from a website into doc files
- SAPUI5 local scope object as formatter function parameter
- DataTable contains strings with Hashtags when imported from .xlsx instead of .xls
Related Questions in DATE-FORMATTING
- How do you combine strings and string variables in python
- add time (char(8)) to date column
- PHP Fatal error: Call to a member function format() on boolean
- date time to date (Y/m/d)
- How to avoid seconds in datetimepicker?
- Formatting dates in PostgreSQL
- sql query date formatting (Hours:Minutes)
- How to serialize a custom formatted time to/from xml in Go?
- use set language in a sql function
- Python: strptime() formatting
- Converting Date in M d, Y format leads to display date in other rows
- Choosing the wanted part of a date
- Excel/ How to convert number of date to
- Force localization of the allowedUnits in DateComponentsFormatter
- Formatting datetime in pandas columns as Quarters
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Just as you'd implement
json.Marshalerandjson.Unmarshalerfor doing this with JSON (there are many posts about that on StackOverflow and the internet); one way is to implement a custom time type that implementsencoding.TextMarshalerandencoding.TextUnmarshaler.Those interfaces are used by
encoding/xmlwhen encoding items (after first checking for the more specificxml.Marshalerorxml.Unmarshalerinterfaces, however those later ones have to do full XML encoding themselves).E.g. something like (full example on the Go Playground):
or
Either of those can be used in place of
time.Timeas part of a larger data structure used with xml (un)marshalling. E.g.:The difference in how these custom time types are defined changes how you use them with regular
time.Timevalues. E.g.