DEV Community

Grant B
Grant B

Posted on

The battle for time

One of the hardships I'm sure many here have faced is how tricky it is to battle timezones even when you have good libs and even when you have smart people working on the problem.

When I started at my company we had diffuse time usages implemented differently in different situations. We had some good shared utilities we had made, but the testing was narrow and our under the hood moment library was something that had just lost support. It was time to switch to a new library and to be honest we ended up doing it poorly. This is a story about fixing that and what it took to be successful in doing so.

So first off we ended up picking dayjs because that is what one of our external libraries used and it was similar in terms of functionality to moment. It was well used and that was the decision. We simply had a dev convert it because moment had lost support.

The problem we encountered was a multitude of things. We hadn't properly tested every area so we were replacing blind. Moment to dayjs conversions may have had similar contracts but they weren't the same thing in practice or most importantly in output. This led to some pretty significant regression through the application.

After all was said and done we learned how disastrous this approach was. Not just to swap, but to have not had proper unit tests and how daunting a task converting to a new time library could be especially when you are given two seemingly similar inputs and getting two very distinct outputs.

The solution I decided upon was one that I think many would decide upon. For something as important as date handling you need a wrapper. But more importantly you should write it in such a way that you are not bound to the underlying library's input or output structure. In our case we used dayjs, but the rule was that we could neither return dayjs objects nor accept them as inputs and nothing tied strictly to their API. Lastly we needed unit tests for everything with good examples especially around formatting. We solidified date formats into an enumerable to ensure consistency. Took the manipulations we would need and codified them with verifiable outputs tested in Jest. Various timezone tests aligned with our biggest customers to ensure times would be correct regardless of area.

The result: we are now decoupled from a singular library that could lose support. We have the unit tests to back up our assessment that we converted correctly. And we know going forward that we will be consistent.

The lesson isn't which library you pick. It's that you should never let that decision matter.

Top comments (0)