-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Fanello
committed
Mar 15, 2020
1 parent
de75bbd
commit b4ede1e
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Temporal Constants | ||
Constants to compute with time units without magic numbers. | ||
|
||
## Why? | ||
|
||
How often do you have to do some calculations to convert seconds to milliseconds, hours to days, | ||
or any number of others? Does everyone looking at your code understand why you are multipling | ||
a number of days by 86400? | ||
|
||
We use these constant values in computations all the time in code, | ||
yet we also know that magic numbers are to be avoided: | ||
|
||
> Unique values with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants | ||
> -- [Wikipedia](https://en.wikipedia.org/wiki/Magic_number_(programming)) | ||
This tiny library defines those constants for you, so now instead of: | ||
|
||
`Math.round(ms / 60000)`, you can write `Math.round(ms / MillisecondsInMinute)` | ||
|
||
Or in place of: `setTimer(() => runMyFunction(), 24*60*60*1000)` | ||
|
||
you get the clearer (and faster): `setTimer(() => runMyFunction(), MillisecondsInDay)`. | ||
|
||
## FAQ | ||
|
||
*Q:* Why should I introduce another dependency for a few constants that I can define myself? | ||
|
||
*A:* Have you defined them? Did you make any mistakes computing them? | ||
|
||
If you don't want a dependency, feel free to copy the lines into your own source. | ||
In this case, the times they are **not** a-changin'. | ||
|
||
## See also.... | ||
|
||
- [temporal-types](https://github.com/kernwig/temporal-types) - Typescript types for time units | ||
- [js-joda](https://js-joda.github.io/js-joda/) - Complete library for date and time representation and manipulation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "temporal-constants", | ||
"version": "1.0.0", | ||
"description": "Constants to compute with time units without magic numbers.", | ||
"main": "temporal-constants.js", | ||
"keywords": [ | ||
"time", | ||
"constant", | ||
"temporal" | ||
], | ||
"author": "Adam Fanello", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Covert time units without magic numbers | ||
export const MillisecondsInSecond = 1000; | ||
export const MillisecondsInMinute = 60000; | ||
export const MillisecondsInHour = 3600000; | ||
export const MillisecondsInDay = 86400000; | ||
export const SecondsInMinute = 60; | ||
export const SecondsInHour = 3600; | ||
export const SecondsInDay = 86400; | ||
export const MinutesInHour = 60; | ||
export const MinutesInDay = 1440; | ||
export const HoursInDay = 24; | ||
export const DaysInWeek = 7; | ||
export const MonthsInYear = 12; |