Timestamp conversion in MacOS
Contents
Idea
This weekend I work on a project that use Strava activity data and Forest data to generate heat map. The data source used to render the JavaScript component is in JSON format, and the date entry is in timestamp.
To update new data entry, I can either use the python to process the whole pipeline or just edit the JSON file needed in React. For convenience, I choose to amend the JSON file, that’s why I need to “Timestamp conversion” here.
Definition
Timestamp to Date
date -r 1696580400
# Fri Oct 6 01:20:00 PDT 2023
Date to Timestamp
date -j -f "%Y-%m-%d %H:%M:%S" "2024-10-07 00:00:00" +%s
# 1728284400
date -j -f "%Y-%m-%d %H:%M:%S" "$(date +%Y-%m-%d) 00:00:00" +%s # get today
date -j -v-1d -f "%Y-%m-%d %H:%M:%S" "$(date +%Y-%m-%d) 00:00:00" +%s # get yesterday
Generating yesterday’s timestamp is indeed useful because I should update it in next day, for full accuracy.