Legacy Platform Help Articles
Timezone / Location
Device can request advanced timezone / location information:
1
Timezone: Europe/Kiev
2
ISO-8601 time: 2020-10-06T10:16:26+03:00
3
Unix time (UTC): 1601968586694
4
TZ offset: 120 minutes
5
DST offset: 60 minutes
6
POSIX TZ rule: EET-2EEST,M3.5.0/3,M10.5.0/4
7
Next offset changes: 120min. on 1603587600, 180min. on 1616893200
8
Location: 41.941692,-88.390674
Copied!
Example:
1
BLYNK_CONNECTED() {
2
// Send requests for different internal data
3
// Request what is actually needed for your use-case
4
Blynk.sendInternal("utc", "tz_name"); // Name of timezone
5
Blynk.sendInternal("utc", "iso"); // ISO-8601 formatted time
6
Blynk.sendInternal("utc", "time"); // Unix timestamp (with msecs)
7
Blynk.sendInternal("utc", "tz"); // Timezone and DST offsets
8
Blynk.sendInternal("utc", "tz_rule"); // POSIX TZ rule
9
Blynk.sendInternal("utc", "dst_next"); // Up to 2 next time offset changes (due to DST)
10
}
11
12
// Receive UTC data
13
BLYNK_WRITE(InternalPinUTC) {
14
String cmd = param[0].asStr();
15
if (cmd == "time") {
16
uint64_t utc_time = param[1].asLongLong();
17
Serial.print("Unix time (UTC): "); Serial.println(utc_time);
18
19
} else if (cmd == "iso") {
20
String iso_time = param[1].asStr();
21
Serial.print("ISO-8601 time: "); Serial.println(iso_time);
22
23
} else if (cmd == "tz") {
24
long tz_offset = param[1].asInt();
25
long dst_offset = param[2].asInt();
26
Serial.print("TZ offset: "); Serial.print(tz_offset); Serial.println(" minutes");
27
Serial.print("DST offset: "); Serial.print(dst_offset); Serial.println(" minutes");
28
29
} else if (cmd == "tz_name") {
30
String tz_name = param[1].asStr();
31
Serial.print("Timezone: "); Serial.println(tz_name);
32
33
} else if (cmd == "tz_rule") {
34
String tz_rule = param[1].asStr();
35
Serial.print("POSIX TZ rule: "); Serial.println(tz_rule);
36
37
} else if (cmd == "dst_next") {
38
uint32_t next1_ts = param[1].asLong();
39
int next1_off = param[2].asInt();
40
41
uint32_t next2_ts = param[3].asLong();
42
int next2_off = param[4].asInt();
43
44
Serial.print("Next offset changes: ");
45
Serial.print(next1_off); Serial.print("min. on "); Serial.print(next1_ts);
46
Serial.print(", ");
47
Serial.print(next2_off); Serial.print("min. on "); Serial.print(next2_ts);
48
Serial.println();
49
}
50
}
Copied!

Tracking local time

It's a common requirement for IoT devices to keep track of local device time. For this, you need to apply the timezone offset, taking into account the Daylight Saving rules. Luckily, there are many ways to implement it, and one of the easiest is to use the ezTime library:
1
#include <ezTime.h>
2
3
Timezone local;
4
BlynkTimer timer;
5
6
BLYNK_CONNECTED() {
7
Blynk.sendInternal("utc", "time"); // Unix timestamp (with msecs)
8
Blynk.sendInternal("utc", "tz_rule"); // POSIX TZ rule
9
}
10
11
BLYNK_WRITE(InternalPinUTC) {
12
String cmd = param[0].asStr();
13
if (cmd == "time") {
14
const uint64_t utc_time = param[1].asLongLong();
15
UTC.setTime(utc_time / 1000, utc_time % 1000);
16
Serial.print("Unix time (UTC): "); Serial.println(utc_time);
17
} else if (cmd == "tz_rule") {
18
String tz_rule = param[1].asStr();
19
local.setPosix(tz_rule);
20
Serial.print("POSIX TZ rule: "); Serial.println(tz_rule);
21
}
22
}
23
24
void printClock() {
25
Serial.println("Time: " + local.dateTime());
26
}
27
28
void setup() {
29
// Init Blynk, etc...
30
31
timer.setInterval(10000, printClock);
32
}
33
34
void loop() {
35
Blynk.run();
36
timer.run();
37
}
Copied!
This way, the clock is always running in UTC, but local time is always computed according to the Timezone/DST rule. It works event if device goes offline temporarily.
Copy link