Skip to content

Commit 0492e10

Browse files
committed
Initial commit
1 parent c3b50ce commit 0492e10

File tree

7 files changed

+209
-1
lines changed

7 files changed

+209
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
# datelata

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
# datelta
1+
# Datelta (Date + Delta = Datelta)
2+
3+
CLI tool to diff dates.
4+
5+
## Quick Start
6+
7+
- Install [Deno](https://docs.deno.com/runtime/manual/#install-deno)
8+
- Run `deno task install`
9+
- Run `datelta --start "Jan 1, 1990" | jq`
10+
11+
## Usage
12+
13+
Time between start date and end date.
14+
15+
```
16+
datelta --start "jan 10, 1990 12:30:01" --end "feb 2002" | jq
17+
```
18+
19+
```json
20+
{
21+
"years": 12,
22+
"months": 0,
23+
"days": 24,
24+
"hours": 11,
25+
"minutes": 29,
26+
"seconds": 59,
27+
"milliseconds": 0
28+
}
29+
```
30+
31+
Time since start date.
32+
33+
```
34+
datelta --start "mar 2006" | jq
35+
```
36+
37+
```json
38+
{
39+
"years": 17,
40+
"months": 7,
41+
"days": 28,
42+
"hours": 19,
43+
"minutes": 42,
44+
"seconds": 36,
45+
"milliseconds": 428
46+
}
47+
```
48+
49+
## Commands
50+
51+
| Task | Description |
52+
| ------------------- | -------------------------------- |
53+
| deno task install | Install datelta as an executable |
54+
| deno task uninstall | Uninstall datelta |

deno.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"tasks": {
3+
"dev": "deno run --watch src/main.ts",
4+
"install": "deno install -f --name datelta src/main.ts",
5+
"uninstall": "deno uninstall datelta"
6+
}
7+
}

deno.lock

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { toUTC, floorDivMod, floorMod } from "./util.ts";
2+
3+
export interface DateDiff {
4+
years: number;
5+
months: number;
6+
days: number;
7+
hours: number;
8+
minutes: number;
9+
seconds: number;
10+
milliseconds: number;
11+
}
12+
13+
export function dateDiff(start: Date, end: Date): DateDiff {
14+
const diffMilliseconds = toUTC(end) - toUTC(start);
15+
const millisecondsPerDay = 1000 * 60 * 60 * 24;
16+
const diffDays = diffMilliseconds / millisecondsPerDay;
17+
const [years, yearsRem] = floorDivMod(diffDays, 365);
18+
const [months, monthsRem] = floorDivMod(yearsRem, 30);
19+
const [days, daysRem] = floorMod(monthsRem);
20+
const [hours, hoursRem] = floorMod(daysRem * 24);
21+
const [minutes, minutesRem] = floorMod(hoursRem * 60);
22+
const [seconds, secondsRem] = floorMod(minutesRem * 60);
23+
const [milliseconds] = floorMod(secondsRem * 1000);
24+
return { years, months, days, hours, minutes, seconds, milliseconds };
25+
}

src/main.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { dateDiff } from "./lib.ts";
2+
import { parse } from "https://deno.land/std@0.202.0/flags/mod.ts";
3+
4+
const version = "v0.0.1";
5+
const help = `Usage: datelta [OPTIONS]
6+
7+
Options:
8+
--start Start date
9+
--end End date
10+
-h, --help Print help
11+
-V, --version Print version`;
12+
13+
function main() {
14+
const flags = parse(Deno.args, {
15+
boolean: ["help", "h", "version", "V"],
16+
string: ["start", "s", "end", "e"],
17+
default: { color: true },
18+
});
19+
20+
if (flags.help || flags.h) {
21+
return console.log(help);
22+
} else if (flags.version || flags.V) {
23+
return console.log(version);
24+
}
25+
26+
let startDate, endDate;
27+
28+
if (flags.start) {
29+
startDate = new Date(flags.start);
30+
endDate = new Date();
31+
}
32+
if (flags.start && flags.end) {
33+
startDate = new Date(flags.start);
34+
endDate = new Date(flags.end);
35+
}
36+
37+
if (!startDate) {
38+
console.error("Error: Start date is required.");
39+
} else if (startDate && endDate) {
40+
console.log(JSON.stringify(dateDiff(startDate, endDate)));
41+
}
42+
}
43+
44+
if (import.meta.main) {
45+
main();
46+
}

src/util.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export function toUTC(date: Date): number {
2+
return Date.UTC(
3+
date.getFullYear(),
4+
date.getMonth(),
5+
date.getDate(),
6+
date.getHours(),
7+
date.getMinutes(),
8+
date.getSeconds(),
9+
date.getMilliseconds()
10+
);
11+
}
12+
13+
export function floorDivMod(
14+
n: number,
15+
m: number
16+
): [quotient: number, remainder: number] {
17+
const q = Math.floor(n / m);
18+
const r = n % m;
19+
return [q, r];
20+
}
21+
22+
export function floorMod(n: number): [quotient: number, remainder: number] {
23+
if (n === 0) {
24+
return [0, 0];
25+
}
26+
const q = Math.floor(n);
27+
const r = n % q;
28+
return [q, r];
29+
}

0 commit comments

Comments
 (0)