AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Duane Rice All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer. Redistributions in binary
10 * form must reproduce the above copyright notice, this list of conditions
11 * and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution. Neither the name of the copyright holders
13 * nor the names of contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. */
27
28#ifndef TIME_H
29#define TIME_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#include <stdint.h>
36#include <stdlib.h>
37
38/** \file */
39
40/** \defgroup avr_time <time.h>: Time
41 \code #include <time.h> \endcode
42 <h3>Introduction to the Time functions</h3>
43 This file declares the time functions implemented in AVR-LibC.
44
45 The implementation aspires to conform with ISO/IEC 9899 (C90).
46 However, due to limitations of the target processor and the nature of
47 its development environment, a practical implementation must of
48 necessity deviate from the standard.
49
50 Section 7.23.2.1 \c clock()<br>
51 The type \c clock_t, the macro \c CLOCKS_PER_SEC, and the function
52 \c clock() are not implemented. We consider these items belong to
53 operating system code, or to application code when no operating
54 system is present.
55
56 Section 7.23.2.3 \c mktime()<br>
57 The standard specifies that \c mktime() should return <tt>(time_t) - 1</tt>
58 if the time cannot be represented.
59 This implementation always returns a 'best effort' representation, which
60 means that there are corner cases where non-representable times are not
61 mapped to <tt>-1</tt>. The purpose is to avoid 64-bit arithmetic.
62
63 Section 7.23.2.4 \c time()<br>
64 The standard specifies that \c time() should return <tt>(time_t) - 1</tt>
65 if the time is not available. Since the application must initialize
66 the time system, this functionality is not implemented.
67
68 Section 7.23.2.2, \c difftime()<br>
69 Due to the lack of a 64-bit double, the function difftime() returns a
70 \c long integer. In most cases this change will be invisible to the user,
71 handled automatically by the compiler.
72
73 Section 7.23.1.4 \c struct \c #tm<br>
74 Per the standard, struct <tt>tm->tm_isdst</tt> is greater than zero when
75 Daylight Saving time is in effect.
76 This implementation further specifies that, when positive, the value
77 of \c tm_isdst represents
78 the amount time is advanced during Daylight Saving time.
79
80 Section 7.23.3.5 \c strftime()<br>
81 Only the 'C' locale is supported, therefore the modifiers 'E' and 'O' are
82 ignored. The 'Z' conversion is also ignored, due to the lack of time
83 zone name.
84
85 In addition to the above departures from the standard, there are some
86 behaviors which are different from what is often expected,
87 though allowed under the standard.
88
89 There is no 'platform standard' method to obtain the current time,
90 time zone, or daylight savings 'rules' in the AVR environment.
91 Therefore the application must initialize the time system with this
92 information. The functions \c set_zone(), \c set_dst(), and
93 \c set_system_time() are provided for initialization. Once initialized,
94 system time is maintained by calling the function \c system_tick()
95 at one second intervals.
96
97 Though not specified in the standard, it is often expected that \c time_t
98 is a signed integer representing an offset in seconds from
99 Midnight Jan 1 1970, i.e. 'Unix time'. This implementation uses an
100 unsigned 32-bit integer offset from Midnight Jan 1 2000.
101 The use of this 'epoch' helps to simplify the conversion functions,
102 while the 32-bit value allows time to be properly represented until
103 Tue Feb 7 06:28:15 2136 UTC. The macros \c #UNIX_OFFSET and \c #NTP_OFFSET
104 are defined to assist in converting to and from Unix and NTP time stamps.
105
106 Unlike desktop counterparts, it is impractical to implement or maintain
107 the 'zoneinfo' database.
108 Therefore no attempt is made to account for time zone, daylight saving,
109 or leap seconds in past dates. All calculations are made according to
110 the currently configured time zone and daylight saving 'rule'.
111
112 In addition to C standard functions, re-entrant versions of \c ctime(),
113 \c asctime(), \c gmtime() and \c localtime() are provided which,
114 in addition to being re-entrant, have the property of claiming
115 less permanent storage in RAM. An additional time conversion, \c isotime()
116 and its re-entrant version, uses far less storage than either \c ctime()
117 or \c asctime().
118
119 Along with the usual smattering of utility functions, such as
120 \c is_leap_year(), this library includes
121 a set of functions related the sun and moon, as well as sidereal
122 time functions.
123*/
124
125/** \ingroup avr_time */
126/**@{*/
127
128/**
129 \c time_t represents seconds elapsed from Midnight, Jan 1 2000 UTC
130 (the Y2K 'epoch'). Its range allows this implementation to represent
131 time up to Tue Feb 7 06:28:15 2136 UTC. */
133
134/**
135 The \c time function returns the systems current time stamp.
136 If timer is not a null pointer, the return value is also assigned
137 to the object it points to. */
138time_t time(time_t *timer);
139
140/**
141 The difftime function returns the difference between two binary time stamps,
142 time1 - time0.
143*/
144int32_t difftime(time_t time1, time_t time0);
145
146
147/**
148 The \c tm structure contains a representation of time 'broken down' into
149 components of the Gregorian calendar.
150
151 The value of tm_isdst is zero if Daylight Saving Time is not in effect,
152 and is negative if the information is not available.
153
154 When Daylight Saving Time is in effect, the value represents the number of
155 seconds the clock is advanced.
156
157 See the \c set_dst() function for more information about Daylight Saving.
158*/
159struct tm
160{
161 int8_t tm_sec; /**< Seconds after the minute (0 to 59) */
162 int8_t tm_min; /**< Minutes after the hour (0 to 59) */
163 int8_t tm_hour; /**< Hours since midnight (0 to 23) */
164 int8_t tm_mday; /**< Day of the month (1 to 31) */
165 int8_t tm_wday; /**< Days since Sunday (0 to 6) */
166 int8_t tm_mon; /**< Months since January (0 to 11) */
167 int16_t tm_year; /**< Years since 1900 */
168 int16_t tm_yday; /**< Days since January 1 (0 to 365) */
169 int16_t tm_isdst; /**< Daylight Saving Time flag */
170};
171
172#ifndef __DOXYGEN__
173/* We have to provide \c clock_t / \c CLOCKS_PER_SEC so that libstdc++-v3 can
174 be built. We define \c CLOCKS_PER_SEC via a symbol \c _CLOCKS_PER_SEC_
175 so that the user can provide the value on the link line, which should
176 result in little or no run-time overhead compared with a constant. */
177typedef uint32_t clock_t;
178extern char *_CLOCKS_PER_SEC_;
179#define CLOCKS_PER_SEC ((clock_t) _CLOCKS_PER_SEC_)
180extern clock_t clock(void);
181#endif /* !__DOXYGEN__ */
182
183/**
184 This function 'compiles' the elements of a broken-down time structure,
185 returning a binary time stamp.
186 The elements of timeptr are interpreted as representing Local Time.
187
188 The original values of the \c tm_wday and tm_yday elements of the structure
189 are ignored, and the original values of the other elements are not
190 restricted to the ranges stated for struct tm.
191
192 The element \c tm_isdst is used for input and output. If set to 0 or a
193 positive value on input, this requests calculation for Daylight Savings
194 Time being off or on, respectively. If set to a negative value on input,
195 it requests calculation to return whether Daylight Savings Time is in
196 effect or not according to the other values.
197
198 On successful completion, the values of all elements of timeptr are set
199 to the appropriate range. */
200time_t mktime(struct tm * timeptr);
201
202/**
203 This function 'compiles' the elements of a broken-down time structure,
204 returning a binary time stamp. The elements of \c timeptr are interpreted
205 as representing UTC.
206
207 The original values of the tm_wday and tm_yday elements of the structure
208 are ignored, and the original values of the other elements are not
209 restricted to the ranges stated for struct \c #tm.
210
211 Unlike \c mktime(), this function \e does \e not modify the elements of
212 \c timeptr. */
213time_t mk_gmtime(const struct tm * timeptr);
214
215/**
216 The gmtime function converts the time stamp pointed to by timer into
217 broken-down time, expressed as UTC. */
218struct tm *gmtime(const time_t * timer);
219
220/**
221 Re entrant version of \c gmtime(). */
222void gmtime_r(const time_t * timer, struct tm * timeptr);
223
224/**
225 The localtime function converts the time stamp pointed to by timer
226 into broken-down time, expressed as Local time. */
227struct tm *localtime(const time_t * timer);
228
229/**
230 Re entrant version of \c localtime(). */
231void localtime_r(const time_t * timer, struct tm * timeptr);
232
233/**
234 The asctime function converts the broken-down time of timeptr,
235 into an ASCII string in the form
236 \code Sun Mar 23 01:03:52 2013 \endcode */
237char *asctime(const struct tm * timeptr);
238
239/**
240 Re entrant version of \c asctime().
241*/
242void asctime_r(const struct tm * timeptr, char *buf);
243
244/**
245 The ctime function is equivalent to <tt>asctime(localtime(timer))</tt>. */
246char *ctime(const time_t * timer);
247
248/**
249 Re entrant version of \c ctime(). */
250void ctime_r(const time_t * timer, char *buf);
251
252/**
253The isotime function constructs an ASCII string in the form
254 \code 2013-03-23 01:03:52 \endcode */
255char *isotime(const struct tm * tmptr);
256
257/**
258 Re entrant version of isotime()
259*/
260void isotime_r(const struct tm *, char *);
261
262/**
263 A complete description of \c strftime() is beyond the pale of this document.
264 Refer to ISO/IEC document 9899 for details.
265
266 All conversions are made using the C Locale, ignoring the E or O modifiers.
267 Due to the lack of a time zone 'name', the 'Z' conversion is also ignored.
268*/
269size_t strftime(char *s, size_t maxsize, const char *format, const struct tm * timeptr);
270
271/**
272 Specify the Daylight Saving function.
273
274 The Daylight Saving function should examine its parameters to determine
275 whether Daylight Saving is in effect, and return a value appropriate
276 for \c tm_isdst.
277
278 Working examples for the USA and the EU are available:
279
280 \code #include <util/eu_dst.h>\endcode
281 for the European Union, and
282 \code #include <util/usa_dst.h>\endcode
283 for the United States.
284
285 If a Daylight Saving function is not specified, the system will
286 ignore Daylight Saving. */
287void set_dst(int (*) (const time_t *, int32_t *));
288
289/**
290 Set the 'time zone'. The parameter is given in seconds East of the
291 Prime Meridian. Example for New York City:
292 \code set_zone(-5 * ONE_HOUR);\endcode
293
294 If the time zone is not set, the time system will operate in UTC only. */
296
297/**
298 Initialize the system time. Examples are...
299
300 From a Clock / Calendar type RTC:
301 \code
302 struct tm rtc_time;
303
304 read_rtc(&rtc_time);
305 rtc_time.tm_isdst = 0;
306 set_system_time( mktime(&rtc_time) );
307 \endcode
308
309 From a Network Time Protocol time stamp:
310 \code
311 set_system_time(ntp_timestamp - NTP_OFFSET);
312 \endcode
313
314 From a UNIX time stamp:
315 \code
316 set_system_time(unix_timestamp - UNIX_OFFSET);
317 \endcode */
318void set_system_time(time_t timestamp);
319
320/**
321 Maintain the system time by calling this function at a rate of 1 Hertz.
322
323 It is anticipated that this function will typically be called from within
324 an Interrupt Service Routine, (though that is not required). It therefore
325 includes code which makes it simple to use from within a naked ISR,
326 avoiding the cost of saving and restoring all the CPU registers.
327
328 Such an ISR may resemble the following example:
329 \code
330 ISR(RTC_OVF_vect, ISR_NAKED)
331 {
332 system_tick();
333 reti();
334 }
335 \endcode */
336void system_tick(void);
337
338/**
339 Enumerated labels for the days of the week.
340*/
342{
343 SUNDAY,
344 MONDAY,
345 TUESDAY,
346 WEDNESDAY,
347 THURSDAY,
348 FRIDAY,
349 SATURDAY
350};
351
352/**
353 Enumerated labels for the months.
354*/
356{
357 JANUARY,
358 FEBRUARY,
359 MARCH,
360 APRIL,
361 MAY,
362 JUNE,
363 JULY,
364 AUGUST,
365 SEPTEMBER,
366 OCTOBER,
367 NOVEMBER,
368 DECEMBER
369};
370
371/**
372 Return 1 if year is a leap year, zero if it is not.
373*/
375
376/**
377 Return the length of month, given the year and month, where month is in
378 the range 1 to 12. */
380
381/**
382 Return the calendar week of year, where week 1 is considered to begin
383 on the day of week specified by \p start. The returned value may range
384 from zero to 52. */
385uint8_t week_of_year(const struct tm * timeptr, uint8_t start);
386
387/**
388 Return the calendar week of month, where the first week is considered
389 to begin on the day of week specified by \p start.
390 The returned value may range from zero to 5. */
391uint8_t week_of_month(const struct tm * timeptr, uint8_t start);
392
393/**
394 Structure which represents a date as a year, week number of that year,
395 and day of week.
396 See http://en.wikipedia.org/wiki/ISO_week_date for more information.
397*/
398struct week_date {
399 int year; /**< Year number (Gregorian calendar) */
400 int week; /**< Week number (#1 is where first Thursday is in) */
401 int day; /**< Day within week */
402};
403
404/**
405 Return a week_date structure with the ISO_8601 week based date
406 corresponding to the given year and day of year.
407 See http://en.wikipedia.org/wiki/ISO_week_date for more information. */
408struct week_date * iso_week_date (int year, int yday);
409
410/**
411 Re-entrant version of \c iso_week_date().
412*/
413void iso_week_date_r (int year, int yday, struct week_date *);
414
415/**
416 Convert a Y2K time stamp into a FAT file system time stamp. */
417uint32_t fatfs_time(const struct tm * timeptr);
418
419/** One hour, expressed in seconds */
420#define ONE_HOUR 3600
421
422/** Angular degree, expressed in arc seconds */
423#define ONE_DEGREE 3600
424
425/** One day, expressed in seconds */
426#define ONE_DAY 86400
427
428/** Difference between the Y2K and the UNIX epochs, in seconds. To convert a Y2K
429 timestamp to UNIX:
430 \code
431 long unix;
432 time_t y2k;
433
434 y2k = time(NULL);
435 unix = y2k + UNIX_OFFSET;
436 \endcode
437*/
438#define UNIX_OFFSET 946684800
439
440/** Difference between the Y2K and the NTP epochs, in seconds. To convert a Y2K
441 timestamp to NTP:
442 \code
443 unsigned long ntp;
444 time_t y2k;
445
446 y2k = time(NULL);
447 ntp = y2k + NTP_OFFSET;
448 \endcode
449*/
450#define NTP_OFFSET 3155673600
451
452/*
453 * ===================================================================
454 * Ephemera
455 */
456
457/**
458 Set the geographic coordinates of the 'observer', for use with several
459 of the following functions. Parameters are passed as seconds of
460 North Latitude, and seconds of East Longitude.
461
462 For New York City:
463 \code set_position (40.7142 * ONE_DEGREE, -74.0064 * ONE_DEGREE); \endcode
464*/
465void set_position(int32_t latitude, int32_t longitude);
466
467/**
468 Computes the difference between apparent solar time and mean solar time.
469 The returned value is in seconds.
470*/
471int16_t equation_of_time(const time_t * timer);
472
473/**
474 Computes the amount of time the sun is above the horizon, at the
475 location of the observer.
476
477 Note: At observer locations inside a polar circle, this value can be
478 zero during the winter, and can exceed \c ONE_DAY during the summer.
479
480 The returned value is in seconds. */
481int32_t daylight_seconds(const time_t * timer);
482
483/**
484 Computes the time of solar noon, at the location of the observer.
485*/
486time_t solar_noon(const time_t * timer);
487
488/**
489 Return the time of sunrise, at the location of the observer.
490 See the note about \c daylight_seconds(). */
491time_t sun_rise(const time_t * timer);
492
493/**
494 Return the time of sunset, at the location of the observer.
495 See the note about \c daylight_seconds(). */
496time_t sun_set(const time_t * timer);
497
498/**
499 Returns the declination of the sun in radians. */
500float solar_declinationf(const time_t * timer);
501
502/**
503 Returns the declination of the sun in radians.
504
505 This implementation is only available when \c double is a 32-bit type. */
506double solar_declination(const time_t * timer);
507
508/**
509 Returns the declination of the sun in radians.
510
511 This implementation is only available when <tt>long double</tt> is
512 a 32-bit type. */
513long double solar_declinationl(const time_t * timer);
514
515/**
516 Returns an approximation to the phase of the moon.
517 The sign of the returned value indicates a waning or waxing phase.
518 The magnitude of the returned value indicates the percentage illumination.
519*/
520int8_t moon_phase(const time_t * timer);
521
522/**
523 Returns Greenwich Mean Sidereal Time, as seconds into the sidereal day.
524 The returned value will range from 0 through 86399 seconds.
525*/
526uint32_t gm_sidereal(const time_t * timer);
527
528/**
529 Returns Local Mean Sidereal Time, as seconds into the sidereal day.
530 The returned value will range from 0 through 86399 seconds.
531*/
532uint32_t lm_sidereal(const time_t * timer);
533
534/**@}*/
535#ifdef __cplusplus
536}
537#endif
538
539#endif /* TIME_H */
unsigned long int uint32_t
Definition: stdint.h:114
signed int int16_t
Definition: stdint.h:92
unsigned char uint8_t
Definition: stdint.h:88
signed long int int32_t
Definition: stdint.h:110
signed char int8_t
Definition: stdint.h:84
void set_system_time(time_t timestamp)
Definition: set_system_time.c:40
char * asctime(const struct tm *timeptr)
Definition: asctime.c:38
time_t sun_set(const time_t *timer)
Definition: sun_set.c:38
char * isotime(const struct tm *tmptr)
Definition: isotime.c:38
uint32_t fatfs_time(const struct tm *timeptr)
void system_tick(void)
int32_t difftime(time_t time1, time_t time0)
Definition: difftime.c:38
time_t sun_rise(const time_t *timer)
Definition: sun_rise.c:38
struct week_date * iso_week_date(int year, int yday)
Definition: iso_week_date.c:42
time_t time(time_t *timer)
Definition: time.c:40
uint32_t gm_sidereal(const time_t *timer)
Definition: gm_sidereal.c:49
uint32_t time_t
Definition: time.h:132
uint32_t lm_sidereal(const time_t *timer)
Definition: lm_sidereal.c:38
double solar_declination(const time_t *timer)
void ctime_r(const time_t *timer, char *buf)
Definition: ctime_r.c:37
time_t solar_noon(const time_t *timer)
Definition: solar_noon.c:38
void set_dst(int(*)(const time_t *, int32_t *))
Definition: set_dst.c:39
struct tm * localtime(const time_t *timer)
Definition: localtime.c:38
uint8_t week_of_month(const struct tm *timeptr, uint8_t start)
Definition: week_of_month.c:42
int16_t equation_of_time(const time_t *timer)
Definition: equation_of_time.c:54
struct tm * gmtime(const time_t *timer)
Definition: gmtime.c:39
int8_t moon_phase(const time_t *timer)
Definition: moon_phase.c:39
uint8_t week_of_year(const struct tm *timeptr, uint8_t start)
Definition: week_of_year.c:42
uint8_t is_leap_year(int16_t year)
int32_t daylight_seconds(const time_t *timer)
Definition: daylight_seconds.c:41
long double solar_declinationl(const time_t *timer)
void gmtime_r(const time_t *timer, struct tm *timeptr)
Definition: gmtime_r.c:38
void iso_week_date_r(int year, int yday, struct week_date *)
Definition: iso_week_date_r.c:49
void set_position(int32_t latitude, int32_t longitude)
_MONTHS_
Definition: time.h:356
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
Definition: strftime.c:113
void asctime_r(const struct tm *timeptr, char *buf)
Definition: asctime_r.c:44
uint8_t month_length(int16_t year, uint8_t month)
void set_zone(int32_t)
time_t mk_gmtime(const struct tm *timeptr)
Definition: mk_gmtime.c:40
float solar_declinationf(const time_t *timer)
Definition: solar_declination.c:49
void localtime_r(const time_t *timer, struct tm *timeptr)
Definition: localtime_r.c:38
char * ctime(const time_t *timer)
Definition: ctime.c:38
time_t mktime(struct tm *timeptr)
Definition: mktime.c:40
void isotime_r(const struct tm *, char *)
Definition: isotime_r.c:40
_WEEK_DAYS_
Definition: time.h:342
Definition: time.h:160
int8_t tm_wday
Definition: time.h:165
int8_t tm_mday
Definition: time.h:164
int8_t tm_mon
Definition: time.h:166
int16_t tm_isdst
Definition: time.h:169
int16_t tm_year
Definition: time.h:167
int8_t tm_hour
Definition: time.h:163
int8_t tm_min
Definition: time.h:162
int8_t tm_sec
Definition: time.h:161
int16_t tm_yday
Definition: time.h:168
Definition: time.h:398
int year
Definition: time.h:399
int week
Definition: time.h:400
int day
Definition: time.h:401