2020-02-09 23:50:33 +01:00
# Time/NTP Module
2018-11-26 21:03:24 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 21:19:03 +01:00
| 2018-11-25 | [Skirmantas Lauzikas ](https://github.com/x-logLT ) | [Skirmantas Lauzikas ](https://github.com/x-logLT ) | [time.c ](../../components/modules/time.c )|
2018-11-26 21:03:24 +01:00
2020-02-09 23:50:33 +01:00
This module offers facilities for converting between Unix time and calendar, setting/getting system time, locale and control of NTP client.
2018-11-26 21:03:24 +01:00
2020-02-09 23:50:33 +01:00
## time.cal2epoch()
Converts calendar table to a timestamp in Unix epoch
2018-11-26 21:03:24 +01:00
#### Syntax
2020-02-09 23:50:33 +01:00
`time.cal2epoch(calendar)`
#### Parameters
- `calendar` Table containing calendar info.
- `year` 1970 ~ 2038
- `mon` month 1 ~ 12 in current year
- `day` day 1 ~ 31 in current month
- `hour`
- `min`
- `sec`
#### Returns
number of seconds since the Epoch
#### Example
```lua
calendar={}
calendar.year = 2018-11-20
calendar.mon = 11
calendar.day = 20
calendar.hour = 1
calendar.min = 40
calendar.sec = 50
timestamp = time.cal2epoch(calendar)
time.set(timestamp)
```
## time.epoch2cal()
Converts timestamp in Unix epoch to calendar format
#### Syntax
Fix bit, bthci, can, encoder, eth, i2s docs (#3432)
* Fix bit, bthci, can, encoder, eth, i2s docs
* Fix bit, mqtt, qrcodegen, sigma-delta, sodium, time docs.
* Add object name in http.md
2021-05-19 21:02:49 +02:00
`time.epoch2cal(time)`
2018-11-26 21:03:24 +01:00
#### Parameters
- `time` number of seconds since the Epoch
#### Returns
2020-02-09 23:50:33 +01:00
A table containing the fields:
- `year` 1970 ~ 2038
- `mon` month 1 ~ 12 in current year
- `day` day 1 ~ 31 in current month
- `hour`
- `min`
- `sec`
- `yday` day 1 ~ 366 in current year
- `wday` day 1 ~ 7 in current weak (Sunday is 1)
- `dst` day time adjustment:
- 1 (DST in effect, i.e. daylight time)
- 0 (DST not in effect, i.e. standard time)
- -1 (Unknown DST status)
2018-11-26 21:03:24 +01:00
#### Example
```lua
2020-02-09 23:50:33 +01:00
--Gets current time calendar format, no locale adjustment
time = time.epoch2cal(time.get())
print(string.format("%04d-%02d-%02d %02d:%02d:%02d DST:%d", time["year"], time["mon"], time["day"], time["hour"], time["min"], time["sec"], time["dst"]))
2018-11-26 21:03:24 +01:00
```
## time.get()
2020-02-09 23:50:33 +01:00
Returns current system time in the Unix epoch (seconds from midnight 1970/01/01).
2018-11-26 21:03:24 +01:00
#### Syntax
Fix bit, bthci, can, encoder, eth, i2s docs (#3432)
* Fix bit, bthci, can, encoder, eth, i2s docs
* Fix bit, mqtt, qrcodegen, sigma-delta, sodium, time docs.
* Add object name in http.md
2021-05-19 21:02:49 +02:00
`time.get()`
2018-11-26 21:03:24 +01:00
#### Parameters
none
#### Returns
A two-value timestamp consisting of:
- `sec` seconds since the Unix epoch
- `usec` the microseconds part
#### Example
```lua
sec, usec = time.get()
```
#### See also
[`time.epch2cal()` ](#timeepoch2cal )
2020-02-09 23:50:33 +01:00
2018-11-26 21:03:24 +01:00
## time.getlocal()
Returns current system time adjusted for the locale in calendar format.
#### Syntax
Fix bit, bthci, can, encoder, eth, i2s docs (#3432)
* Fix bit, bthci, can, encoder, eth, i2s docs
* Fix bit, mqtt, qrcodegen, sigma-delta, sodium, time docs.
* Add object name in http.md
2021-05-19 21:02:49 +02:00
`time.getlocal()`
2018-11-26 21:03:24 +01:00
#### Parameters
none
#### Returns
A table containing the fields:
- `year` 1970 ~ 2038
- `mon` month 1 ~ 12 in current year
- `day` day 1 ~ 31 in current month
- `hour`
- `min`
- `sec`
- `yday` day 1 ~ 366 in current year
- `wday` day 1 ~ 7 in current weak (Sunday is 1)
- `dst` day time adjustment:
- 1 (DST in effect, i.e. daylight time)
- 0 (DST not in effect, i.e. standard time)
- -1 (Unknown DST status)
#### Example
```lua
localTime = time.getlocal()
print(string.format("%04d-%02d-%02d %02d:%02d:%02d DST:%d", localTime["year"], localTime["mon"], localTime["day"], localTime["hour"], localTime["min"], localTime["sec"], localTime["dst"]))
```
## time.initntp()
2020-02-09 23:50:33 +01:00
Initializes and starts NTP client
2018-11-26 21:03:24 +01:00
#### Syntax
`time.initntp([ntpAddr])`
#### Parameters
2020-02-09 23:50:33 +01:00
- `ntpAddr` address of a NTP server, defaults to "pool.ntp.org" if none is specified
2018-11-26 21:03:24 +01:00
#### Returns
`nil`
#### Example
```lua
time.initntp("pool.ntp.org")
```
2020-02-09 23:50:33 +01:00
2018-11-26 21:03:24 +01:00
## time.ntpenabled()
2020-02-09 23:50:33 +01:00
Checks if NTP client is enabled.
2018-11-26 21:03:24 +01:00
#### Syntax
`time.ntpenabled()`
#### Parameters
none
#### Returns
Fix bit, bthci, can, encoder, eth, i2s docs (#3432)
* Fix bit, bthci, can, encoder, eth, i2s docs
* Fix bit, mqtt, qrcodegen, sigma-delta, sodium, time docs.
* Add object name in http.md
2021-05-19 21:02:49 +02:00
`true` if NTP client is enabled.
2020-02-09 23:50:33 +01:00
2018-11-26 21:03:24 +01:00
## time.ntpstop()
2020-02-09 23:50:33 +01:00
Stops NTP client.
2018-11-26 21:03:24 +01:00
#### Syntax
`time.ntpstop()`
#### Parameters
none
#### Returns
`nil`
2020-02-09 23:50:33 +01:00
## time.set()
Sets system time to a given timestamp in the Unix epoch (seconds from midnight 1970/01/01).
2018-11-26 21:03:24 +01:00
#### Syntax
2020-02-09 23:50:33 +01:00
`time.set(time)`
2018-11-26 21:03:24 +01:00
#### Parameters
- `time` number of seconds since the Epoch
#### Returns
2020-02-09 23:50:33 +01:00
`nil`
2018-11-26 21:03:24 +01:00
#### Example
```lua
2020-02-09 23:50:33 +01:00
--set time to 2018-11-20 01:40:50
time.set(1542678050)
2018-11-26 21:03:24 +01:00
```
2020-02-09 23:50:33 +01:00
#### See also
[`time.cal2epoc()` ](#timecal2epoch )
## time.settimezone()
Sets correct format for Time Zone locale
2018-11-26 21:03:24 +01:00
#### Syntax
2020-02-09 23:50:33 +01:00
`time.settimezone(timezone)`
2018-11-26 21:03:24 +01:00
#### Parameters
2020-02-09 23:50:33 +01:00
- `timezone` a string representing timezone, can also include DST adjustment. For full syntax see [TZ variable documentation ](http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html ).
2018-11-26 21:03:24 +01:00
#### Returns
2020-02-09 23:50:33 +01:00
`nil`
2018-11-26 21:03:24 +01:00
#### Example
```lua
2020-02-09 23:50:33 +01:00
--set timezone to Eastern Standard Time
time.settimezone("EST+5")
2018-11-26 21:03:24 +01:00
```