{"id":13780477,"url":"https://github.com/PaulStoffregen/Time","last_synced_at":"2025-05-11T13:32:26.056Z","repository":{"id":20440067,"uuid":"23716898","full_name":"PaulStoffregen/Time","owner":"PaulStoffregen","description":"Time library for Arduino","archived":false,"fork":false,"pushed_at":"2024-08-17T18:05:17.000Z","size":79,"stargazers_count":1258,"open_issues_count":92,"forks_count":668,"subscribers_count":102,"default_branch":"master","last_synced_at":"2024-11-11T16:24:43.651Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://playground.arduino.cc/code/time","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PaulStoffregen.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-09-05T20:12:31.000Z","updated_at":"2024-11-09T09:14:01.000Z","dependencies_parsed_at":"2022-07-26T09:15:05.237Z","dependency_job_id":"b53efffa-1ffd-437d-b732-ac75e02c92d8","html_url":"https://github.com/PaulStoffregen/Time","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulStoffregen%2FTime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulStoffregen%2FTime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulStoffregen%2FTime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulStoffregen%2FTime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PaulStoffregen","download_url":"https://codeload.github.com/PaulStoffregen/Time/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225056828,"owners_count":17414218,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:16.132Z","updated_at":"2024-11-17T15:31:15.108Z","avatar_url":"https://github.com/PaulStoffregen.png","language":"C++","funding_links":[],"categories":["Libraries","C++"],"sub_categories":[],"readme":"# Arduino Time Library\n\nTime is a library that provides timekeeping functionality for Arduino.\n\nUsing the Arduino Library Manager, install \"*Time* by *Michael Margolis*\".\n\nThe code is derived from the Playground DateTime library but is updated\nto provide an API that is more flexible and easier to use.\n\nA primary goal was to enable date and time functionality that can be used with\na variety of external time sources with minimum differences required in sketch logic.\n\nExample sketches illustrate how similar sketch code can be used with: a Real Time Clock,\ninternet NTP time service, GPS time data, and Serial time messages from a computer\nfor time synchronization.\n\n## Functionality\n\nTo use the Time library in an Arduino sketch, include TimeLib.h.\n\n```c\n#include \u003cTimeLib.h\u003e\n```\n\nThe functions available in the library include\n\n```c\nhour();            // the hour now  (0-23)\nminute();          // the minute now (0-59)\nsecond();          // the second now (0-59)\nday();             // the day now (1-31)\nweekday();         // day of the week (1-7), Sunday is day 1\nmonth();           // the month now (1-12)\nyear();            // the full four digit year: (2009, 2010 etc)\n```\n\nthere are also functions to return the hour in 12-hour format\n\n```c\nhourFormat12();    // the hour now in 12 hour format\nisAM();            // returns true if time now is AM\nisPM();            // returns true if time now is PM\n\nnow();             // returns the current time as seconds since Jan 1 1970\n```\n\nThe time and date functions can take an optional parameter for the time. This prevents\nerrors if the time rolls over between elements. For example, if a new minute begins\nbetween getting the minute and second, the values will be inconsistent. Using the\nfollowing functions eliminates this problem\n\n```c\ntime_t t = now(); // store the current time in time variable t\nhour(t);          // returns the hour for the given time t\nminute(t);        // returns the minute for the given time t\nsecond(t);        // returns the second for the given time t\nday(t);           // the day for the given time t\nweekday(t);       // day of the week for the given time t\nmonth(t);         // the month for the given time t\nyear(t);          // the year for the given time t\n```\n\nFunctions for managing the timer services are:\n\n```c\nsetTime(t);                      // set the system time to the give time t\nsetTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr\n                                 // (2010 or 10 sets year to 2010)\nadjustTime(adjustment);          // adjust system time by adding the adjustment value\ntimeStatus();                    // indicates if time has been set and recently synchronized\n                                 // returns one of the following enumerations:\ntimeNotSet                       // the time has never been set, the clock started on Jan 1, 1970\ntimeNeedsSync                    // the time had been set but a sync attempt did not succeed\ntimeSet                          // the time is set and is synced\n```\n\nTime and Date values are not valid if the status is `timeNotSet`. Otherwise, values can be used but\nthe returned time may have drifted if the status is `timeNeedsSync`. \t\n\n```c\nsetSyncProvider(getTimeFunction);  // set the external time provider\nsetSyncInterval(interval);         // set the number of seconds between re-sync\n```\n\nThere are many convenience macros in the `time.h` file for time constants and conversion\nof time units.\n\nTo use the library, copy the download to the Library directory.\n\n## Examples\n\nThe Time directory contains the Time library and some example sketches\nillustrating how the library can be used with various time sources:\n\n- `TimeSerial.pde` shows Arduino as a clock without external hardware.\n  It is synchronized by time messages sent over the serial port.\n  A companion Processing sketch will automatically provide these messages\n  if it is running and connected to the Arduino serial port.\n\n- `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above.\n  Short (3 characters) and long strings are available to print the days of\n  the week and names of the months.\n\n- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization.\n  The basic [DS1307RTC library][1] must be downloaded and installed,\n  in order to run this sketch.\n\n- `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock.\n\n- `TimeRTCLog` demonstrates how to calculate the difference between times.\n  It is a very simple logger application that monitors events on digital pins\n  and prints (to the serial port) the time of an event and the time period since\n  the previous event.\n\n- `TimeNTP` uses the Arduino Ethernet shield to access time using the internet NTP time service.\n  The NTP protocol uses UDP and the UdpBytewise library is required, see:\n  \u003chttp://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/\u003e\n\n- `TimeGPS` gets time from a GPS.\n  This requires the TinyGPS library from Mikal Hart:\n  \u003chttp://arduiniana.org/libraries/TinyGPS\u003e\n\n## Differences\n\nDifferences between this code and the playground DateTime library\nalthough the Time library is based on the DateTime codebase, the API has changed.\nChanges in the Time library API:\n\n- time elements are functions returning `int` (they are variables in DateTime)\n- Years start from 1970\n- days of the week and months start from 1 (they start from 0 in DateTime)\n- DateStrings do not require a separate library\n- time elements can be accessed non-atomically (in DateTime they are always atomic)\n- function added to automatically sync time with external source\n- `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime`\n\n## Technical Notes\n\nInternal system time is based on the standard Unix `time_t`.\nThe value is the number of seconds since Jan 1, 1970.\nSystem time begins at zero when the sketch starts.\n\nThe internal time can be automatically synchronized at regular intervals to an external time source.\nThis is enabled by calling the `setSyncProvider(provider)` function - the provider argument is\nthe address of a function that returns the current time as a `time_t`.\nSee the sketches in the examples directory for usage.\n\nThe default interval for re-syncing the time is 5 minutes but can be changed by calling the\n`setSyncInterval(interval)` method to set the number of seconds between re-sync attempts.\n\nThe Time library defines a structure for holding time elements that is a compact version of the C `tm` structure.\nAll the members of the Arduino `tm` structure are bytes and the year is offset from 1970.\nConvenience macros provide conversion to and from the Arduino format.\n\nLow-level functions to convert between system time and individual time elements are provided:\n\n```c\nbreakTime(time, \u0026tm);  // break time_t into elements stored in tm struct\nmakeTime(\u0026tm);         // return time_t from elements stored in tm struct\n```\n\nThis [DS1307RTC library][1] provides an example of how a time provider\ncan use the low-level functions to interface with the Time library.\n\n[1]:\u003chttps://github.com/PaulStoffregen/DS1307RTC\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaulStoffregen%2FTime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPaulStoffregen%2FTime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaulStoffregen%2FTime/lists"}