Skip to content

Commit 58abb66

Browse files
MarekMarek
Marek
authored and
Marek
committed
Repair commit
1 parent be956aa commit 58abb66

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed

Justfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
default:
2+
@just --list --unsorted
3+
4+
host := `uname -a`
5+
6+
# used for building pcre...
7+
export CC := "musl-gcc -static"
8+
9+
project := "datediff"
10+
main_file := project + ".nim"
11+
main_exec := project
12+
musl_exec := project + "_x86_64-unknown-linux-musl"
13+
14+
alias b := build
15+
alias s := shasum
16+
alias r := release
17+
18+
# build main
19+
# Garbage Collector and Memory management https://nim-lang.org/docs/mm.html
20+
# --mm:markAndSweep \
21+
# --mm:arc \
22+
build:
23+
nim --gcc.exe:musl-gcc \
24+
--gcc.linkerexe:musl-gcc \
25+
--mm:orc \
26+
-d:release \
27+
--opt:speed \
28+
--passL:-static \
29+
--dynlibOverrideAll \
30+
compile {{main_file}}
31+
32+
# run a specific test
33+
# test TEST: build
34+
# ./test --test {{TEST}}
35+
36+
shasum:
37+
sha256sum {{main_exec}} >{{main_exec}}.sha256
38+
39+
release tag:
40+
gh release create {{tag}}

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,106 @@
11
# datediff
2+
<<<<<<< HEAD
23
Find differents between two dates
4+
=======
5+
6+
Program written in Nim to find difference between `DATE1` and `DATE2`.
7+
8+
Check releases with `musl`.
9+
10+
## `datediff --help`
11+
12+
```
13+
datediff - Show dates difference in specific format
14+
15+
+ optionaly select a timezone with TZ environment variable
16+
Use tzselect(8) to find your TZ zone
17+
18+
USAGE:
19+
datediff [options] [--] <DATE1> <DATE2>
20+
datediff --help
21+
22+
OPTIONS:
23+
-f --format=FORMAT Format for date [default: yyyy-MM-dd]
24+
-v --verbose Show what is going on
25+
--help Show this help
26+
-s --seconds Output in: Seconds
27+
-m --minutes Output in: Minutes
28+
-h --hours Output in: Hours
29+
-d --days Output in: Days (This is default)
30+
-w --weeks Output in: Weeks
31+
32+
FORMATTING:
33+
FORMAT is parsing DATE inputs as follows:
34+
35+
d Numeric value representing the day of the month, it will be either one or two digits long.
36+
1/04/2012 -> 1
37+
21/04/2012 -> 21
38+
dd Same as above, but is always two digits.
39+
1/04/2012 -> 01
40+
21/04/2012 -> 21
41+
h The hours in one digit if possible. Ranging from 1-12.
42+
5pm -> 5
43+
2am -> 2
44+
hh The hours in two digits always. If the hour is one digit, 0 is prepended.
45+
5pm -> 05
46+
11am -> 11
47+
H The hours in one digit if possible, ranging from 0-23.
48+
5pm -> 17
49+
2am -> 2
50+
HH The hours in two digits always. 0 is prepended if the hour is one digit.
51+
5pm -> 17
52+
2am -> 02
53+
m The minutes in one digit if possible.
54+
5:30 -> 30
55+
2:01 -> 1
56+
mm Same as above but always two digits, 0 is prepended if the minute is one digit.
57+
5:30 -> 30
58+
2:01 -> 01
59+
M The month in one digit if possible.
60+
September -> 9
61+
December -> 12
62+
MM The month in two digits always. 0 is prepended if the month value is one digit.
63+
September -> 09
64+
December -> 12
65+
s Seconds as one digit if possible.
66+
00:00:06 -> 6
67+
ss Same as above but always two digits. 0 is prepended if the second is one digit.
68+
00:00:06 -> 06
69+
yy The last two digits of the year. When parsing, the current century is assumed.
70+
2012 AD -> 12
71+
yyyy The year, padded to at least four digits. Is always positive, even when the year is BC. When the year is more than four digits, '+' is prepended.
72+
2012 AD -> 2012
73+
24 AD -> 0024
74+
24 BC -> 00024
75+
12345 AD -> +12345
76+
YYYY The year without any padding. Is always positive, even when the year is BC.
77+
2012 AD -> 2012
78+
24 AD -> 24
79+
24 BC -> 24
80+
12345 AD -> 12345
81+
82+
More patterns can be found here:
83+
https://nim-lang.org/docs/times.html#parsing-and-formatting-dates
84+
85+
EXAMPLES:
86+
datediff 2000-01-01 2001-01-01
87+
# 366
88+
datediff -s -f "yyyy" 2022 2025
89+
# 94694400
90+
TZ='America/Anchorage' datediff -h -f "yyyy-MM-dd-HH" 2022-03-13-01 2022-03-13-04
91+
# 2 # Because of DST change
92+
```
93+
94+
### Build
95+
96+
Use `just` as modern alternative to `make`: https://github.com/casey/just or manually run commands in `Justfile`
97+
98+
```
99+
just build
100+
```
101+
102+
### Alternative
103+
104+
There are greater tools available:
105+
https://github.com/hroptatyr/dateutils
106+
>>>>>>> 7b9e4d9 (First commit)

datediff.nim

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import std/[ times, strutils]
2+
import docopt
3+
4+
proc perr(s: string, i: int) =
5+
writeLine(stderr, "ERROR: " & s)
6+
flushFile(stderr)
7+
quit i
8+
9+
proc pinf(s: string, verbose: bool) =
10+
if verbose:
11+
writeLine(stderr, "INFO: " & s)
12+
flushFile(stderr)
13+
14+
let doc = """
15+
datediff - Show dates difference in specific format
16+
17+
+ optionaly select a timezone with TZ environment variable
18+
Use tzselect(8) to find your TZ zone
19+
20+
USAGE:
21+
datediff [options] [--] <DATE1> <DATE2>
22+
datediff --help
23+
24+
OPTIONS:
25+
-f --format=FORMAT Format for date [default: yyyy-MM-dd]
26+
-v --verbose Show what is going on
27+
--help Show this help
28+
-s --seconds Output in: Seconds
29+
-m --minutes Output in: Minutes
30+
-h --hours Output in: Hours
31+
-d --days Output in: Days (This is default)
32+
-w --weeks Output in: Weeks
33+
34+
FORMATTING:
35+
FORMAT is parsing DATE inputs as follows:
36+
37+
d Numeric value representing the day of the month, it will be either one or two digits long.
38+
1/04/2012 -> 1
39+
21/04/2012 -> 21
40+
dd Same as above, but is always two digits.
41+
1/04/2012 -> 01
42+
21/04/2012 -> 21
43+
h The hours in one digit if possible. Ranging from 1-12.
44+
5pm -> 5
45+
2am -> 2
46+
hh The hours in two digits always. If the hour is one digit, 0 is prepended.
47+
5pm -> 05
48+
11am -> 11
49+
H The hours in one digit if possible, ranging from 0-23.
50+
5pm -> 17
51+
2am -> 2
52+
HH The hours in two digits always. 0 is prepended if the hour is one digit.
53+
5pm -> 17
54+
2am -> 02
55+
m The minutes in one digit if possible.
56+
5:30 -> 30
57+
2:01 -> 1
58+
mm Same as above but always two digits, 0 is prepended if the minute is one digit.
59+
5:30 -> 30
60+
2:01 -> 01
61+
M The month in one digit if possible.
62+
September -> 9
63+
December -> 12
64+
MM The month in two digits always. 0 is prepended if the month value is one digit.
65+
September -> 09
66+
December -> 12
67+
s Seconds as one digit if possible.
68+
00:00:06 -> 6
69+
ss Same as above but always two digits. 0 is prepended if the second is one digit.
70+
00:00:06 -> 06
71+
yy The last two digits of the year. When parsing, the current century is assumed.
72+
2012 AD -> 12
73+
yyyy The year, padded to at least four digits. Is always positive, even when the year is BC. When the year is more than four digits, '+' is prepended.
74+
2012 AD -> 2012
75+
24 AD -> 0024
76+
24 BC -> 00024
77+
12345 AD -> +12345
78+
YYYY The year without any padding. Is always positive, even when the year is BC.
79+
2012 AD -> 2012
80+
24 AD -> 24
81+
24 BC -> 24
82+
12345 AD -> 12345
83+
84+
More patterns can be found here:
85+
https://nim-lang.org/docs/times.html#parsing-and-formatting-dates
86+
87+
EXAMPLES:
88+
datediff 2000-01-01 2001-01-01
89+
# 366
90+
datediff -s -f "yyyy" 2022 2025
91+
# 94694400
92+
TZ='America/Anchorage' datediff -h -f "yyyy-MM-dd-HH" 2022-03-13-01 2022-03-13-04
93+
# 2 # Because of DST change
94+
""".dedent()
95+
96+
let args = docopt(doc, version = "0.1.0", optionsFirst = true)
97+
98+
var
99+
verboseBool = false
100+
timeFormat : TimeFormat
101+
dateTime1 : DateTime
102+
dateTime2 : DateTime
103+
104+
if not args["<DATE1>"]:
105+
perr("Missing input <DATE1>", 1)
106+
if not args["<DATE2>"]:
107+
perr("Missing input <DATE2>", 1)
108+
109+
if args["--verbose"]:
110+
verboseBool = true
111+
pinf("Verbose output", verboseBool)
112+
113+
if not args["--format"]:
114+
perr("Did not found FORMAT. See --help", 2)
115+
else:
116+
pinf("Specified format is: " & $args["--format"], verboseBool)
117+
try:
118+
timeFormat = initTimeFormat($args["--format"])
119+
except:
120+
perr(getCurrentExceptionMsg(), 3)
121+
122+
pinf("Parsing DATE1: " & $args["<DATE1>"], verboseBool)
123+
try:
124+
dateTime1 = parse($args["<DATE1>"], timeFormat)
125+
except:
126+
perr(getCurrentExceptionMsg(), 3)
127+
128+
pinf("Parsing DATE2: " & $args["<DATE2>"], verboseBool)
129+
try:
130+
dateTime2 = parse($args["<DATE2>"], timeFormat)
131+
except:
132+
perr(getCurrentExceptionMsg(), 3)
133+
134+
pinf("Duration...", verboseBool)
135+
let dur = abs(dateTime1 - dateTime2)
136+
137+
# Duration can be: inWeeks inDays inHours inMinutes inSeconds inMilliseconds inMicroseconds inNanoseconds
138+
139+
if args["--seconds"]:
140+
echo dur.inSeconds
141+
elif args["--minutes"]:
142+
echo dur.inMinutes
143+
elif args["--hours"]:
144+
echo dur.inHours
145+
elif args["--weeks"]:
146+
echo dur.inWeeks
147+
else:
148+
echo dur.inDays

0 commit comments

Comments
 (0)