0

I tried to parsing string to time in go, here is what I've done:

dateTime := fmt.Sprintf("%s %s CST", dateValue, timeValue)
date, err := time.Parse("2006-1-2 150405 MST", dateTime)

I got the following error message:

parsing time "2012-4-9 174031 CST" as "2006-1-2 150405 MST": cannot parse "2012-4-9 174031 CST" as "2006"

From the error message, it shows the dateTime value I passed in is correct. I also tried to do the following, it works fine:

dateTime := "2012-4-9 174031 CST"
date, err := time.Parse("2006-1-2 150405 MST", dateTime)

This is bothering me for a few days. Could anybody help to point out where is the mistake? Thanks!

7
  • Please post dateValue and timeValue too. Commented Dec 31, 2017 at 8:48
  • These 2 values I read from a CSV file. I printed them out, there are “2012-4-9” and “174031”. Looks correct. Commented Dec 31, 2017 at 9:01
  • 1
    Try print them into a file and use a decent file editor or a hex viewer and see if there are unwanted leading chars. Commented Dec 31, 2017 at 9:09
  • This makes sense! Will try it. Thanks. Commented Dec 31, 2017 at 9:10
  • @ray Try comparing them fmt.Sprintf("%s %s CST", dateValue, timeValue) == "2012-4-9 174031 CST" and post the result. Commented Dec 31, 2017 at 10:24

1 Answer 1

1

Your values are not as you expect. I suspect you have whitespace in one or more values. Here is an example to play with which produces the same error (note the leading space on dateValue):

https://play.golang.org/p/UwKhjQs6Nig

dateValue := " 2012-4-9"
timeValue := "174031"
dateTime := fmt.Sprintf("%s %s CST", dateValue, timeValue)
date, err := time.Parse("2006-1-2 150405 MST", dateTime)

The first step if you ever hit a problem like this is to print your values before using them to verify they are exactly as you expect. If posting for help, also try to reproduce it on play.golang.org - often the process of doing this will help you finding the problem yourself.

The error message could be better, but I suspect what it is doing is taking the first part of the format (year), attempting to find it in the string value and failing, hence the report of not finding 2006.

Sign up to request clarification or add additional context in comments.

4 Comments

Instead of white space, I would suggest special characters like \x0 which will not be printed to screen. Try print the string into a file for more details.
Thanks for your reply. From the error message, it looks the string is correct (I tried to copy the string in error message and parse it directly without any error). The values are actually read from a csv file, I have difficulty to reproduce it in play.golang.org.
Can you post what you get with printf for those values? Can you check the csv file for the right encoding and for non printable characters or white space in a text editor? If you can isolate to one line of a csv it should be simpler.
@KennyGrant Thanks! Looks like it's the encoding issue. I copy and paste the same line to a new empty csv file, the error is gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.