PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Readme.md 7.0KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Arduino Time Library
  2. Time is a library that provides timekeeping functionality for Arduino.
  3. Using the Arduino Library Manager, install "*Time* by *Michael Margolis*".
  4. The code is derived from the Playground DateTime library but is updated
  5. to provide an API that is more flexible and easier to use.
  6. A primary goal was to enable date and time functionality that can be used with
  7. a variety of external time sources with minimum differences required in sketch logic.
  8. Example sketches illustrate how similar sketch code can be used with: a Real Time Clock,
  9. internet NTP time service, GPS time data, and Serial time messages from a computer
  10. for time synchronization.
  11. ## Functionality
  12. To use the Time library in an Arduino sketch, include TimeLib.h.
  13. ```c
  14. #include <TimeLib.h>
  15. ```
  16. The functions available in the library include
  17. ```c
  18. hour(); // the hour now (0-23)
  19. minute(); // the minute now (0-59)
  20. second(); // the second now (0-59)
  21. day(); // the day now (1-31)
  22. weekday(); // day of the week (1-7), Sunday is day 1
  23. month(); // the month now (1-12)
  24. year(); // the full four digit year: (2009, 2010 etc)
  25. ```
  26. there are also functions to return the hour in 12-hour format
  27. ```c
  28. hourFormat12(); // the hour now in 12 hour format
  29. isAM(); // returns true if time now is AM
  30. isPM(); // returns true if time now is PM
  31. now(); // returns the current time as seconds since Jan 1 1970
  32. ```
  33. The time and date functions can take an optional parameter for the time. This prevents
  34. errors if the time rolls over between elements. For example, if a new minute begins
  35. between getting the minute and second, the values will be inconsistent. Using the
  36. following functions eliminates this problem
  37. ```c
  38. time_t t = now(); // store the current time in time variable t
  39. hour(t); // returns the hour for the given time t
  40. minute(t); // returns the minute for the given time t
  41. second(t); // returns the second for the given time t
  42. day(t); // the day for the given time t
  43. weekday(t); // day of the week for the given time t
  44. month(t); // the month for the given time t
  45. year(t); // the year for the given time t
  46. ```
  47. Functions for managing the timer services are:
  48. ```c
  49. setTime(t); // set the system time to the give time t
  50. setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr
  51. // (2010 or 10 sets year to 2010)
  52. adjustTime(adjustment); // adjust system time by adding the adjustment value
  53. timeStatus(); // indicates if time has been set and recently synchronized
  54. // returns one of the following enumerations:
  55. timeNotSet // the time has never been set, the clock started on Jan 1, 1970
  56. timeNeedsSync // the time had been set but a sync attempt did not succeed
  57. timeSet // the time is set and is synced
  58. ```
  59. Time and Date values are not valid if the status is `timeNotSet`. Otherwise, values can be used but
  60. the returned time may have drifted if the status is `timeNeedsSync`.
  61. ```c
  62. setSyncProvider(getTimeFunction); // set the external time provider
  63. setSyncInterval(interval); // set the number of seconds between re-sync
  64. ```
  65. There are many convenience macros in the `time.h` file for time constants and conversion
  66. of time units.
  67. To use the library, copy the download to the Library directory.
  68. ## Examples
  69. The Time directory contains the Time library and some example sketches
  70. illustrating how the library can be used with various time sources:
  71. - `TimeSerial.pde` shows Arduino as a clock without external hardware.
  72. It is synchronized by time messages sent over the serial port.
  73. A companion Processing sketch will automatically provide these messages
  74. if it is running and connected to the Arduino serial port.
  75. - `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above.
  76. Short (3 characters) and long strings are available to print the days of
  77. the week and names of the months.
  78. - `TimeRTC` uses a DS1307 real-time clock to provide time synchronization.
  79. The basic [DS1307RTC library][1] must be downloaded and installed,
  80. in order to run this sketch.
  81. - `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock.
  82. - `TimeRTCLog` demonstrates how to calculate the difference between times.
  83. It is a very simple logger application that monitors events on digital pins
  84. and prints (to the serial port) the time of an event and the time period since
  85. the previous event.
  86. - `TimeNTP` uses the Arduino Ethernet shield to access time using the internet NTP time service.
  87. The NTP protocol uses UDP and the UdpBytewise library is required, see:
  88. <http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/>
  89. - `TimeGPS` gets time from a GPS.
  90. This requires the TinyGPS library from Mikal Hart:
  91. <http://arduiniana.org/libraries/TinyGPS>
  92. ## Differences
  93. Differences between this code and the playground DateTime library
  94. although the Time library is based on the DateTime codebase, the API has changed.
  95. Changes in the Time library API:
  96. - time elements are functions returning `int` (they are variables in DateTime)
  97. - Years start from 1970
  98. - days of the week and months start from 1 (they start from 0 in DateTime)
  99. - DateStrings do not require a separate library
  100. - time elements can be accessed non-atomically (in DateTime they are always atomic)
  101. - function added to automatically sync time with external source
  102. - `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime`
  103. ## Technical Notes
  104. Internal system time is based on the standard Unix `time_t`.
  105. The value is the number of seconds since Jan 1, 1970.
  106. System time begins at zero when the sketch starts.
  107. The internal time can be automatically synchronized at regular intervals to an external time source.
  108. This is enabled by calling the `setSyncProvider(provider)` function - the provider argument is
  109. the address of a function that returns the current time as a `time_t`.
  110. See the sketches in the examples directory for usage.
  111. The default interval for re-syncing the time is 5 minutes but can be changed by calling the
  112. `setSyncInterval(interval)` method to set the number of seconds between re-sync attempts.
  113. The Time library defines a structure for holding time elements that is a compact version of the C `tm` structure.
  114. All the members of the Arduino `tm` structure are bytes and the year is offset from 1970.
  115. Convenience macros provide conversion to and from the Arduino format.
  116. Low-level functions to convert between system time and individual time elements are provided:
  117. ```c
  118. breakTime(time, &tm); // break time_t into elements stored in tm struct
  119. makeTime(&tm); // return time_t from elements stored in tm struct
  120. ```
  121. This [DS1307RTC library][1] provides an example of how a time provider
  122. can use the low-level functions to interface with the Time library.
  123. [1]:<https://github.com/PaulStoffregen/DS1307RTC>