Skip to content

Update Timezones (if needed) #82

Update Timezones (if needed)

Update Timezones (if needed) #82

name: Update Timezones (if needed)
on:
schedule:
- cron: '0 0 * * 0' # Run Sunday at midnight
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Fetch latest tzdb version first
- name: Fetch latest tzdb release filename
id: fetch_latest
run: |
latest=$(curl -s ftp://ftp.iana.org/tz/releases/ | grep -oE 'tzdata[0-9]{4}[a-z]\.tar\.gz' | sort -V | tail -1)
echo "Latest release file: $latest"
version=$(echo $latest | grep -oE '[0-9]{4}[a-z]')
echo "Latest version: $version"
echo "version=$version" >> $GITHUB_OUTPUT
# Read currently recorded tzdb version
- name: Read current tzdb version
id: read_version
run: |
current=$(cat tzdb_version.txt)
echo "Current version: $current"
echo "current=$current" >> $GITHUB_OUTPUT
# Decide if update is needed
- name: Check if tzdb update needed
id: check_update
run: |
if [ "${{ steps.fetch_latest.outputs.version }}" = "${{ steps.read_version.outputs.current }}" ]; then
echo "Up-to-date, no update needed."
echo "update_needed=false" >> $GITHUB_OUTPUT
else
echo "New version available, update needed."
echo "update_needed=true" >> $GITHUB_OUTPUT
fi
# Proceed with environment setup and generation only if update is needed
- uses: actions/setup-python@v5
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
with:
python-version: '3.x'
- name: Install dependencies
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: pip install natsort
# Download and install the new tzdb version
- name: Download and install tzdb
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: |
cd /tmp
wget ftp://ftp.iana.org/tz/releases/tzdata${{ steps.fetch_latest.outputs.version }}.tar.gz
mkdir -p tzdata
tar -xzf tzdata${{ steps.fetch_latest.outputs.version }}.tar.gz -C tzdata
# Compile and install the timezone data
sudo zic -d /usr/share/zoneinfo tzdata/africa tzdata/antarctica tzdata/asia tzdata/australasia tzdata/europe tzdata/northamerica tzdata/southamerica tzdata/etcetera tzdata/backward
echo "Installed tzdb version: ${{ steps.fetch_latest.outputs.version }}"
- name: Generate timezones.json and csv
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: |
python3 ./gen-tz.py --json > timezones.json
python3 ./gen-tz.py --csv > timezones.csv
python3 ./gen-tz.py --minimal-json > timezones.min.json
python3 ./gen-tz.py --minimal-csv > timezones.min.csv
- name: Gzip outputs
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: |
gzip -fk timezones.json
gzip -fk timezones.csv
gzip -fk timezones.min.json
gzip -fk timezones.min.csv
- name: Show contents of current directory
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: ls -alh
# Verify files were generated
- name: Verify generated files
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: |
echo "Checking for generated files..."
ls -lh timezones.json timezones.csv timezones.min.json timezones.min.csv
ls -lh timezones.json.gz timezones.csv.gz timezones.min.json.gz timezones.min.csv.gz
echo "All files present!"
# Update tzdb_version.txt with latest version
- name: Update tzdb_version.txt
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: echo "${{ steps.fetch_latest.outputs.version }}" > tzdb_version.txt
# Generate markdown files for timezone tables
- name: Generate timezone markdown files
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
run: |
# Generate timezones.min.md
echo "# Timezones.min.csv Time Zones Minimal List" > timezones.min.md
echo "" >> timezones.min.md
echo "| Timezone | POSIX String |" >> timezones.min.md
echo "|----------|--------------|" >> timezones.min.md
while IFS=',' read -r zone posix; do
zone=$(echo "$zone" | tr -d '"')
posix=$(echo "$posix" | tr -d '"')
echo "| \`$zone\` | \`$posix\` |" >> timezones.min.md
done < timezones.min.csv
# Generate timezones.md
echo "# Timezones.csv Time Zones Full List" > timezones.md
echo "" >> timezones.md
echo "| Timezone | POSIX String |" >> timezones.md
echo "|----------|--------------|" >> timezones.md
while IFS=',' read -r zone posix; do
zone=$(echo "$zone" | tr -d '"')
posix=$(echo "$posix" | tr -d '"')
echo "| \`$zone\` | \`$posix\` |" >> timezones.md
done < timezones.csv
echo "Generated markdown table files"
- name: Commit and push all at once
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
env:
TZDB_VERSION: ${{ steps.fetch_latest.outputs.version }}
run: |
git config --global user.name 'github-actions'
git config --global user.email 'actions@github.com'
git add timezones.json timezones.json.gz timezones.csv timezones.csv.gz timezones.min.json timezones.min.json.gz timezones.min.csv timezones.min.csv.gz timezones.md timezones.min.md tzdb_version.txt
git diff --staged --quiet || git commit -m "Update timezones JSON and CSV files - $TZDB_VERSION"
git push
# Create a GitHub release and upload all assets
- name: Create Release
if: ${{ steps.check_update.outputs.update_needed == 'true' }}
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.fetch_latest.outputs.version }}
name: Release ${{ steps.fetch_latest.outputs.version }}
files: |
*.gz
*.json
*.csv
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}