-
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.
Merge pull request #2 from nascjoao/feat-0.3.0
Release 0.3.0
- Loading branch information
Showing
6 changed files
with
182 additions
and
67 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
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
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
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
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,63 @@ | ||
import { | ||
MILLISECONDS_IN_A_SECOND, | ||
SECONDS_IN_A_DAY, | ||
SECONDS_IN_A_MINUTE, | ||
SECONDS_IN_A_MONTH, | ||
SECONDS_IN_A_YEAR, | ||
SECONDS_IN_AN_HOUR, | ||
} from "../utils/timeEquivalences"; | ||
|
||
export default function parseTimeToSeconds(time: string | number): number { | ||
const timeCollection = []; | ||
let seconds = 0; | ||
|
||
if (typeof time === "string") { | ||
const regex = /(\d+\.?\d*)\s*([yMdhmsS])?/g; | ||
let match; | ||
const matches = []; | ||
|
||
while ((match = regex.exec(time)) !== null) { | ||
matches.push(match); | ||
} | ||
|
||
for (const match of matches) { | ||
timeCollection.push([match[1], match[2] || "s"]); | ||
} | ||
} | ||
|
||
if (timeCollection.length === 0) { | ||
timeCollection.push([time, "s"]); | ||
} | ||
|
||
for (const [_time, unit] of timeCollection) { | ||
switch (unit) { | ||
case "y": | ||
seconds += Number(_time) * SECONDS_IN_A_YEAR; | ||
break; | ||
case "M": | ||
seconds += Number(_time) * SECONDS_IN_A_MONTH; | ||
break; | ||
case "d": | ||
seconds += Number(_time) * SECONDS_IN_A_DAY; | ||
break; | ||
case "h": | ||
seconds += Number(_time) * SECONDS_IN_AN_HOUR; | ||
break; | ||
case "m": | ||
seconds += Number(_time) * SECONDS_IN_A_MINUTE; | ||
break; | ||
case "s": | ||
seconds += Number(_time); | ||
break; | ||
case "S": | ||
seconds += Number(_time) / MILLISECONDS_IN_A_SECOND; | ||
break; | ||
default: | ||
throw new Error( | ||
`Valid units: "y", "M", "d", "h", "m", "s" or "S".`, | ||
); | ||
} | ||
} | ||
|
||
return seconds; | ||
} |
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,21 @@ | ||
/** | ||
* The average year length in days. | ||
* | ||
* This is the average length of a year in days, taking into account leap years. | ||
* | ||
* A common year has 365 days, but a leap year has 366 days. We add an extra day every 4 years | ||
* to account for the extra 0.25 days per year. However, century years are not leap years unless | ||
* they are divisible by 400. For example, 2000 is a leap year, but 2100 is not. | ||
* | ||
* To do this calculation, we add 1/4 of a day every year, but subtract 1/100 of a day every year. | ||
* Also add 1/400 of a day every year. | ||
*/ | ||
export const AVERAGE_YEAR = 365 + 1 / 4 - 1 / 100 + 1 / 400; | ||
export const AVERAGE_MONTH = AVERAGE_YEAR / 12; | ||
export const HOURS_IN_A_DAY = 24; | ||
export const MILLISECONDS_IN_A_SECOND = 1000; | ||
export const SECONDS_IN_A_MINUTE = 60; | ||
export const SECONDS_IN_AN_HOUR = 60 * 60; | ||
export const SECONDS_IN_A_DAY = HOURS_IN_A_DAY * SECONDS_IN_AN_HOUR; | ||
export const SECONDS_IN_A_MONTH = AVERAGE_MONTH * SECONDS_IN_A_DAY; | ||
export const SECONDS_IN_A_YEAR = AVERAGE_YEAR * SECONDS_IN_A_DAY; |