Converting Timestamps Between Timezones on Linux
When working with logs, scheduling, or data pipelines, you often need to convert timestamps from one timezone to another. The date command…
When working with logs, scheduling, or data pipelines, you often need to convert timestamps from one timezone to another. The date command on Linux makes this simple and reliable.
1. This article explains how the following command works:
echo "2023-07-06 06:34:10 UTC" | while read line; do
TZ="Europe/Athens" date -d "$line" +"%Y-%m-%d %H:%M:%S %Z"
doneOutput:
2023-07-06 09:34:10 EESTThe Input: Timestamp in UTC
2023-07-06 06:34:10 UTCThis is an ISO 8601-style date string, with UTC explicitly specifying the timezone.
If you omit UTC, date assumes the timestamp is in the target timezone (Europe/Athens), which can produce the wrong result.
✅ Always include the source timezone for clarity.
2. The Pipeline: echo into while read
echo "2023-07-06 06:34:10 UTC" | while read line; do
...
doneThis pattern is useful because:
- You can easily replace
echowithcat filename.txtto process many timestamps. - The
readcommand reads each line into a variable$line.
3. Setting the Output Timezone
TZ="Europe/Athens"The TZ environment variable tells date which timezone to display the result in.
Common examples:
UTCAmerica/New_YorkAsia/TokyoEurope/Athens
The timezone names come from the IANA timezone database (/usr/share/zoneinfo).
4. Parsing and Converting with date
date -d "$line"-d tells date to:
- Parse the string in
$line - Recognize the
UTCin the input - Convert it to the output timezone (
Europe/Athens)
5. Formatting the Output
+"%Y-%m-%d %H:%M:%S %Z"This specifies:
%Y: 4-digit year%m: 2-digit month%d: day%H:%M:%S: hour, minute, second%Z: timezone abbreviation
Example output:
2023-07-06 09:34:10 EESTNote:
- UTC time was 06:34
- Athens in July is UTC+3 (EEST)
- So it correctly converts to 09:34
6. Processing Multiple Timestamps
To convert multiple timestamps from a file:
cat timestamps.txt | while read line; do
TZ="Europe/Athens" date -d "$line" +"%Y-%m-%d %H:%M:%S %Z"
doneIf timestamps.txt contains:
2023-07-06 06:34:10 UTC
2023-12-01 15:00:00 UTCYou get:
2023-07-06 09:34:10 EEST
2023-12-01 17:00:00 EET7. Why Use This Approach?
✅ Clear separation of:
- Input timezone (UTC)
- Output timezone (Europe/Athens)
✅ Handles daylight saving time automatically.
✅ Flexible: works in scripts, cron jobs, and data pipelines.
✅ Avoids manual timezone calculations.
Final Tip
Always include the timezone in your input string if it’s UTC or anything other than your system timezone.
✅ Correct:
2023-07-06 06:34:10 UTC❌ Wrong:
2023-07-06 06:34:10Note for macOS Users
On macOS, the default date does not support -d.
You will need GNU date (gdate) instead:
1️⃣ Install via Homebrew:
brew install coreutils2️⃣ Replace date with gdate:
echo "2023-07-06 06:34:10 UTC" | while read line; do
TZ="Europe/Athens" gdate -d "$line" +"%Y-%m-%d %H:%M:%S %Z"
done✅ Now it works exactly like Linux.
✅ Conclusion
Converting timestamps between timezones is a common but critical task for anyone working with logs, data pipelines, or distributed systems. With the simple date command on Linux, you can accurately transform and format your times, handle daylight saving changes, and avoid manual calculation errors. Whether you’re processing a single timestamp or thousands in a file, this approach keeps your workflows clean, reproducible, and timezone-aware.