You are browsing as a guest. Sign up (or log in) to start making projects!

1h 24m 43s logged

Time formatting doesn’t suck anymore! I made the implementation way better so I could make it support stuff even past weeks, like months and years.

The new implementation added around 60 lines, but it’s for a good cause. The previous implementation used a match statement with duplicated logic that only supported seconds, minutes, and hours, using a pluralize function and inline if-statements everywhere. If it were sent to any rational code-reviewer, they would likely have a heart attack.

Now it’s a bit more complex but less complicated. You have TimeUnits which format the unit themselves and TimeParts which format a value + a unit. Then, just use some math to build a Vec and format it into a string.

Boom! Beautiful, well-formed time formatting. To soak in the new beauty, I added days n weeks. I just had to add 14 lines! The previous one would have probably needed a bunch of stupid magic numbers and weird chains everywhere to accomplish the same thing.

Okay, I’m done using your time on reading a way-too-long write-up about fixing a crappy implementation that shouldn’t have been there in the first place.

I’m gonna ship right after posting this. If you get it, make sure to give it 10 stars in everything 😉. Haha, jk. Well, not really. Cya later.

0
10

Comments 0

No comments yet. Be the first!