Skip to content

Commit 5a5618e

Browse files
committed
Refactor date calculation in create_post.sh for compatibility with macOS and Linux
1 parent 602c2f0 commit 5a5618e

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

create_post.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/bin/bash
22

33
# Calculate the next Tuesday (or today if it's Tuesday)
4-
# In Linux date, weekday: 0=Sunday, 1=Monday, ..., 6=Saturday
54
current_weekday=$(date +%w) # 0=Sun ... 6=Sat
65
days_to_tuesday=$(( (2 - current_weekday + 7) % 7 ))
76

8-
# If today is Tuesday, days_to_tuesday will be 0 → perfect
9-
# Add the calculated days
10-
tuesday=$(date -d "+${days_to_tuesday} days" +%Y-%m-%d)
11-
12-
year=$(date -d "$tuesday" +%Y)
13-
month=$(date -d "$tuesday" +%02m)
14-
day=$(date -d "$tuesday" +%02d)
7+
# Add the calculated days (compatible with both macOS and Linux)
8+
if date -v +0d &>/dev/null; then
9+
# macOS (BSD date)
10+
tuesday=$(date -v "+${days_to_tuesday}d" +%Y-%m-%d)
11+
else
12+
# Linux (GNU date)
13+
tuesday=$(date -d "+${days_to_tuesday} days" +%Y-%m-%d)
14+
fi
15+
16+
year=${tuesday%%-*} # 2026
17+
month=${tuesday#*-}; month=${month%-*} # 02
18+
day=${tuesday##*-} # 24
1519

1620
# Define paths
1721
folder_path="docs/posts/$year/$month/$day"

0 commit comments

Comments
 (0)