ESP32 NTP Time Zone and Daylight Saving with POSIX TZ Strings
ESP32 NTP time zone and daylight saving: read a POSIX TZ string, ready strings for Berlin, London, New York and Mexico City, configTzTime code and a DST test.
Setting up the ESP32 NTP time zone and daylight saving time takes a single line of text, yet most examples get it wrong. The ESP32 always receives the time from internet NTP servers as UTC; to show your city's time, you have to tell it the time zone and the daylight saving time (DST) rules. If you do that with a short piece of text called a POSIX TZ string, the ESP32 applies the March and October/November changes by itself every year, and you never have to touch the code again.
In this article we take the TZ string apart letter by letter, give ready-made strings for Berlin, Paris, Rome, Madrid, London, New York, Los Angeles, Mexico City and Latin America in one table, write working code with configTzTime and show how to test the clock change without waiting for months. If you want the NTP basics, showing the time on an OLED and an RTC backup first, have a look at ESP32 NTP Internet Time.

Why does daylight saving time cause trouble on the ESP32?
The most common mistake is writing the offset as a fixed number, as in configTime(3600, 3600, ...). The second number means "add one hour when daylight saving is on", but it never says when it is on. As a result, the clock is either one hour ahead all year or runs on winter time all summer. Countries without DST, such as Turkey (fixed UTC+3 since 2016), never notice this; but anyone who builds a clock for Germany, Spain or the US gets the first surprise at the end of March. The right fix is to give the rule itself, not a number.
What is a POSIX TZ string and how do you read it?
A POSIX TZ string describes, in one line, the standard time name, the UTC offset, the daylight saving name and the day and hour when daylight saving starts and ends. The ESP32's C library reads this string and works out on every localtime call whether it is currently winter or summer time. Let's break down the Central European string:
| Part | Meaning |
|---|---|
| CET | Abbreviation of standard (winter) time |
| -1 | UTC offset with the sign reversed: -1 for UTC+1 |
| CEST | Abbreviation of summer time; with no offset given it is 1 hour ahead of standard |
| M3.5.0 | DST start: month 3 (March), week 5 (last), day 0 (Sunday) |
| (no time) | Without a start time, 02:00 local time is assumed |
| M10.5.0/3 | End: last Sunday of October at 03:00 (summer time) |
In the Mm.w.d format, m is the month (1–12), w is the week (1–5; 5 always means "the last" week of the month) and d is the day of the week (0 Sunday, 6 Saturday). So M3.5.0 means "last Sunday of March" and M3.2.0 means "second Sunday of March". The switch time comes after a slash; /3 means 03:00 and /1 means 01:00. If a zone name contains digits or signs, it goes in angle brackets: Turkey's string <+03>-3 is written like that and has no DST rule.
Ready TZ strings: Europe, North America and Latin America
| City | TZ string | Winter / summer | DST period |
|---|---|---|---|
| Istanbul | <+03>-3 | UTC+3 (fixed) | None (since 2016) |
| Berlin, Paris, Rome, Madrid | CET-1CEST,M3.5.0,M10.5.0/3 | UTC+1 / UTC+2 | Last Sunday of March – last Sunday of October |
| London | GMT0BST,M3.5.0/1,M10.5.0 | UTC+0 / UTC+1 | Last Sunday of March – last Sunday of October |
| Lisbon | WET0WEST,M3.5.0/1,M10.5.0 | UTC+0 / UTC+1 | Last Sunday of March – last Sunday of October |
| New York, Toronto | EST5EDT,M3.2.0,M11.1.0 | UTC-5 / UTC-4 | 2nd Sunday of March – 1st Sunday of November |
| Chicago | CST6CDT,M3.2.0,M11.1.0 | UTC-6 / UTC-5 | 2nd Sunday of March – 1st Sunday of November |
| Los Angeles | PST8PDT,M3.2.0,M11.1.0 | UTC-8 / UTC-7 | 2nd Sunday of March – 1st Sunday of November |
| Phoenix | MST7 | UTC-7 (fixed) | None |
| Mexico City | CST6 | UTC-6 (fixed) | None (since 2022) |
| Bogotá, Lima | <-05>5 | UTC-5 (fixed) | None |
| Buenos Aires, São Paulo | <-03>3 | UTC-3 (fixed) | None |
| Sydney | AEST-10AEDT,M10.1.0,M4.1.0/3 | UTC+10 / UTC+11 | 1st Sunday of October – 1st Sunday of April |
Two things stand out in the table. First, all European countries switch at the same moment, 01:00 UTC: in Berlin the clock jumps from 02:00 to 03:00, in London from 01:00 to 02:00. In the US, every city switches at 02:00 its own local time. Second, rules change: Mexico dropped DST in 2022, Brazil in 2019 and Turkey in 2016. A string copied from an old example may be wrong today; if a country changes its law, you only update this one line. In the southern hemisphere (Sydney), DST starts in October and ends in April, and the ESP32 handles that correctly too.
Step by step: automatic daylight saving with configTzTime
- Make sure the ESP32 board package is installed in the Arduino IDE and select ESP32 Dev Module as the board.
- Replace the Wi-Fi name and password in the code with your own; the ESP32 only connects to 2.4 GHz networks.
- Paste your city's string from the table into the TZ_INFO line.
- Upload the code, open the Serial Monitor at 115200 baud and check that CET or CEST appears next to the time.
1#include <WiFi.h>
2#include <time.h>
3
4const char* WIFI_SSID = "AgAdi"; // Wi-Fi adı / Wi-Fi name
5const char* WIFI_PASS = "Sifre123"; // Wi-Fi şifresi / Wi-Fi password
6// Berlin, Paris, Roma, Madrid: kış UTC+1, yaz UTC+2 / winter UTC+1, summer UTC+2
7const char* TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3";
8
9void setup() {
10 Serial.begin(115200);
11 WiFi.begin(WIFI_SSID, WIFI_PASS);
12 while (WiFi.status() != WL_CONNECTED) { // bağlanana kadar bekle / wait until connected
13 delay(500);
14 Serial.print(".");
15 }
16 Serial.println(" Wi-Fi OK");
17
18 // Saat dilimi + NTP sunucuları tek satırda / time zone + NTP servers in one line
19 configTzTime(TZ_INFO, "pool.ntp.org", "time.cloudflare.com");
20
21 struct tm t;
22 while (!getLocalTime(&t)) { // ilk eşitlemeyi bekle / wait for the first sync
23 Serial.println("NTP bekleniyor / waiting for NTP");
24 }
25}
26
27void loop() {
28 struct tm t;
29 if (getLocalTime(&t)) {
30 // %Z kısaltmayı (CET/CEST) yazar, tm_isdst yaz saatinde 1 olur
31 // %Z prints the abbreviation (CET/CEST), tm_isdst is 1 in summer time
32 Serial.print(&t, "%d.%m.%Y %H:%M:%S %Z");
33 Serial.printf(" yaz saati / DST: %s\n", t.tm_isdst > 0 ? "evet / yes" : "hayir / no");
34 }
35 delay(1000);
36}configTzTime does two jobs at once: it sets the TZ string as an environment variable and starts the SNTP client with the servers you give it. From then on, every getLocalTime call takes the UTC time, checks the rule and hands you local time. The tm_isdst field is 1 in summer time and 0 in winter; use it if you want a "DST" icon on a display. The %Z format specifier prints the abbreviation from the string (CET, CEST, EST, EDT). This code works the same way on the 2.x and 3.x versions of the ESP32 Arduino core.
configTime vs configTzTime
| Method | Example | Daylight saving | When to use it |
|---|---|---|---|
| configTime(sec, dst_sec, ...) | configTime(3600, 3600, "pool.ntp.org") | Always on or always off | Countries without DST |
| configTime(0, 0, ...) + setenv/tzset | setenv("TZ", string, 1); tzset(); | Automatic | Changing the time zone later |
| configTzTime(string, ...) | configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", ...) | Automatic | Recommended |
The second parameter of configTime is not a rule but a fixed addition, so in a country with DST it shows the wrong time for half of the year. In a place with a fixed offset, such as Turkey, configTime(3 * 3600, 0, ...) works fine too, but getting used to configTzTime makes your code portable to any country.
Testing the DST change without Wi-Fi
You don't have to wait for the end of March to see that your code works. You set the system clock a few seconds before the switch with settimeofday and print the time every second. The code below simulates the European changes in spring and autumn 2027; it needs no Wi-Fi or NTP, only the USB cable.
1#include <time.h>
2#include <sys/time.h>
3
4// Wi-Fi gerekmez: saati elle geçişin 5 sn öncesine kurup izleriz
5// No Wi-Fi needed: set the clock 5 s before the switch and watch it
6const char* TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3";
7
8const time_t SPRING_2027 = 1806195600; // 28.03.2027 01:00 UTC: 02:00 CET -> 03:00 CEST
9const time_t AUTUMN_2027 = 1824944400; // 31.10.2027 01:00 UTC: 03:00 CEST -> 02:00 CET
10
11void watchSwitch(time_t moment) {
12 struct timeval tv = { moment - 5, 0 }; // geçişten 5 sn önce / 5 s before the switch
13 settimeofday(&tv, nullptr);
14 for (int i = 0; i < 10; i++) {
15 time_t now = time(nullptr);
16 struct tm t;
17 localtime_r(&now, &t); // UTC'den yerel saate / UTC to local time
18 char line[40];
19 strftime(line, sizeof(line), "%d.%m.%Y %H:%M:%S %Z", &t);
20 Serial.println(line);
21 delay(1000);
22 }
23 Serial.println("----");
24}
25
26void setup() {
27 Serial.begin(115200);
28 delay(500);
29 setenv("TZ", TZ_INFO, 1); // saat dilimini ayarla / set the time zone
30 tzset();
31 watchSwitch(SPRING_2027); // 01:59:59 -> 03:00:00 beklenir / expected
32 watchSwitch(AUTUMN_2027); // 02:59:59 CEST -> 02:00:00 CET beklenir / expected
33}
34
35void loop() {
36}| Test | Before the switch | After the switch |
|---|---|---|
| Spring (28.03.2027) | 01:59:59 CET | 03:00:00 CEST |
| Autumn (31.10.2027) | 02:59:59 CEST | 02:00:00 CET |
| New York spring (14.03.2027) | 01:59:59 EST | 03:00:00 EDT |
Changing and saving the time zone at runtime
If your device will travel between countries, let the user pick the time zone from a list instead of hard-coding it. Calling setenv("TZ", ...) followed by tzset() applies the new rule immediately; you don't have to restart NTP because the system clock is already UTC, and only the rule for converting to local time changes. If you save the choice to flash with the Preferences library, the ESP32 remembers it after a restart. In the example below you switch cities by typing a digit from 0 to 6 in the Serial Monitor.
1#include <WiFi.h>
2#include <Preferences.h>
3#include <time.h>
4
5const char* WIFI_SSID = "AgAdi";
6const char* WIFI_PASS = "Sifre123";
7
8struct Zone { const char* city; const char* tz; };
9const Zone ZONES[] = {
10 { "Istanbul", "<+03>-3" },
11 { "Berlin", "CET-1CEST,M3.5.0,M10.5.0/3" },
12 { "London", "GMT0BST,M3.5.0/1,M10.5.0" },
13 { "New York", "EST5EDT,M3.2.0,M11.1.0" },
14 { "Los Angeles", "PST8PDT,M3.2.0,M11.1.0" },
15 { "Mexico City", "CST6" },
16 { "Sydney", "AEST-10AEDT,M10.1.0,M4.1.0/3" },
17};
18const int ZONE_COUNT = sizeof(ZONES) / sizeof(ZONES[0]);
19
20Preferences prefs;
21int zoneIndex = 0;
22
23void applyZone(int i) {
24 zoneIndex = i;
25 setenv("TZ", ZONES[i].tz, 1); // yeni kural / new rule
26 tzset(); // hemen geçerli olsun / take effect now
27 prefs.putInt("zone", i); // kalıcı belleğe yaz / save to flash (NVS)
28 Serial.printf("Saat dilimi / time zone: %s\n", ZONES[i].city);
29}
30
31void setup() {
32 Serial.begin(115200);
33 prefs.begin("clock", false);
34 int saved = prefs.getInt("zone", 0); // yoksa İstanbul / Istanbul if nothing saved
35 if (saved < 0 || saved >= ZONE_COUNT) saved = 0;
36
37 WiFi.begin(WIFI_SSID, WIFI_PASS);
38 while (WiFi.status() != WL_CONNECTED) delay(500);
39 configTzTime(ZONES[saved].tz, "pool.ntp.org");
40 applyZone(saved);
41
42 for (int i = 0; i < ZONE_COUNT; i++) { // menüyü göster / show the menu
43 Serial.printf("%d = %s\n", i, ZONES[i].city);
44 }
45}
46
47void loop() {
48 if (Serial.available()) { // Seri Monitör'den 0-6 yaz / type 0-6 in the Serial Monitor
49 char c = Serial.read();
50 if (c >= '0' && c < '0' + ZONE_COUNT) applyZone(c - '0');
51 }
52 static unsigned long last = 0;
53 if (millis() - last >= 1000) {
54 last = millis();
55 struct tm t;
56 if (getLocalTime(&t, 0)) {
57 Serial.print(&t, "%H:%M:%S %Z ");
58 Serial.println(ZONES[zoneIndex].city);
59 }
60 }
61}Notice that the time zone list is made of plain strings only; adding a city means adding one more line to the array. If the saved value is corrupt or outside the list, the code falls back to Istanbul, so the device never starts with an undefined rule. You can hook the same structure up to a drop-down menu on a web page, a rotary encoder or two buttons. Changing the selection does not trigger a new NTP sync and not even a second is lost, because the ESP32 keeps a single UTC clock and every city is just a different view of it.
Daylight saving time on the Raspberry Pi Pico W
The time module in MicroPython doesn't understand TZ strings; ntptime.settime() sets the internal clock to UTC and leaves the rest to you. So on the Pico W you calculate DST yourself with a small function: find that year's switch moments in UTC and check whether the current time falls in that range. The code below contains the European rules (last Sunday of March and October, 01:00 UTC) and the US Eastern rules (2nd Sunday of March, 1st Sunday of November, 02:00 local).
1import network
2import ntptime
3import time
4
5SSID = "AgAdi"
6PASS = "Sifre123"
7ZONE = "EU" # "EU" = Berlin/Paris/Roma/Madrid, "US" = New York
8
9def last_sunday(year, month): # ayın son pazarı / last Sunday of the month
10 t = time.mktime((year, month, 31, 12, 0, 0, 0, 0))
11 wd = time.localtime(t)[6] # 0 = Pazartesi ... 6 = Pazar / 0 = Monday ... 6 = Sunday
12 return 31 - (wd + 1) % 7
13
14def nth_sunday(year, month, n): # ayın n. pazarı / n-th Sunday of the month
15 wd = time.localtime(time.mktime((year, month, 1, 12, 0, 0, 0, 0)))[6]
16 return 1 + (6 - wd) % 7 + 7 * (n - 1)
17
18def local_time(utc): # UTC saniyesinden yerel saate / UTC seconds to local time
19 year = time.localtime(utc)[0]
20 if ZONE == "EU": # AB: geçiş 01:00 UTC'de / EU: switch at 01:00 UTC
21 std = 1
22 start = time.mktime((year, 3, last_sunday(year, 3), 1, 0, 0, 0, 0))
23 end = time.mktime((year, 10, last_sunday(year, 10), 1, 0, 0, 0, 0))
24 else: # ABD Doğu: 02:00 yerel saatte / US Eastern: at 02:00 local
25 std = -5
26 start = time.mktime((year, 3, nth_sunday(year, 3, 2), 7, 0, 0, 0, 0)) # 02:00 EST = 07:00 UTC
27 end = time.mktime((year, 11, nth_sunday(year, 11, 1), 6, 0, 0, 0, 0)) # 02:00 EDT = 06:00 UTC
28 dst = start <= utc < end
29 return time.localtime(utc + (std + (1 if dst else 0)) * 3600), dst
30
31wlan = network.WLAN(network.STA_IF)
32wlan.active(True)
33wlan.connect(SSID, PASS)
34while not wlan.isconnected(): # bağlanana kadar bekle / wait until connected
35 time.sleep(0.5)
36
37for attempt in range(5): # NTP bazen zaman aşımına uğrar / NTP may time out
38 try:
39 ntptime.settime() # dahili RTC'yi UTC'ye kurar / sets the internal RTC to UTC
40 break
41 except OSError:
42 time.sleep(2)
43
44while True:
45 t, dst = local_time(time.time())
46 print("{:02d}.{:02d}.{} {:02d}:{:02d}:{:02d} {}".format(t[2], t[1], t[0], t[3], t[4], t[5], "DST" if dst else "STD"))
47 time.sleep(1)The last_sunday function checks which weekday the 31st falls on and counts back; in MicroPython 0 is Monday and 6 is Sunday, so the math uses (day + 1) % 7. Comparing in UTC seconds avoids confusion during the autumn hour that happens twice in local time. We covered Wi-Fi and a web server on the Pico W in Pico W Wi-Fi Web Server.
Project ideas that need the right time
- A word clock for a relative in Europe that switches to summer time by itself; see ESP32 Word Clock for the letter layout.
- A world clock showing several cities at once: each line calls localtime with a different TZ string.
- An irrigation timer that opens the water at 07:00 and stays correct during summer time.
- A data logger that keeps going with an RTC module when the internet drops and stores UTC in the RTC.
- A battery-powered sensor that wakes up every hour to measure; set the sleep time with ESP32 Deep Sleep.
Common mistakes and how to fix them
| Symptom | Cause | Fix |
|---|---|---|
| Clock exactly 6 hours behind | +3 written for UTC+3 (Turkey) | Reverse the sign: <+03>-3 |
| 1 hour behind in summer, right in winter | configTime(3600, 0, ...) used | Use configTzTime and a full TZ string |
| 1 hour ahead in winter | A fixed 3600 DST offset passed to configTime | Use a string with the M rule instead of a fixed offset |
| Clock shows 1970 | NTP hasn't arrived yet | Wait until getLocalTime returns true, check Wi-Fi |
| 1 hour ahead in Mexico in summer | Pre-2022 string CST6CDT used | Use CST6 |
| Task ran twice in autumn | The hour that was set back happened twice | Limit the task to once per day with tm_yday |
Once the right TZ string is in place, the ESP32 shows the correct time for years. In the BlueGrays app you can open ESP32 NTP clock, RTC digital clock and temperature logger projects with step-by-step wiring diagrams and ready code, and add the time zone line from this article to your own project.
Frequently asked questions
Does the ESP32 switch to daylight saving time automatically?
Yes, if you pass a POSIX TZ string that includes a DST rule to configTzTime. For Germany, the string CET-1CEST,M3.5.0,M10.5.0/3 moves the clock forward and back on the last Sundays of March and October by itself. Passing fixed seconds to configTime does not give you automatic switching.
What is the TZ string for New York on the ESP32?
Use EST5EDT,M3.2.0,M11.1.0. It means UTC-5 in winter and UTC-4 from the second Sunday of March to the first Sunday of November, switching at 02:00 local time. The same rule with CST6CDT, MST7MDT or PST8PDT covers Chicago, Denver and Los Angeles.
What is the difference between configTime and configTzTime?
configTime takes the offset and the DST addition as fixed seconds and doesn't know when DST starts or ends. configTzTime takes a full TZ string and calculates the switch dates every year by itself. In countries with daylight saving you should use configTzTime.
Why is my ESP32 clock one hour off?
Usually DST was added as a fixed value or not added at all. Check the TZ string, make sure the sign is reversed and use configTzTime instead of configTime. If the country's rule changed recently (like Mexico in 2022), an old string causes this offset too.
How can I test the daylight saving change on an ESP32?
Set the system clock with settimeofday to a UTC time a few seconds before the switch and print localtime every second. For Europe, at 01:00 UTC on 28 March 2027 you should see the clock jump from 01:59:59 CET to 03:00:00 CEST. No Wi-Fi is needed for this test.
How do I set the time zone on a Raspberry Pi Pico W?
MicroPython doesn't support TZ strings, and ntptime sets the clock to UTC. To get local time you add the offset to UTC, and for DST you write a small function that works out that year's switch days, such as the last Sunday of March.
Projects to build after reading this
See all projects
Real-Time Clock from the Internet via ESP32 NTP
The ESP32 connects to Wi-Fi and fetches the real time from NTP (Network Time Protocol) servers with millisecond precision. configTzTime() applies the Turkey time zone (TRT, UTC+3) automatically, and in countries with daylight saving time the DST switch is handled by a POSIX TZ string. The time is printed both to the Serial Monitor and to an optional I2C LCD.

Digital Clock with ESP32 and DS1307 RTC (I2C + LCD)
In this project, we display the time and date on a 16x2 I2C LCD screen using the DS1307 real-time clock (RTC) module. The DS1307 is a battery-backed RTC that maintains the time even if the ESP32 is powered off. Both modules share the same I2C bus (SDA/SCL), allowing us to learn how to control multiple devices using just two signal wires. Through this project, you will gain practical knowledge of the I2C communication protocol, RTC module operation, and dynamic data display on an LCD screen.

ESP32 Wi-Fi STA Connection (with LCD Screen)
In this project, we connect the ESP32 to the home Wi-Fi network in Station (STA) mode in a secure, fault-tolerant manner. We store the SSID and password in a separate secrets.h file to prevent leaking to Git, set up an auto-reconnect mechanism, and display the RSSI signal level and IP address on an I2C LCD screen. The internal LED works as a status indicator. It is a production-grade connection manager.

ESP32 HTTPClient + Weather API (with SSD1306 OLED Display)
In this project the ESP32 makes a REST API call over the internet to a weather API (wttr.in) using HTTPClient.h. Unlike the I2C LCD version, it uses an SSD1306 OLED (128×64) display, with large text and vector icons for a richer look. Seven weather icons (sunny, rainy, snowy and more) are drawn on the fly, with no bitmap files.

ESP32 HTTPClient + Weather API (with LCD Screen)
In this project the ESP32 makes a REST API call over the internet to a weather API (wttr.in) using HTTPClient.h. The JSON response is parsed with ArduinoJson v7, and the city, temperature, humidity, weather and wind are shown on an I2C LCD. wttr.in needs no API key; just type the name of any city.

ESP32 Deep Sleep — Battery Saving (OLED + RTC RAM Counter)
The ESP32's Deep Sleep mode shuts down the CPU and most peripherals, cutting current draw to about 10µA, which is vital for battery-powered IoT devices. This project uses three wake-up sources: a timer (15 s), an external pin (EXT0, the button on GPIO 33) and a touch pin (T0/GPIO 4). Counters marked with RTC_DATA_ATTR survive deep sleep.

ESP32 Temperature Data Logger (DHT22 + DS1307 RTC + microSD + OLED)
Build a timed temperature and humidity logger with the ESP32! A DHT22 sensor reads temperature and humidity once a minute, and each reading is saved to a microSD card in CSV format with a real-time stamp from the DS1307 RTC module. The OLED display shows the latest temperature, humidity, timestamp and total number of records live, in large text.

ESP32 I2C LCD Display (16x2)
In this project we connect a 16x2 character LCD to the ESP32 over I2C and display text. With I2C, just 2 data pins (SDA and SCL) control the screen, instead of the 6+ pins a classic parallel connection wastes. The code prints a fixed message on the first line and a counter that goes up every second on the second. At startup an I2C scanner also prints the display's address to the Serial Monitor (usually 0x27 or 0x3F).
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

