From d6238612319ca527a5606c10bf6ab2942a4c4919 Mon Sep 17 00:00:00 2001 From: SidestreamColdMelon Date: Mon, 4 Dec 2023 10:35:35 +0100 Subject: [PATCH 1/3] add support for Darwin platform --- scripts/time.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/time.sh b/scripts/time.sh index f3af80379..14a818d41 100755 --- a/scripts/time.sh +++ b/scripts/time.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash set -e +SYSTEM=`uname`; +FORMAT="%Y-%m-%d %H:%M:%S"; + for ARGUMENT in "$@" do KEY=$(echo "$ARGUMENT" | cut -f1 -d=) @@ -16,15 +19,25 @@ done if [[ -z "$STAMP" && -z "$DATE" ]]; then cat <&2 <<. Please use the date or stamp flag, ex: - ${0##*/} date="1 May 2022" + ${0##*/} date="2023-12-01 00:00:00" or ${0##*/} stamp="1651363200" . exit 1 elif [[ -z "$DATE" ]]; then - date -u -d @"$STAMP" - date -u -d @"$STAMP" +%s + if [ "$SYSTEM" = "Darwin" ]; then + date -u -r "$STAMP" + date -u -r "$STAMP" "+$FORMAT" + else + date -u -d @"$STAMP" + date -u -d @"$STAMP" +%s + fi else - date --utc --date="$DATE" - date --utc --date="$DATE" +%s + if [ "$SYSTEM" = "Darwin" ]; then + date -u -j -f "$FORMAT" "$DATE" + date -u -j -f "$FORMAT" "$DATE" +%s + else + date --utc --date="$DATE" + date --utc --date="$DATE" +%s + fi fi From 489508af1a9dcb15d072c1e3b75d4cb3faddd34f Mon Sep 17 00:00:00 2001 From: SidestreamColdMelon Date: Wed, 6 Dec 2023 11:52:26 +0100 Subject: [PATCH 2/3] replace shell script with python --- Makefile | 2 +- scripts/time.py | 26 ++++++++++++++++++++++++++ scripts/time.sh | 43 ------------------------------------------- 3 files changed, 27 insertions(+), 44 deletions(-) create mode 100755 scripts/time.py delete mode 100755 scripts/time.sh diff --git a/Makefile b/Makefile index 3f9c2f0e5..6fcf81299 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ diff-archive-spell :; ./scripts/diff-archive-dssspell.sh "$(if $(date),$(date) feed :; ./scripts/check-oracle-feed.sh $(pip) feed-lp :; ./scripts/check-oracle-feed-lp.sh $(pip) wards :; ./scripts/wards.sh $(target) -time :; ./scripts/time.sh date="$(date)" stamp="$(stamp)" +time :; ./scripts/time.py date="$(date)" stamp="$(stamp)" exec-hash :; ./scripts/hash-exec-copy.sh "$(if $(date),$(date),$(shell date +'%Y-%m-%d'))" opt-cost :; ./scripts/get-opt-relay-cost.sh $(spell) arb-cost :; ./scripts/get-arb-relay-cost.sh $(spell) diff --git a/scripts/time.py b/scripts/time.py new file mode 100755 index 000000000..53c2990c8 --- /dev/null +++ b/scripts/time.py @@ -0,0 +1,26 @@ +#! /usr/bin/env python3 + +import argparse +from datetime import datetime, timezone + +DATE_FORMAT="%Y-%m-%d %H:%M:%S"; + +# Parse positional arguments +parser=argparse.ArgumentParser() +parser.add_argument("date", help="Converts date to UTC timestamp") +parser.add_argument("stamp", help="Converts timestamp to UTC date") +parsed=parser.parse_args() + +# Cleanup positional arguments +date = parsed.date.replace("date=", "").replace(" UTC", "") +stamp = parsed.stamp.replace("stamp=", "") + +# Convert provided input in UTC format into desired output in UTC as well +if date: + utc_date = datetime.strptime(date, DATE_FORMAT).replace(tzinfo=timezone.utc) + print(utc_date) + print(int(utc_date.timestamp())) +if stamp: + utc_date = datetime.fromtimestamp(int(stamp), timezone.utc) + print(utc_date) + print(utc_date.strftime(DATE_FORMAT)) diff --git a/scripts/time.sh b/scripts/time.sh deleted file mode 100755 index 14a818d41..000000000 --- a/scripts/time.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash -set -e - -SYSTEM=`uname`; -FORMAT="%Y-%m-%d %H:%M:%S"; - -for ARGUMENT in "$@" -do - KEY=$(echo "$ARGUMENT" | cut -f1 -d=) - VALUE=$(echo "$ARGUMENT" | cut -f2 -d=) - - case "$KEY" in - date) DATE="$VALUE" ;; - stamp) STAMP="$VALUE" ;; - *) - esac -done - -if [[ -z "$STAMP" && -z "$DATE" ]]; then -cat <&2 <<. -Please use the date or stamp flag, ex: - ${0##*/} date="2023-12-01 00:00:00" - or - ${0##*/} stamp="1651363200" -. - exit 1 -elif [[ -z "$DATE" ]]; then - if [ "$SYSTEM" = "Darwin" ]; then - date -u -r "$STAMP" - date -u -r "$STAMP" "+$FORMAT" - else - date -u -d @"$STAMP" - date -u -d @"$STAMP" +%s - fi -else - if [ "$SYSTEM" = "Darwin" ]; then - date -u -j -f "$FORMAT" "$DATE" - date -u -j -f "$FORMAT" "$DATE" +%s - else - date --utc --date="$DATE" - date --utc --date="$DATE" +%s - fi -fi From f44738b837444de1d877a00bc2c3de175af545bf Mon Sep 17 00:00:00 2001 From: SidestreamColdMelon Date: Wed, 6 Dec 2023 16:57:14 +0100 Subject: [PATCH 3/3] accept any iso date as input --- scripts/time.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/time.py b/scripts/time.py index 53c2990c8..83993c54b 100755 --- a/scripts/time.py +++ b/scripts/time.py @@ -12,12 +12,12 @@ parsed=parser.parse_args() # Cleanup positional arguments -date = parsed.date.replace("date=", "").replace(" UTC", "") +date = parsed.date.replace("date=", "").upper().replace(" UTC", "") stamp = parsed.stamp.replace("stamp=", "") # Convert provided input in UTC format into desired output in UTC as well if date: - utc_date = datetime.strptime(date, DATE_FORMAT).replace(tzinfo=timezone.utc) + utc_date = datetime.fromisoformat(date).replace(tzinfo=timezone.utc) print(utc_date) print(int(utc_date.timestamp())) if stamp: