Skip to content

Commit

Permalink
Merge pull request #2 from system4-tech/fix-date-format
Browse files Browse the repository at this point in the history
fix date format in filename
  • Loading branch information
joakimhellum authored Dec 10, 2024
2 parents 7f7541c + 5bb7f28 commit d66cf87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
30 changes: 27 additions & 3 deletions dist/binance-data-downloader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,26 @@ now() {
date '+%Y-%m-%d %H:%M:%S.%3N'
}

#######################################
# Formats a given date string into a specified format.
# Globals:
# None
# Arguments:
# date (string): Date string
# format (string): Desired date format
# Outputs:
# Writes the formatted date to stdout
# Returns:
# 0 on success, 1 if the date is invalid
#######################################
format_date() {
local date=${1:?missing required <date> argument}
local format=${2:?missing required <format> argument}

date -d "$date" +"$format"
}


#######################################
# Converts a JSON array into newline-delimited JSON (NDJSON).
# Globals:
Expand Down Expand Up @@ -606,7 +626,7 @@ symbols() {
fail "Error: $response"?
}

echo "$response" | jq -r '.symbols[].symbol'
jq -r '.symbols[].symbol' <<< "$response"
}

#######################################
Expand Down Expand Up @@ -688,8 +708,9 @@ main() {
local product interval start_time symbols klines
local format="csv"
local output_dir="."
local end_time=$(today)
local end_time=$(now)
local max_parallel=4 num_jobs=0
local start_date end_date

while getopts ":p:i:s:e:f:o:P:h" opt; do
case "$opt" in
Expand All @@ -709,6 +730,9 @@ main() {
: ${interval:?Missing required <interval>}
: ${start_time:?Missing required <start_time>}

start_date=$(format_date "$start_time" "%Y-%m-%d")
end_date=$(format_date "$end_time" "%Y-%m-%d")

case "$format" in
tsv|csv|ndjson) ;;
*) fail "Error: Invalid format '$format'. Valid formats are: tsv, csv, ndjson." ;;
Expand All @@ -722,7 +746,7 @@ main() {

process_symbol() {
local symbol=$1
local filename="${output_dir}/${symbol}_${start_time}_${end_time}.${format}"
local filename="${output_dir}/${symbol}_${start_date}_${end_date}.${format}"
local temp_file=$(mktemp)

trap 'rm -f "$temp_file"' EXIT
Expand Down
8 changes: 6 additions & 2 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ main() {
local product interval start_time symbols klines
local format="csv"
local output_dir="."
local end_time=$(today)
local end_time=$(now)
local max_parallel=4 num_jobs=0
local start_date end_date

while getopts ":p:i:s:e:f:o:P:h" opt; do
case "$opt" in
Expand All @@ -45,6 +46,9 @@ main() {
: ${interval:?Missing required <interval>}
: ${start_time:?Missing required <start_time>}

start_date=$(format_date "$start_time" "%Y-%m-%d")
end_date=$(format_date "$end_time" "%Y-%m-%d")

case "$format" in
tsv|csv|ndjson) ;;
*) fail "Error: Invalid format '$format'. Valid formats are: tsv, csv, ndjson." ;;
Expand All @@ -58,7 +62,7 @@ main() {

process_symbol() {
local symbol=$1
local filename="${output_dir}/${symbol}_${start_time}_${end_time}.${format}"
local filename="${output_dir}/${symbol}_${start_date}_${end_date}.${format}"
local temp_file=$(mktemp)

trap 'rm -f "$temp_file"' EXIT
Expand Down

0 comments on commit d66cf87

Please sign in to comment.