If you need an object that includes time, you’re probably going to need to convert it. Unit conversion is the cause of many bugs, some more disastrous than others.
When designing software, always make the right thing easy and the wrong thing hard.
(CNN) — NASA lost a $125 million Mars orbiter because a Lockheed Martin engineering team used English units of measurement while the agency’s team used the more conventional metric system for a key spacecraft operation
What if you never had to bother about which one it was and would get it right 100% of the time no matter if you needed milliseconds or hours?
Here’s how.
Let’s presume you’re measuring exercise duration.
What you get from using this pattern is, it’s impossible to mess up saving or getting information from that object.
It doesn’t matter what internal representation you choose. You’ll never go wrong in either setting or getting time out because you choose a unit each time you actually need it.
You’ll never go wrong in either setting or getting time out because you choose a unit each time you actually need it.
Caution Points
- Watch out for precision loss with nanos, consider your own usecase.
- Don’t use this to get days, weeks or, heaven forbid, years. Dates are more complicated than you think, use JodaTime for anything like that.
Ask me questions on Twitter or comment below!
原文链接:Are you using seconds or milliseconds? Don’t lose a Mars orbiter over it!
暂无评论内容