Skip to content

Commit b611da8

Browse files
committed
cleanup, add pretty-print
1 parent 907ab91 commit b611da8

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

crates/nu-std/lib/dt.nu

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def borrow-year [from: record, current: record] {
88
}
99

1010
def leap-year-days [year] {
11-
# ($year mod 4) == 0 and (($year mod 100 != 0) or ($year mod 400 == 0))
1211
if $year mod 400 == 0 {
1312
29
1413
} else if $year mod 4 == 0 and $year mod 100 != 0 {
@@ -34,7 +33,6 @@ def borrow-month [from: record, current: record] {
3433
}
3534
} else {
3635
# oh February
37-
# let num_days_feb = if $current.from mod 400 == 0 { 29 } else if $current.from mod 4 == 0 and $current.from mod 100 != 0 { 29 } else { 28 }
3836
let num_days_feb = (leap-year-days $current.year)
3937
$current.day = $current.day + $num_days_feb
4038
$current.month = $current.month - 1
@@ -152,10 +150,77 @@ export def datetime-diff [from: datetime, to: datetime] {
152150
}
153151

154152
$result.millisecond = ($result.nanosecond / 1_000_000 | into int) # don't want a decimal
155-
$result.microsecond = (($result.nanosecond mod 1_000_000) / 1_000)
156-
$result.nanosecond = ($result.nanosecond mod 1_000)
153+
$result.microsecond = (($result.nanosecond mod 1_000_000) / 1_000 | into int)
154+
$result.nanosecond = ($result.nanosecond mod 1_000 | into int)
157155

158156
$result
159157
}
160158

159+
export def pretty-print-duration [dur: duration] {
160+
mut result = ""
161+
if $dur.year > 0 {
162+
if $dur.year > 1 {
163+
$result = $"($dur.year)yrs "
164+
} else {
165+
$result = $"($dur.year)yr "
166+
}
167+
}
168+
if $dur.month > 0 {
169+
if $dur.month > 1 {
170+
$result = $"($result)($dur.month)months "
171+
} else {
172+
$result = $"($result)($dur.month)month "
173+
}
174+
}
175+
if $dur.day > 0 {
176+
if $dur.day > 1 {
177+
$result = $"($result)($dur.day)days "
178+
} else {
179+
$result = $"($result)($dur.day)day "
180+
}
181+
}
182+
if $dur.hour > 0 {
183+
if $dur.hour > 1 {
184+
$result = $"($result)($dur.hour)hrs "
185+
} else {
186+
$result = $"($result)($dur.hour)hr "
187+
}
188+
}
189+
if $dur.minute > 0 {
190+
if $dur.minute > 1 {
191+
$result = $"($result)($dur.minute)mins "
192+
} else {
193+
$result = $"($result)($dur.minute)min "
194+
}
195+
}
196+
if $dur.second > 0 {
197+
if $dur.second > 1 {
198+
$result = $"($result)($dur.second)secs "
199+
} else {
200+
$result = $"($result)($dur.second)sec "
201+
}
202+
}
203+
if $dur.millisecond > 0 {
204+
if $dur.millisecond > 1 {
205+
$result = $"($result)($dur.millisecond)ms "
206+
} else {
207+
$result = $"($result)($dur.millisecond)ms "
208+
}
209+
}
210+
if $dur.microsecond > 0 {
211+
if $dur.microsecond > 1 {
212+
$result = $"($result)($dur.microsecond)µs "
213+
} else {
214+
$result = $"($result)($dur.microsecond)µs "
215+
}
216+
}
217+
if $dur.nanosecond > 0 {
218+
if $dur.nanosecond > 1 {
219+
$result = $"($result)($dur.nanosecond)ns "
220+
} else {
221+
$result = $"($result)($dur.nanosecond)ns "
222+
}
223+
}
161224

225+
$result
226+
}

crates/nu-std/lib/mod.nu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export use iter *
99
export use log *
1010
export use testing *
1111
export use xml *
12-
export use dt datetime-diff
12+
export use dt [datetime-diff, pretty-print-duration]
1313

1414
# Add the given paths to the PATH.
1515
#
@@ -172,7 +172,7 @@ def "from ns" [] {
172172
# >
173173
# ```
174174
#
175-
# > **Note**
175+
# > **Note**
176176
# > `std bench --pretty` will return a `string`.
177177
#
178178
# # Examples
@@ -244,7 +244,7 @@ Our (ansi green)Documentation(ansi reset) is located at (ansi green)https://nush
244244
Learn how to remove this at: (ansi green)https://nushell.sh/book/configuration.html#remove-welcome-message(ansi reset)
245245
246246
It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
247-
(($dt).year)yr (($dt).month)month (($dt).day)day (($dt).hour)hr (($dt).minute)min (($dt).second)sec (($dt).millisecond)ms (($dt).microsecond)µs
247+
(pretty-print-duration $dt)
248248
249249
Startup Time: ($nu.startup-time)
250250
"

0 commit comments

Comments
 (0)