Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix date format in filename #2

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading