All Projects → JChristensen → Timezone

JChristensen / Timezone

Licence: gpl-3.0
Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments.

Projects that are alternatives of or similar to Timezone

Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (-41.46%)
Mutual labels:  arduino, time, arduino-library
Arduino Applemidi Library
Send and receive MIDI messages over Ethernet (rtpMIDI or AppleMIDI)
Stars: ✭ 177 (-13.66%)
Mutual labels:  arduino, arduino-library
Packetserial
An Arduino Library that facilitates packet-based serial communication using COBS or SLIP encoding.
Stars: ✭ 177 (-13.66%)
Mutual labels:  arduino, arduino-library
Dsckeybusinterface
An Arduino/esp8266/esp32 library to directly interface with DSC security systems.
Stars: ✭ 202 (-1.46%)
Mutual labels:  arduino, arduino-library
Bh1750
An Arduino library for the digital light sensor breakout boards containing the BH1750FVI IC
Stars: ✭ 173 (-15.61%)
Mutual labels:  arduino, arduino-library
Eztime
ezTime — pronounced "Easy Time" — is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, user events, millisecond precision and more.
Stars: ✭ 173 (-15.61%)
Mutual labels:  time, arduino-library
Esp32 Ble Mouse
Bluetooth LE Mouse library for the ESP32 (Arduino IDE compatible)
Stars: ✭ 180 (-12.2%)
Mutual labels:  arduino, arduino-library
Pzem004t
Arduino communication library for Peacefair PZEM-004T Energy monitor
Stars: ✭ 165 (-19.51%)
Mutual labels:  arduino, arduino-library
Corsairlightingprotocol
Control LEDs connected to an Arduino with iCUE, create an unofficial Corsair iCUE compatible Arduino LED controller.
Stars: ✭ 182 (-11.22%)
Mutual labels:  arduino, arduino-library
Duetimer
⏳ Timer Library fully implemented for Arduino DUE
Stars: ✭ 184 (-10.24%)
Mutual labels:  arduino, arduino-library
Md max72xx
LED Matrix Library
Stars: ✭ 186 (-9.27%)
Mutual labels:  arduino, arduino-library
Radio
An Arduino library to control FM radio chips like SI4703, SI4705, RDA5807M, TEA5767.
Stars: ✭ 193 (-5.85%)
Mutual labels:  arduino, arduino-library
Socket.io Client
A socket.io-client implementation for ESP8266 and Arduino
Stars: ✭ 170 (-17.07%)
Mutual labels:  arduino, arduino-library
M5stack Sd Updater
💾 Customizable menu system for M5Stack and ESP32-Chimera-Core - loads apps from the Micro SD card. Easily add you own apps
Stars: ✭ 175 (-14.63%)
Mutual labels:  arduino, arduino-library
Espmqttclient
Wifi and MQTT handling for ESP8266 and ESP32
Stars: ✭ 169 (-17.56%)
Mutual labels:  arduino, arduino-library
Acebutton
An adjustable, compact, event-driven button library for Arduino that debounces and dispatches events to a user-defined event handler.
Stars: ✭ 180 (-12.2%)
Mutual labels:  arduino, arduino-library
Dmxserial
An Arduino library for sending and receiving DMX packets.
Stars: ✭ 190 (-7.32%)
Mutual labels:  arduino, arduino-library
Time
Building a better date/time library for Swift
Stars: ✭ 1,983 (+867.32%)
Mutual labels:  time, timezone
Arduino Menusystem
Arduino library for implementing a menu system
Stars: ✭ 161 (-21.46%)
Mutual labels:  arduino, arduino-library
Date
A date and time library based on the C++11/14/17 <chrono> header
Stars: ✭ 2,389 (+1065.37%)
Mutual labels:  time, timezone

Arduino Timezone Library

https://github.com/JChristensen/Timezone
README file
Jack Christensen
Mar 2012

License

Arduino Timezone Library Copyright (C) 2018 Jack Christensen GNU GPL v3.0

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/gpl.html

Introduction

The Timezone library is designed to work in conjunction with the Arduino Time library, which must also be installed on your system. This documentation assumes some familiarity with the Time library.

The primary aim of the Timezone library is to convert Universal Coordinated Time (UTC) to the correct local time, whether it is daylight saving time (a.k.a. summer time) or standard time. The time source could be a GPS receiver, an NTP server, or a Real-Time Clock (RTC) set to UTC. But whether a hardware RTC or other time source is even present is immaterial, since the Time library can function as a software RTC without additional hardware (although its accuracy is dependent on the accuracy of the microcontroller's system clock.)

The Timezone library implements two objects to facilitate time zone conversions:

  • A TimeChangeRule object describes when local time changes to daylight (summer) time, or to standard time, for a particular locale.
  • A Timezone object uses TimeChangeRules to perform conversions and related functions. It can also write its TimeChangeRules to EEPROM, or read them from EEPROM. Multiple time zones can be represented by defining multiple Timezone objects.

Examples

The following example sketches are included with the Timezone library:

  • Clock: A simple self-adjusting clock for a single time zone. TimeChangeRules may be optionally read from EEPROM.
  • HardwareRTC: A self-adjusting clock for one time zone using an external real-time clock, either a DS1307 or DS3231 (e.g. Chronodot) which is set to UTC.
  • WorldClock: A self-adjusting clock for multiple time zones.
  • WriteRules: A sketch to write TimeChangeRules to EEPROM.
  • Change_TZ_1: Changes between time zones by modifying the TimeChangeRules.
  • Change_TZ_2: Changes between time zones by selecting from an array of Timezone objects.

Coding TimeChangeRules

Normally these will be coded in pairs for a given time zone: One rule to describe when daylight (summer) time starts, and one to describe when standard time starts.

As an example, here in the Eastern US time zone, Eastern Daylight Time (EDT) starts on the 2nd Sunday in March at 02:00 local time. Eastern Standard Time (EST) starts on the 1st Sunday in November at 02:00 local time.

Define a TimeChangeRule as follows:

TimeChangeRule myRule = {abbrev, week, dow, month, hour, offset};

Where:

abbrev is a character string abbreviation for the time zone; it must be no longer than five characters.

week is the week of the month that the rule starts.

dow is the day of the week that the rule starts.

hour is the hour in local time that the rule starts (0-23).

offset is the UTC offset in minutes for the time zone being defined.

For convenience, the following symbolic names can be used:

week: First, Second, Third, Fourth, Last
dow: Sun, Mon, Tue, Wed, Thu, Fri, Sat
month: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

For the Eastern US time zone, the TimeChangeRules could be defined as follows:

TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240};  //UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300};   //UTC - 5 hours

Coding Timezone objects

There are three ways to define Timezone objects.

By first defining TimeChangeRules (as above) and giving the daylight time rule and the standard time rule (assuming usEDT and usEST defined as above):
Timezone usEastern(usEDT, usEST);

For a time zone that does not change to daylight/summer time, pass a single rule to the constructor. For example:
Timezone usAZ(usMST, usMST);

By reading rules previously stored in EEPROM. This reads both the daylight and standard time rules previously stored at EEPROM address 100:
Timezone usPacific(100);

Note that TimeChangeRules require 12 bytes of storage each, so the pair of rules associated with a Timezone object requires 24 bytes total. This could possibly change in future versions of the library. The size of a TimeChangeRule can be checked with sizeof(usEDT).

Timezone library methods

Note that the time_t data type is defined by the Arduino Time library <TimeLib.h>. See the Time library documentation here and here for additional details.

time_t toLocal(time_t utc);

Description

Converts the given UTC time to local time, standard or daylight as appropriate.

Syntax

myTZ.toLocal(utc);

Parameters

utc: Universal Coordinated Time (time_t)

Returns

Local time (time_t)

Example
time_t eastern, utc;
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240};  //UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300};   //UTC - 5 hours
Timezone usEastern(usEDT, usEST);
utc = now();	//current time from the Time Library
eastern = usEastern.toLocal(utc);

time_t toLocal(time_t utc, TimeChangeRule **tcr);

Description

As above, converts the given UTC time to local time, and also returns a pointer to the TimeChangeRule that was applied to do the conversion. This could then be used, for example, to include the time zone abbreviation as part of a time display. The caller must take care not to alter the pointed TimeChangeRule, as this will then result in incorrect conversions.

Syntax

myTZ.toLocal(utc, &tcr);

Parameters

utc: Universal Coordinated Time (time_t)
tcr: Address of a pointer to a TimeChangeRule (**TimeChangeRule)

Returns

Local time (time_t)
Pointer to TimeChangeRule (**TimeChangeRule)

Example
time_t eastern, utc;
TimeChangeRule *tcr;
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240};  //UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300};   //UTC - 5 hours
Timezone usEastern(usEDT, usEST);
utc = now();	//current time from the Time Library
eastern = usEastern.toLocal(utc, &tcr);
Serial.print("The time zone is: ");
Serial.println(tcr -> abbrev);

bool utcIsDST(time_t utc);

bool locIsDST(time_t local);

Description

These functions determine whether a given UTC time or a given local time is within the daylight saving (summer) time interval, and return true or false accordingly.

Syntax

utcIsDST(utc);
locIsDST(local);

Parameters

utc: Universal Coordinated Time (time_t)
local: Local Time (time_t)

Returns

true or false (bool)

Example

if (usEastern.utcIsDST(utc)) { /*do something*/ }

void readRules(int address);

void writeRules(int address);

Description

These functions read or write a Timezone object's two TimeChangeRules from or to EEPROM.

Syntax

myTZ.readRules(address);
myTZ.writeRules(address);

Parameters

address: The beginning EEPROM address to write to or read from (int)

Returns

None.

Example

usEastern.writeRules(100); //write rules beginning at EEPROM address 100

void setRules(TimeChangeRule dstStart, TimeChangeRule stdStart);

Description

This function reads or updates the daylight and standard time rules from RAM. Can be used to change TimeChangeRules dynamically while a sketch runs.

Syntax

myTZ.setRules(dstStart, stdStart);

Parameters

dstStart: A TimeChangeRule denoting the start of daylight saving (summer) time.
stdStart: A TimeChangeRule denoting the start of standard time.

Returns

None.

Example
TimeChangeRule EDT = {"EDT", Second, Sun, Mar, 2, -240};
TimeChangeRule EST = {"EST", First, Sun, Nov, 2, -300};
Timezone ET(EDT, EST);
...
tz.setRules(EDT, EST);

time_t toUTC(time_t local);

Description

Converts the given local time to UTC time.

WARNING: This function is provided for completeness, but should seldom be needed and should be used sparingly and carefully.

Ambiguous situations occur after the Standard-to-DST and the DST-to-Standard time transitions. When changing to DST, there is one hour of local time that does not exist, since the clock moves forward one hour. Similarly, when changing to standard time, there is one hour of local time that occurs twice since the clock moves back one hour.

This function does not test whether it is passed an erroneous time value during the Local-to-DST transition that does not exist. If passed such a time, an incorrect UTC time value will be returned.

If passed a local time value during the DST-to-Local transition that occurs twice, it will be treated as the earlier time, i.e. the time that occurs before the transition.

Calling this function with local times during a transition interval should be avoided!

Syntax

myTZ.toUTC(local);

Parameters

local: Local Time (time_t)

Returns

UTC (time_t)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].