parsing - time.Parse with custom layout -


i'm trying parse string pattern "4-jan-12 9:30:14" time.time.

tried time.parse("2-jan-06 15:04:05", inputstring) , many others cannot working.

i've read http://golang.org/pkg/time/#parse , https://gobyexample.com/time-formatting-parsing seems there aren't examples this.

thanks!

edit: full code:

type customtime time.time  func (t *customtime) unmarshaljson(b []byte) error {     auxtime, err := time.parse("2-jan-06 15:04:05", string(b))     *t = customtime(auxtime)     return err } 

parsing time ""10-jan-12 11:20:41"" "2-jan-06 15:04:05": cannot parse ""24-jan-15 10:27:44"" "2"

don't know did wrong (should post code), simple function call:

s := "4-jan-12 9:30:14" t, err := time.parse("2-jan-06 15:04:05", s) fmt.println(t, err) 

outputs:

2012-01-04 09:30:14 +0000 utc <nil> 

try on go playground.

note time.parse() returns 2 values: parsed time.time value (if parsing succeeds) , optional error value (if parsing fails).

see following example intentionally specify wrong input string:

s := "34-jan-12 9:30:14"  if t, err := time.parse("2-jan-06 15:04:05", s); err == nil {     fmt.println("success:", t) } else {     fmt.println("failure:", err) } 

output:

failure: parsing time "34-jan-12 9:30:14": day out of range 

try on go playground.

edit:

now posted code , error message, problem input string contains leading , trailing quotation mark!

remove leading , trailing quotation mark , work. case:

s := `"4-jan-12 9:30:14"`  s = s[1 : len(s)-1] if t, err := time.parse("2-jan-06 15:04:05", s); err == nil {     fmt.println("success:", t) } else {     fmt.println("failure:", err) } 

output (try on go playground):

success: 2012-01-04 09:30:14 +0000 utc 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -