SystemClock.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SystemClock.h
8  *
9  ****/
10 
15 #pragma once
16 
17 #include "DateTime.h"
18 #include "WString.h"
19 
24 enum TimeZone {
25  eTZ_UTC = 0,
26  eTZ_Local = 1
27 };
35 {
36 public:
52  using CheckTimeZoneOffset = Delegate<void(time_t systemTime)>;
53 
58  time_t now(TimeZone timeType = eTZ_Local) const;
59 
68  bool setTime(time_t time, TimeZone timeType);
69 
75 
80  void setTimeZoneOffset(int seconds)
81  {
82  zoneInfo.offsetMins = seconds / SECS_PER_MIN;
83  }
84 
91  void setTimeZone(float localTimezoneOffset)
92  {
93  setTimeZoneOffset(localTimezoneOffset * SECS_PER_HOUR);
94  }
95 
99  void setTimeZone(const DateTime::ZoneInfo& zoneInfo)
100  {
101  this->zoneInfo = zoneInfo;
102  }
103 
108  {
109  return zoneInfo;
110  }
111 
115  int getTimeZoneOffset() const
116  {
117  return zoneInfo.offsetSecs();
118  }
119 
123  bool isSet() const
124  {
125  return timeSet;
126  }
127 
133  {
134  checkTimeZoneOffset = callback;
135  }
136 
137 private:
138  CheckTimeZoneOffset checkTimeZoneOffset;
139  DateTime::ZoneInfo zoneInfo;
140  bool timeSet = false;
141 };
142 
150 
The String class.
Definition: WString.h:133
Definition: SystemClock.h:35
time_t now(TimeZone timeType=eTZ_Local) const
Get the current date and time.
const DateTime::ZoneInfo & getTimeZone() const
Get current time zone information.
Definition: SystemClock.h:107
void onCheckTimeZoneOffset(CheckTimeZoneOffset callback)
Set/clear a callback which is invoked whenever local time is queried.
Definition: SystemClock.h:132
Delegate< void(time_t systemTime)> CheckTimeZoneOffset
Optional callback which can be used to automate handling of timezone changes.
Definition: SystemClock.h:52
void setTimeZoneOffset(int seconds)
Sets the local time zone offset.
Definition: SystemClock.h:80
String getSystemTimeString(TimeZone timeType=eTZ_Local) const
Get current time as a string.
void setTimeZone(float localTimezoneOffset)
Set the local time zone offset in hours.
Definition: SystemClock.h:91
int getTimeZoneOffset() const
Get the current time zone offset.
Definition: SystemClock.h:115
bool isSet() const
Determine if setTime() has been called yet.
Definition: SystemClock.h:123
void setTimeZone(const DateTime::ZoneInfo &zoneInfo)
Set current time zone information.
Definition: SystemClock.h:99
bool setTime(time_t time, TimeZone timeType)
Set the system clock's time.
TimeZone
Time zones.
Definition: SystemClock.h:24
@ eTZ_UTC
Use universal time coordinate (UTC)
Definition: SystemClock.h:25
@ eTZ_Local
Use local time.
Definition: SystemClock.h:26
#define SECS_PER_HOUR
Definition: DateTime.h:26
#define SECS_PER_MIN
Definition: DateTime.h:25
SystemClockClass SystemClock
Global instance of system clock object.
Time< T > time(Unit unit, T value)
Helper function to create a Time and deduce the type.
Definition: NanoTime.h:432
Basic information required when displaying or handling local times.
Definition: DateTime.h:144
int16_t offsetMins
Offset from UTC in minutes.
Definition: DateTime.h:167
int offsetSecs() const
Get the offset in seconds so it can be added/subtracted directly from a time_t value.
Definition: DateTime.h:173