Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Fanello committed Mar 15, 2020
1 parent de75bbd commit b4ede1e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea

37 changes: 37 additions & 0 deletions README.md
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

13 changes: 13 additions & 0 deletions package.json
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"
}
13 changes: 13 additions & 0 deletions temporal-constants.js
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;

0 comments on commit b4ede1e

Please sign in to comment.