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…

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 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" 
done

Output:

2023-07-06 09:34:10 EEST

The Input: Timestamp in UTC

2023-07-06 06:34:10 UTC

This 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 
  ... 
done

This pattern is useful because:

  • You can easily replace echo with cat filename.txt to process many timestamps.
  • The read command 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:

  • UTC
  • America/New_York
  • Asia/Tokyo
  • Europe/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 UTC in 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 EEST

Note:

  • 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" 
done

If timestamps.txt contains:

2023-07-06 06:34:10 UTC 
2023-12-01 15:00:00 UTC

You get:

2023-07-06 09:34:10 EEST 
2023-12-01 17:00:00 EET

7. 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:10

Note 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 coreutils

2️⃣ 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.