C++ library with client side of WorldTimeAPI.
WorldTimeAPI is a simple web service which returns the current local time for a given timezone as either plain-text or JSON.
- Windows
- Linux
- Mac OS
- ESP8266 with arduino core
- ESP32 with arduino core
There are three main functions:
getByIP()which retrieves time and time zone informations from specified public IP address (only IPv4). If IP address is not specified, operation will be done for your public IP address.getByTimeZone()which retrieves time and time zone informations by specified olson time zone name.getListOfTimeZones()which gets list of all supported olson time zone names. Those functions are blocking, so code is stopped until response from API is received. On ESP32 and ESP8266 there is 1s timeout for receiving response.
This library uses multiplatform DateTimeLib library for C++. It has to be included to your project/solution.
Example for Windows, Linux and Mac OS:
#include <iostream>
#include <thread>
#include "DateTime.h"
#include "WorldTimeAPI.h"
int main()
{
WorldTimeAPI api;
auto res = api.getByIP(); //Gets current time and time zone based on your public IP address.
while (true) {
std::cout << res.datetime.toString() << std::endl; //Print current time obtained from WorldTimeAPI
//Accuracy of time obtained from WorldTimeAPI is usually ±1 second.
std::this_thread::sleep_for(std::chrono::seconds(1)); //Wait for one second
}
return 0;
}
ESP32 and ESP8266 examples are located in examples folder.
Thanks to author of WorldTimeAPI for his brilliant idea.
Copyright © 2022 Matej Fitoš.
This project is MIT licensed.