AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

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