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

fmtdate from Unix Epoch or Excel Epoch number -- feature request #298

Open
avilella opened this issue Jan 21, 2025 · 1 comment
Open

fmtdate from Unix Epoch or Excel Epoch number -- feature request #298

avilella opened this issue Jan 21, 2025 · 1 comment

Comments

@avilella
Copy link

Would it be possible to implement a date converter in fmtdate from Unix Epoch or Excel Epoch?

See below:

echo -e "date\n1737454964\n1737454954" | csvtk fmtdate -f 1 --unix-epoch

echo -e "date\n45674.412777777776\n45674.410474537035" | csvtk fmtdate -f 1 --excel-epoch

See the explanation the ChatGPT Enterprise gave me for this (in bash):

# Input Excel serial date
excel_date="45677.39685185185"

# Define constants
excel_epoch="1900-01-01"
unix_epoch="1970-01-01"

# Convert Excel date to seconds since the Unix epoch
seconds_since_epoch=$(
  awk -v ed="$excel_date" 'BEGIN {
    excel_epoch_offset = 25569;  # Excel epoch in days since Unix epoch
    seconds_per_day = 86400;
    seconds_since_epoch = (ed - excel_epoch_offset) * seconds_per_day;
    print seconds_since_epoch;
  }'
)

# Format the date in YYYY-MM-DD hh:mm:ss
formatted_date=$(date -u -d "@$seconds_since_epoch" +"%Y-%m-%d %H:%M:%S")

echo "$formatted_date"
@shenwei356
Copy link
Owner

It seems that the package for parsing date (https://github.com/araddon/dateparse) can support it (UNIX epoch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants