From 913880c49df25f39fcf7d7092301aab2b5b3b223 Mon Sep 17 00:00:00 2001 From: NonlinearFruit <1123benji5813@gmail.com> Date: Sat, 6 Jan 2024 07:50:27 -0700 Subject: [PATCH] Convert date scripts to nushell --- scripts/datediff | 33 +++++++++++++++++++++------------ scripts/sundays | 19 ++++++++++++++++--- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/scripts/datediff b/scripts/datediff index 64851a9..b5807df 100755 --- a/scripts/datediff +++ b/scripts/datediff @@ -1,12 +1,21 @@ -d1=$(date -d now +%s) -d2=$(date -d "$*" +%s) -delta=$((d1 - d2)) -# echo $(( delta )) seconds -# echo $(( delta / 60 )) minutes -# echo $(( delta / (60*60) )) hours -# echo $(( delta / (60*60*24) )) days -# echo $(( delta / (60*60*24*7) )) weeks -# echo $(( delta / (60*60*24*30) )) months -# echo $(( delta / (60*60*24*365) )) years -# echo $(( delta / (60*60*24*365*10) )) decades -echo $(( delta / (60*60*24) )) +#!/usr/bin/env nu + +def main [ + date: string # Date to harken back to + --seconds # Diff in seconds + --minutes # Diff in minutes + --hours # Diff in hours + --days = true # Diff in days + --weeks # Diff in weeks + --years # Diff in years +] { + let then = $date | into datetime + let denominator = match [$seconds $minutes $hours $days $weeks] { + [true _ _ _ _] => 1sec, + [_ true _ _ _] => 1min, + [_ _ true _ _] => 1hr, + [_ _ _ true _] => 1day, + [_ _ _ _ true] => 1wk, + } + ((date now) - $then) / $denominator | math round --precision 3 +} diff --git a/scripts/sundays b/scripts/sundays index 8847367..6903666 100755 --- a/scripts/sundays +++ b/scripts/sundays @@ -1,3 +1,16 @@ -birthday=$* -daysTill18=$(datediff $birthday +18 years) -echo "-1 * ${daysTill18} / 7" | bc +#!/usr/bin/env nu + +def main [ + birthdate: string # Someone's birthday +] { + $birthdate + | date to-record + | update year {$in + 18} + | $"($in.month)-($in.day)-($in.year)" + | into datetime + | datediff $in --weeks + | into float + | $in / 7 + | math abs + | math floor +}