Tuesday, November 10, 2020

[Video] Hugo date formatting bug - Oh it was a bad assumption

Mistakes are with humans. Most of those maybe because of ignorance, lack of experience, and there are many other reasons. But programmers make mistakes because of one more category called lack of reading documentation. They just try it and builds assumptions. When things are not getting working they think there is a bug. It could have occurred because of simple assumptions made without reading the documentation.

This is a story of such a mistake.

I got obsessed with Hugo especially with its templating. The templating is easy to learn and there are a lot of examples on the internet. If we take an example of date formatting, we can easily get help saying we can use "2006-Jan-2" as a format string to format the date. If we are from the traditional formatting world of  "YYYY-MMM-DD" we feel this format is innovative and assumes that the year 2006 is just a year. Hugo is intelligent enough to understand any year in the format string.

With that ASSUMPTION if we give "2020-Jan-2' things will not work and start thinking as an error in Hugo. I went one more step ahead and did a video on the workaround saying use 2006 only. Below it is embedded.

Later publishing the video and when I get a chance I read the Hugo code as well as documentation. That opened my eyes. The documentation about the same is below.

https://gohugo.io/functions/format/#gos-layout-string

The above works for 'Date' data type only. To format string use the function .dateFormat .The root goes to the Go programming language that has this specific date to format dates.

Moral of the story is never goes with assumptions especially before deciding something is not working as expected.

No comments: