Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a8f9be7
Add issue template
PaulStoffregen Mar 15, 2018
8ac0830
Don't define strcpy_P if already defined
PaulStoffregen Jan 28, 2019
50a56f2
Fix error grammar
huyhoquang Jan 28, 2019
6d0fc5e
Merge pull request #113 from huyhqit/fix-grammar
PaulStoffregen Jan 28, 2019
81b3192
strcpy_P not needed for ESP8266/2.5.0-beta2 sdk
Bolukan Mar 14, 2019
b05d455
Merge pull request #120 from Bolukan/patch-1
PaulStoffregen Mar 14, 2019
2c08bcc
Fix bad URL in library.properties
per1234 May 11, 2019
baa403f
Merge pull request #128 from per1234/fix-url
PaulStoffregen May 11, 2019
28f7067
Update DateStrings.cpp
danbates2 May 31, 2019
d4fafb2
Add more explicit comments about Processing to SyncArduinoClock
PaulStoffregen Jun 11, 2019
f9cef7f
Merge pull request #132 from danbates2/patch-1
PaulStoffregen Jun 27, 2019
15af849
Added ESP32
venice1200 Jul 1, 2019
98c6ec9
Merge pull request #136 from venice1200/master
PaulStoffregen Jul 4, 2019
f11f6fc
Improve DateStrings, reply on Arduino.h to define PROGMEM, etc
PaulStoffregen Aug 12, 2019
c0436e6
Bump version to 1.6
PaulStoffregen Dec 11, 2019
0723137
Update Readme.md
pieterbl Feb 10, 2020
d788757
Update Readme.md
pieterbl Feb 10, 2020
286d443
Update Readme.md
pieterbl Feb 10, 2020
7acd244
Several punctuation fixes, add pointer to DS1307RTC lib.
Feb 10, 2020
3d5114d
Fixed typos in TimeLib.h and Time.cpp
Feb 11, 2020
b9be1cb
Merge pull request #143 from pieterbl/master
PaulStoffregen Feb 11, 2020
416d009
Update readme with include header, fixes #101
PaulStoffregen Apr 4, 2020
ebda9bd
Update readme with specific info about Arduino Library Manager
PaulStoffregen Apr 4, 2020
d1e7638
Added includes to properties file
majenkotech Apr 25, 2020
ece49d7
Merge pull request #148 from majenkotech/master
PaulStoffregen Apr 27, 2020
410aee4
Add deprecated Time.h warning
PaulStoffregen May 5, 2020
6dccf93
Update Time.cpp
cyberman54 May 3, 2021
fa4535e
Update TimeLib.h
cyberman54 May 3, 2021
58bb5cd
Update DateStrings.cpp
cyberman54 May 3, 2021
ae9fb0a
Rename Time.cpp to microTime.cpp
cyberman54 May 3, 2021
dd135e9
Rename TimeLib.h to microTimeLib.h
cyberman54 May 3, 2021
91ee40d
Rename microTimeLib.h to microTime.h
cyberman54 May 3, 2021
e83c24b
Rename microTime.h to microTimeLib.h
cyberman54 May 3, 2021
ca25f95
Update and rename Time.h to microTime.h
cyberman54 May 3, 2021
1f67cdc
Update microTime.cpp
cyberman54 May 3, 2021
348d9f2
Update microTime.h
cyberman54 May 3, 2021
ea3966c
Update and rename microTimeLib.h to TimeLib.h
cyberman54 May 3, 2021
0cb0e1b
Update DateStrings.cpp
cyberman54 May 3, 2021
e368d02
Update microTime.cpp
cyberman54 May 3, 2021
c1f8d29
Update microTime.cpp
cyberman54 May 3, 2021
a9cf966
Update microTime.cpp
cyberman54 May 3, 2021
4f4cd44
Update microTime.cpp
cyberman54 May 3, 2021
093c270
Rename library.json to 0library.json
cyberman54 May 8, 2021
5521af5
Rename library.properties to 0library.properties
cyberman54 May 8, 2021
9b87e13
Update 0library.properties
cyberman54 May 8, 2021
d46e1df
Rename 0library.properties to library.properties
cyberman54 May 8, 2021
89c07dd
Update and rename 0library.json to library.json
cyberman54 May 8, 2021
092106e
Update library.json
cyberman54 May 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions DateStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
*
*/

#if defined(__AVR__)
#include <avr/pgmspace.h>
#else
// for compatiblity with Arduino Due and Teensy 3.0 and maybe others?
#include <Arduino.h>

// Arduino.h should properly define PROGMEM, PGM_P, strcpy_P, pgm_read_byte, pgm_read_ptr
// But not all platforms define these as they should. If you find a platform needing these
// defined, or if any this becomes unnecessary as platforms improve, please send a pull req.
#if defined(ESP8266)
#undef PROGMEM
#define PROGMEM
#define PGM_P const char *
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define pgm_read_word(addr) (*(const unsigned char **)(addr))
#define strcpy_P(dest, src) strcpy((dest), (src))
#endif
#include <string.h> // for strcpy_P or strcpy

#include "TimeLib.h"


// the short strings for each day or month must be exactly dt_SHORT_STR_LEN
#define dt_SHORT_STR_LEN 3 // the length of short strings
Expand Down Expand Up @@ -69,7 +69,7 @@ const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat";

char* monthStr(uint8_t month)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
strcpy_P(buffer, (PGM_P)pgm_read_ptr(&(monthNames_P[month])));
return buffer;
}

Expand All @@ -83,7 +83,7 @@ char* monthShortStr(uint8_t month)

char* dayStr(uint8_t day)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day])));
strcpy_P(buffer, (PGM_P)pgm_read_ptr(&(dayNames_P[day])));
return buffer;
}

Expand Down
44 changes: 27 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Time is a library that provides timekeeping functionality for Arduino.

Using the Arduino Library Manager, install "*Time* by *Michael Margolis*".

The code is derived from the Playground DateTime library but is updated
to provide an API that is more flexible and easier to use.

Expand All @@ -14,6 +16,12 @@ for time synchronization.

## Functionality

To use the Time library in an Arduino sketch, include TimeLib.h.

```c
#include <TimeLib.h>
```

The functions available in the library include

```c
Expand Down Expand Up @@ -66,8 +74,8 @@ timeNeedsSync // the time had been set but a sync attempt did
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.
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
Expand All @@ -89,15 +97,15 @@ illustrating how the library can be used with various time sources:
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 character) and long strings are available to print the days of
- `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.
- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization.
The basic [DS1307RTC library][1] must be downloaded and installed,
in order to run this sketch.

- `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock
- `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
Expand All @@ -106,11 +114,11 @@ illustrating how the library can be used with various time sources:

- `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/
<http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/>

- `TimeGPS` gets time from a GPS
- `TimeGPS` gets time from a GPS.
This requires the TinyGPS library from Mikal Hart:
http://arduiniana.org/libraries/TinyGPS
<http://arduiniana.org/libraries/TinyGPS>

## Differences

Expand All @@ -126,7 +134,7 @@ Changes in the Time library API:
- function added to automatically sync time with external source
- `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime`

## Technical notes:
## Technical Notes

Internal system time is based on the standard Unix `time_t`.
The value is the number of seconds since Jan 1, 1970.
Expand All @@ -140,16 +148,18 @@ 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.
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:
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
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
This [DS1307RTC library][1] provides an example of how a time provider
can use the low-level functions to interface with the Time library.

[1]:<https://github.com/PaulStoffregen/DS1307RTC>
12 changes: 11 additions & 1 deletion TimeLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
typedef unsigned long time_t;
#endif

#define usePPS
#define TIMELIB_ENABLE_MILLIS

// This ugly hack allows us to define C++ overloaded functions, when included
// from within an extern "C", as newlib's sys/stat.h does. Actually it is
Expand Down Expand Up @@ -80,7 +82,7 @@ typedef time_t(*getExternalTime)();
#define elapsedDays(_time_) ((_time_) / SECS_PER_DAY) // this is number of days since Jan 1 1970
#define elapsedSecsToday(_time_) ((_time_) % SECS_PER_DAY) // the number of seconds since last midnight
// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
// Always set the correct time before settting alarms
// Always set the correct time before setting alarms
#define previousMidnight(_time_) (((_time_) / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day
#define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day
#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY)) // note that week starts on day 1
Expand Down Expand Up @@ -118,6 +120,14 @@ int year(); // the full four digit year: (2009, 2010 etc)
int year(time_t t); // the year for the given time

time_t now(); // return the current time as seconds since Jan 1 1970

#ifdef TIMELIB_ENABLE_MILLIS
time_t now(uint32_t& sysTimeMicros); // return the current time as seconds and microseconds since Jan 1 1970
#endif
#ifdef usePPS
void IRAM_ATTR SyncToPPS();
#endif

void setTime(time_t t);
void setTime(int hr,int min,int sec,int day, int month, int yr);
void adjustTime(long adjustment);
Expand Down
64 changes: 64 additions & 0 deletions docs/issue_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Please use this form only to report code defects or bugs.

For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using.

Arduino: forum.arduino.cc
Teensy: forum.pjrc.com
ESP8266: www.esp8266.com
ESP32: www.esp32.com
Adafruit Feather/Metro/Trinket: forums.adafruit.com
Particle Photon: community.particle.io

If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code!

Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software.


----------------------------- Remove above -----------------------------



### Description

Describe your problem.



### Steps To Reproduce Problem

Please give detailed instructions needed for anyone to attempt to reproduce the problem.



### Hardware & Software

Board
Shields / modules used
Arduino IDE version
Teensyduino version (if using Teensy)
Version info & package name (from Tools > Boards > Board Manager)
Operating system & version
Any other software or hardware?


### Arduino Sketch

```cpp
// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <Arduino.h>

// libraries: give links/details so anyone can compile your code for the same result

void setup() {
}

void loop() {
}
```


### Errors or Incorrect Output

If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears.


11 changes: 10 additions & 1 deletion examples/Processing/SyncArduinoClock/SyncArduinoClock.pde
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/**
* SyncArduinoClock.
*
* SyncArduinoClock is a Processing sketch that responds to Arduino
* requests for time synchronization messages. Run this in the
* Processing environment (not in Arduino) on your PC or Mac.
*
* Download TimeSerial onto Arduino and you should see the time
* message displayed when you run SyncArduinoClock in Processing.
* The Arduino time is set from the time on your computer through the
* Processing sketch.
*
* portIndex must be set to the port connected to the Arduino
*
*
* The current time is sent in response to request message from Arduino
* or by clicking the display window
*
Expand Down
10 changes: 4 additions & 6 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
{
"name": "Time",
"name": "microTime",
"description": "Time keeping library",
"keywords": "Time, date, hour, minute, second, day, week, month, year, RTC",
"authors": [
{
"name": "Michael Margolis"
},
{
"name": "Paul Stoffregen",
"email": "paul@pjrc.com",
"url": "http://www.pjrc.com",
"name": "cyberman54",
"maintainer": true
}
],
"repository": {
"type": "git",
"url": "https://github.com/PaulStoffregen/Time"
"url": "https://github.com/cyberman54/microTime"
},
"version": "1.5",
"version": "1.6",
"homepage": "http://playground.arduino.cc/Code/Time",
"frameworks": "Arduino",
"examples": [
Expand Down
9 changes: 5 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name=Time
version=1.5
name=microTime
version=1.6
author=Michael Margolis
maintainer=Paul Stoffregen
maintainer=cyberman54
sentence=Timekeeping functionality for Arduino
paragraph=Date and Time functions, with provisions to synchronize to external time sources like GPS and NTP (Internet). This library is often used together with TimeAlarms and DS1307RTC.
category=Timing
url=http://playground.arduino.cc/code/time
url=http://playground.arduino.cc/Code/Time/
includes=TimeLib.h
architectures=*

43 changes: 30 additions & 13 deletions Time.cpp → microTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <WProgram.h>
#endif

#define TIMELIB_ENABLE_MILLIS
#define usePPS

#include "TimeLib.h"

static tmElements_t tm; // a cache of time elements
Expand Down Expand Up @@ -141,9 +144,9 @@ int year(time_t t) { // the year for the given time

/*============================================================================*/
/* functions to convert to and from system time */
/* These are for interfacing with time serivces and are not normally needed in a sketch */
/* These are for interfacing with time services and are not normally needed in a sketch */

// leap year calulator expects year argument as years offset from 1970
// leap year calculator expects year argument as years offset from 1970
#define LEAP_YEAR(Y) ( ((1970+(Y))>0) && !((1970+(Y))%4) && ( ((1970+(Y))%100) || !((1970+(Y))%400) ) )

static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
Expand Down Expand Up @@ -213,7 +216,7 @@ time_t makeTime(const tmElements_t &tm){
seconds= tm.Year*(SECS_PER_DAY * 365);
for (i = 0; i < tm.Year; i++) {
if (LEAP_YEAR(i)) {
seconds += SECS_PER_DAY; // add extra days for leap years
seconds += SECS_PER_DAY; // add extra days for leap years
}
}

Expand All @@ -235,7 +238,7 @@ time_t makeTime(const tmElements_t &tm){
/* Low level system time functions */

static uint32_t sysTime = 0;
static uint32_t prevMillis = 0;
static uint32_t prevMicros = 0;
static uint32_t nextSyncTime = 0;
static timeStatus_t Status = timeNotSet;

Expand All @@ -246,18 +249,30 @@ getExternalTime getTimePtr; // pointer to external sync function
time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
#endif

#ifdef usePPS
void IRAM_ATTR SyncToPPS() {
sysTime++;
prevMicros = micros();
}
#endif

time_t now() {
// calculate number of seconds passed since last call to now()
while (millis() - prevMillis >= 1000) {
// millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference
sysTime++;
prevMillis += 1000;
uint32_t sysTimeMicros;
return now(sysTimeMicros);
}

time_t now(uint32_t& sysTimeMicros) {
// calculate number of seconds passed since last call to now()
uint32_t n_secs = (micros() - prevMicros) / 1000000U;
sysTime += n_secs;
prevMicros += n_secs * 1000000U;

#ifdef TIME_DRIFT_INFO
sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift
sysUnsyncedTime++; // this can be compared to the synced time to measure long
// term drift
#endif
}
if (nextSyncTime <= sysTime) {

if (sysTime - nextSyncTime < 2147483648U) {
if (getTimePtr != 0) {
time_t t = getTimePtr();
if (t != 0) {
Expand All @@ -280,7 +295,9 @@ void setTime(time_t t) {
sysTime = (uint32_t)t;
nextSyncTime = (uint32_t)t + syncInterval;
Status = timeSet;
prevMillis = millis(); // restart counting from now (thanks to Korman for this fix)
#ifndef usePPS
prevMicros = micros(); // restart counting from now (thanks to Korman for this fix)
#endif
}

void setTime(int hr,int min,int sec,int dy, int mnth, int yr){
Expand Down
File renamed without changes.