dot

packages and services management
Log | Files | Refs | README

commit 35f3134befae47e69ba4c874608efa8afb6d3ad6
parent 8025c385b570354ea90f7468ce6a7948ffbac7bc
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 19 Nov 2017 22:52:02 +0100

parsing rfc3339 time in awk (to epoch)

Diffstat:
Mbin/calendar-show | 67++++++++++++++++++++++++++++++++++---------------------------------
Mbin/doc-rfc | 4++--
Abin/twtsh | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdot/ssh/config | 4++++
4 files changed, 157 insertions(+), 35 deletions(-)

diff --git a/bin/calendar-show b/bin/calendar-show @@ -1,46 +1,47 @@ #!/bin/sh awk ' -function -bissextile(year) +function bissextile(yrs) { - return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); + return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); } -function -days_in_month(month, year) +function days_per_month(mth, yrs) { - if (month == 2) { - return 28 + is_bissextile(year); - } else if (month < 8) { - return 30 + (month % 2 == 1); - } else { - return 30 + (month % 2 == 0); - } + if (mth == 2) + return 28 + bissextile(yrs); + else if (mth < 7) + return 30 + mth % 2; + else + return 30 + mth % 2; } -function -to_seconds(year, month, day, hour, minute, second) +function to_seconds(yrs, mth, day, hrs, min, sec) { - for (month--; month > 0; month--) - day += days_in_month(month, year); - - for (year--; year >= 1970; year--) - day += 365 + is_bissextile(year); - - return (second + 60 * (minute + 60 * (hour + (24 * (day - 1))))); -}' + for (mth--; mth > 0; mth--) + day += days_per_month(mth, yrs); + for (yrs--; yrs >= 1970; yrs--) + day += 365 + bissextile(yrs); + return sec + 60 * (min + 60 * (hrs + (24 * (day - 1)))); +} -function -to_date(seconds) +function to_date(fmt, sec) { - year = 1970 - - b = bissextile(year) - while (seconds > 3600 * 24 * (365 + b)) { - seconds -= 3600 * 24 * (364 + is_bissextile(year)) - b = bissextile(++year) + b = bissextile(1970); + for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { + b = bissextile(yrs); + sec -= 3600 * 24 * (365 + bissextile(yrs)); } - - return year "/" month "/" day " " hour ":" minute ":" second -} + d = days_per_month(1); + for (mth = 1; sec >= 3600 * 24 * d; mth++) { + d = days_per_month(mth); + sec -= 3600 * 24 * d; + } + for (day = 1; sec >= 3600 * 24; day++) + sec -= 3600 * 24; + for (hrs = 0; sec >= 3600; hrs++) + sec -= 3600; + for (min = 0; sec >= 60; min++) + sec -= 60; + return sprintf(fmt, yrs, mth, day, hrs, min, sec); +}' diff --git a/bin/doc-rfc b/bin/doc-rfc @@ -1,6 +1,6 @@ #!/bin/sh # download and print an RFC from ietf.org -wget -q -O- http://www.ietf.org/rfc/rfc"$1".txt | +curl -L http://www.ietf.org/rfc/rfc"$1".txt | awk -v RS='\n*[^\n]*\n\f\n[^\n]*\n*' '{ print $0 }' | -$MANPAGER +$PAGER diff --git a/bin/twtsh b/bin/twtsh @@ -0,0 +1,117 @@ +#!/bin/sh + +TWT_NICK=josuah +TWT_FILE=$HOME/twtxt.txt +TWT_CONF=$HOME + +err() +{ + printf 'Error: %s\n' "$1" + exit 1 +} + +mkdir -p "$TWT_CONF" +touch "$TWT_CONF/following.txt" + +awk_date=' +function bissextile(yrs) +{ + return yrs % 4 == 0 && (yrs % 100 != 0 || yrs % 400 == 0); +} + +function days_per_month(mth, yrs) +{ + if (mth == 2) + return 28 + bissextile(yrs); + else if (mth < 7) + return 30 + mth % 2; + else + return 30 + mth % 2; +} + +function to_sec(yrs, mth, day, hrs, min, sec) +{ + for (mth--; mth > 0; mth--) + day += days_per_month(mth, yrs); + for (yrs--; yrs >= 1970; yrs--) + day += 365 + bissextile(yrs); + return sec + 60 * (min + 60 * (hrs + (24 * day))); +} + +function to_date(fmt, sec) +{ + b = bissextile(1970); + for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { + b = bissextile(yrs); + sec -= 3600 * 24 * (365 + bissextile(yrs)); + } + d = days_per_month(1); + for (mth = 1; sec >= 3600 * 24 * d; mth++) { + d = days_per_month(mth); + sec -= 3600 * 24 * d; + } + for (day = 1; sec >= 3600 * 24; day++) + sec -= 3600 * 24; + for (hrs = 0; sec >= 3600; hrs++) + sec -= 3600; + for (min = 0; sec >= 60; min++) + sec -= 60; + return sprintf(fmt, yrs, mth, day, hrs, min, sec); +} + +function rfc3339(str) { + yrs = substr(str, 1, 4); + mth = substr(str, 6, 2); + day = substr(str, 9, 2); + hrs = substr(str, 12, 2); + min = substr(str, 15, 2); + sec = substr(str, 18, 2); + off = substr(str, 20, 1) =="Z" ? 0 : substr(str, 20, 3); + return to_sec(yrs, mth, day, hrs, min, sec) - off * 3600; +}' + +case "$1" in +(tweet) + [ $# -ne 2 ] && exec "$0" + [ "${#2}" -eq 0 ] && err 'Empty tweet.' + msg=$(printf %s "$2" | sed 's|[^ ]*://[^ ]*|@<&>|') + while read -r usr url + do + new=$(printf %s "$usr" | sed 's/[&/\]/\\&/g') + usr=$(printf %s "$usr" | sed 's/./[&]/g') + url=$(printf %s "$url" | sed 's/[&/\]/\\&/g') + msg=$(printf %s "$msg" | sed "s/@$usr/@<$new $url>/") + done < "$TWT_CONF/following.txt" + printf '%s\t%s\n' "$(date +'%Y-%m-%dT%T%z' | sed 's/..$/:&/')" "$msg" | + tr '\n' ' ' >> "$TWT_FILE" + ;; +(view) + [ $# -ne 2 ] && exec err "$0" + awk -v off="$(date +%z)" "$awk_date"'{ + sec = rfc3339($1) + off * 3600; + print(to_date("%04d/%02d/%02d %02d:%02d:%02d", sec)); + }' "$TWT_FILE" + ;; +(follow) + [ $# -ne 3 ] && exec "$0" + sed 's/[ ].*//' "$TWT_CONF/following.txt" | grep -Fqx "$2" && + err "already following the nick $2" + printf '%s\t%s\n' "$2" "$3" >> "$TWT_CONF/following.txt" + sort -u "$TWT_CONF/following.txt" -o "$TWT_CONF/following.txt" + ;; +(unfollow) + [ $# -ne 2 ] && exec "$0" + tr '\t' ' ' | sed 's/ .*//' "$TWT_CONF/following.txt" | grep -Fx "$2" || + err "not folling $2" + ;; +(following) + [ $# -ne 1 ] && exec "$0" + tr '\t' ' ' < "$TWT_CONF/following.txt" | + sed -r -e 's/ / /' -e 's/(.{10}[^ ]*) */ \1 @ /' + ;; +(*) + printf 'usage: twtsh tweet msg | view nick | timeline\n' + printf ' follow nick url | unfollow nick | following\n' + ;; +esac + diff --git a/dot/ssh/config b/dot/ssh/config @@ -25,3 +25,7 @@ host store.josuah.net host josuah.net IdentityFile /mnt/key/josuah.net.key AddKeysToAgent yes + +host store.josuah.net + IdentityFile /mnt/key/josuah.net.key + AddKeysToAgent yes