# Arduino Time Library
Time is a library that provides timekeeping functionality for Arduino.
The code is derived from the Playground DateTime library but is updated
to provide an API that is more flexible and easier to use.
A primary goal was to enable date and time functionality that can be used with
a variety of external time sources with minimum differences required in sketch logic.
Example sketches illustrate how similar sketch code can be used with: a Real Time Clock,
internet NTP time service, GPS time data, and Serial time messages from a computer
for time synchronization.
## Functionality
The functions available in the library include
```c
hour(); // the hour now (0-23)
minute(); // the minute now (0-59)
second(); // the second now (0-59)
day(); // the day now (1-31)
weekday(); // day of the week (1-7), Sunday is day 1
month(); // the month now (1-12)
year(); // the full four digit year: (2009, 2010 etc)
```
there are also functions to return the hour in 12-hour format
```c
hourFormat12(); // the hour now in 12 hour format
isAM(); // returns true if time now is AM
isPM(); // returns true if time now is PM
now(); // returns the current time as seconds since Jan 1 1970
```
The time and date functions can take an optional parameter for the time. This prevents
errors if the time rolls over between elements. For example, if a new minute begins
between getting the minute and second, the values will be inconsistent. Using the
following functions eliminates this problem
```c
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
minute(t); // returns the minute for the given time t
second(t); // returns the second for the given time t
day(t); // the day for the given time t
weekday(t); // day of the week for the given time t
month(t); // the month for the given time t
year(t); // the year for the given time t
```
Functions for managing the timer services are:
```c
setTime(t); // set the system time to the give time t
setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr
// (2010 or 10 sets year to 2010)
adjustTime(adjustment); // adjust system time by adding the adjustment value
timeStatus(); // indicates if time has been set and recently synchronized
// returns one of the following enumerations:
timeNotSet // the time has never been set, the clock started on Jan 1, 1970
timeNeedsSync // the time had been set but a sync attempt did not succeed
timeSet // the time is set and is synced
```
Time and Date values are not valid if the status is timeNotSet. Otherwise, values can be used but
the returned time may have drifted if the status is timeNeedsSync.
```c
setSyncProvider(getTimeFunction); // set the external time provider
setSyncInterval(interval); // set the number of seconds between re-sync
```
There are many convenience macros in the `time.h` file for time constants and conversion
of time units.
To use the library, copy the download to the Library directory.
## Examples
The Time directory contains the Time library and some example sketches
illustrating how the library can be used with various time sources:
- `TimeSerial.pde` shows Arduino as a clock without external hardware.
It is synchronized by time messages sent over the serial port.
A companion Processing sketch will automatically provide these messages
if it is running and connected to the Arduino serial port.
- `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above
Short (3 characters) and long strings are available to print the days of
the week and names of the months.
- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization.
A basic RTC library named DS1307RTC is included in the download.
To run this sketch the DS1307RTC library must be installed.
- `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock
- `TimeRTCLog` demonstrates how to calculate the difference between times.
It is a very simple logger application that monitors events on digital pins
and prints (to the serial port) the time of an event and the time period since
the previous event.
- `TimeNTP` uses the Arduino Ethernet shield to access time using the internet NTP time service.
The NTP protocol uses UDP and the UdpBytewise library is required, see:
http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/
- `TimeGPS` gets time from a GPS
This requires the TinyGPS library from Mikal Hart:
http://arduiniana.org/libraries/TinyGPS
## Differences
Differences between this code and the playground DateTime library
although the Time library is based on the DateTime codebase, the API has changed.
Changes in the Time library API:
- time elements are functions returning `int` (they are variables in DateTime)
- Years start from 1970
- days of the week and months start from 1 (they start from 0 in DateTime)
- DateStrings do not require a separate library
- time elements can be accessed non-atomically (in DateTime they are always atomic)
- function added to automatically sync time with external source
- `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime`
## Technical notes:
Internal system time is based on the standard Unix `time_t`.
The value is the number of seconds since Jan 1, 1970.
System time begins at zero when the sketch starts.
The internal time can be automatically synchronized at regular intervals to an external time source.
This is enabled by calling the `setSyncProvider(provider)` function - the provider argument is
the address of a function that returns the current time as a `time_t`.
See the sketches in the examples directory for usage.
The default interval for re-syncing the time is 5 minutes but can be changed by calling the
`setSyncInterval(interval)` method to set the number of seconds between re-sync attempts.
The Time library defines a structure for holding time elements that is a compact version of the C tm structure.
All the members of the Arduino tm structure are bytes and the year is offset from 1970.
Convenience macros provide conversion to and from the Arduino format.
Low level functions to convert between system time and individual time elements are provided:
```c
breakTime(time, &tm); // break time_t into elements stored in tm struct
makeTime(&tm); // return time_t from elements stored in tm struct
```
The DS1307RTC library included in the download provides an example of how a time provider
can use the low-level functions to interface with the Time library.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
嵌入式优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人单片机开发经验充足,深耕嵌入式领域,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明,项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要嵌入式物联网单片机相关领域开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注嵌入式领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【建议小白】: 在所有嵌入式开发中硬件部分若不会画PCB/电路,可选择根据引脚定义将其代替为面包板+杜邦线+外设模块的方式,只需轻松简单连线,下载源码烧录进去便可轻松复刻出一样的项目 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能
资源推荐
资源详情
资源评论
收起资源包目录
基于esp8266+0.96oled的气象站桌面时钟.zip (84个子文件)
Archieabc
烧录配置.png 42KB
54.png 671KB
LICENSE 34KB
4545.png 686KB
图片资源
503_结果.bmp 6KB
999_结果.bmp 6KB
304_结果.bmp 6KB
wxwx.bmp 1KB
400_结果.bmp 6KB
509_结果.bmp 6KB
307_结果.bmp 6KB
407_结果.bmp 6KB
205_结果.bmp 6KB
100_结果.bmp 6KB
102_结果.bmp 6KB
404_结果.bmp 6KB
306_结果.bmp 6KB
401_结果.bmp 6KB
499_结果.bmp 6KB
500_结果.bmp 6KB
103_结果.bmp 6KB
310_结果.bmp 6KB
jg.txt 27KB
301_结果.bmp 6KB
302_结果.bmp 6KB
403_结果.bmp 6KB
104_结果.bmp 6KB
201_结果.bmp 6KB
399_结果.bmp 6KB
101_结果.bmp 6KB
208_结果.bmp 6KB
305_结果.bmp 6KB
esp01s电路.png 22KB
封面.jpg 1.2MB
buid.png 284KB
量产版
1.png 46KB
PCB_2020-07-10_02-44-51_2020-07-10_09-57-35.pcbdoc 57KB
pcb生产文件.zip 38KB
2.png 19KB
uid.png 66KB
源码
qxz_b
zlt.h 32KB
data
config.html 14KB
qxz_b.ino 12KB
qxz
zlt.h 34KB
qxz.ino 13KB
data
index.html 3KB
config.html 14KB
libraries
Time
Time.h 21B
TimeLib.h 6KB
library.json 669B
examples
TimeTeensy3
TimeTeensy3.ino 2KB
TimeRTC
TimeRTC.ino 1KB
TimeNTP_ESP8266WiFi
TimeNTP_ESP8266WiFi.ino 5KB
TimeRTCSet
TimeRTCSet.ino 2KB
TimeSerialDateStrings
TimeSerialDateStrings.ino 3KB
TimeNTP_ENC28J60
TimeNTP_ENC28J60.ino 4KB
Processing
SyncArduinoClock
SyncArduinoClock.pde 2KB
readme.txt 393B
TimeSerial
TimeSerial.ino 2KB
TimeArduinoDue
TimeArduinoDue.ino 2KB
TimeNTP
TimeNTP.ino 4KB
TimeRTCLog
TimeRTCLog.ino 3KB
TimeGPS
TimeGPS.ino 2KB
Time.cpp 9KB
keywords.txt 823B
docs
issue_template.md 2KB
library.properties 384B
Readme.md 7KB
DateStrings.cpp 3KB
.gitignore 270B
PC上位机
1.png 10KB
气象站pc性能监视器上位机.exe 57KB
2.png 140KB
pcb.png 701KB
烧录流程.png 9KB
README.md 4KB
esp01固件
v2.bin 1004KB
v5.bin 1004KB
v1(接口将在2021.12.30日失效).bin 1004KB
v3.bin 1004KB
v4.bin 1004KB
v4a.bin 1004KB
v3b.bin 1004KB
v3bb.png 417KB
共 84 条
- 1
资源评论
阿齐Archie
- 粉丝: 1w+
- 资源: 2303
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功