meh

Fork of mbed by mbed official

Committer:
emilmont
Date:
Fri Oct 26 17:40:46 2012 +0100
Revision:
43:e2ed12d17f06
Parent:
27:7110ebee3484
Child:
44:24d45a770a51
Update documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 43:e2ed12d17f06 1 /** Implementation of the C time.h functions
rolf.meyer@arm.com 14:20a79241b4a0 2 *
rolf.meyer@arm.com 14:20a79241b4a0 3 * Provides mechanisms to set and read the current time, based
rolf.meyer@arm.com 14:20a79241b4a0 4 * on the microcontroller Real-Time Clock (RTC), plus some
rolf.meyer@arm.com 14:20a79241b4a0 5 * standard C manipulation and formating functions.
rolf.meyer@arm.com 14:20a79241b4a0 6 *
rolf.meyer@arm.com 14:20a79241b4a0 7 * Example:
emilmont 43:e2ed12d17f06 8 * @code
emilmont 43:e2ed12d17f06 9 * #include "mbed.h"
emilmont 43:e2ed12d17f06 10 *
emilmont 43:e2ed12d17f06 11 * int main() {
emilmont 43:e2ed12d17f06 12 * set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
emilmont 43:e2ed12d17f06 13 *
emilmont 43:e2ed12d17f06 14 * while(1) {
emilmont 43:e2ed12d17f06 15 * time_t seconds = time(NULL);
emilmont 43:e2ed12d17f06 16 *
emilmont 43:e2ed12d17f06 17 * printf("Time as seconds since January 1, 1970 = %d\n", seconds);
emilmont 43:e2ed12d17f06 18 *
emilmont 43:e2ed12d17f06 19 * printf("Time as a basic string = %s", ctime(&seconds));
emilmont 43:e2ed12d17f06 20 *
emilmont 43:e2ed12d17f06 21 * char buffer[32];
emilmont 43:e2ed12d17f06 22 * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
emilmont 43:e2ed12d17f06 23 * printf("Time as a custom formatted string = %s", buffer);
emilmont 43:e2ed12d17f06 24 *
emilmont 43:e2ed12d17f06 25 * wait(1);
emilmont 43:e2ed12d17f06 26 * }
emilmont 43:e2ed12d17f06 27 * }
emilmont 43:e2ed12d17f06 28 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 29 */
rolf.meyer@arm.com 14:20a79241b4a0 30
rolf.meyer@arm.com 14:20a79241b4a0 31 /* mbed Microcontroller Library - rtc_time
rolf.meyer@arm.com 14:20a79241b4a0 32 * Copyright (c) 2009 ARM Limited. All rights reserved.
rolf.meyer@arm.com 14:20a79241b4a0 33 */
rolf.meyer@arm.com 14:20a79241b4a0 34
rolf.meyer@arm.com 14:20a79241b4a0 35 #include <time.h>
rolf.meyer@arm.com 14:20a79241b4a0 36
rolf.meyer@arm.com 14:20a79241b4a0 37 #ifdef __cplusplus
rolf.meyer@arm.com 14:20a79241b4a0 38 extern "C" {
rolf.meyer@arm.com 14:20a79241b4a0 39 #endif
rolf.meyer@arm.com 14:20a79241b4a0 40
rolf.meyer@arm.com 14:20a79241b4a0 41 #if 0 // for documentation only
emilmont 43:e2ed12d17f06 42 /** Get the current time
rolf.meyer@arm.com 14:20a79241b4a0 43 *
emilmont 43:e2ed12d17f06 44 * Returns the current timestamp as the number of seconds since January 1, 1970
emilmont 43:e2ed12d17f06 45 * (the UNIX timestamp). The value is based on the current value of the
emilmont 43:e2ed12d17f06 46 * microcontroller Real-Time Clock (RTC), which can be set using <set_time>.
emilmont 43:e2ed12d17f06 47 *
emilmont 43:e2ed12d17f06 48 * @param t Pointer to a time_t to be set, or NULL if not used
emilmont 43:e2ed12d17f06 49 *
emilmont 43:e2ed12d17f06 50 * @returns
emilmont 43:e2ed12d17f06 51 * Number of seconds since January 1, 1970 (the UNIX timestamp)
rolf.meyer@arm.com 14:20a79241b4a0 52 *
rolf.meyer@arm.com 14:20a79241b4a0 53 * Example:
emilmont 43:e2ed12d17f06 54 * @code
emilmont 43:e2ed12d17f06 55 * #include "mbed.h"
emilmont 43:e2ed12d17f06 56 *
emilmont 43:e2ed12d17f06 57 * int main() {
emilmont 43:e2ed12d17f06 58 * time_t seconds = time(NULL);
emilmont 43:e2ed12d17f06 59 * printf("It is %d seconds since January 1, 1970\n", seconds);
emilmont 43:e2ed12d17f06 60 * }
emilmont 43:e2ed12d17f06 61 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 62 */
rolf.meyer@arm.com 14:20a79241b4a0 63 time_t time(time_t *t);
rolf.meyer@arm.com 14:20a79241b4a0 64 #endif
rolf.meyer@arm.com 14:20a79241b4a0 65
emilmont 43:e2ed12d17f06 66 /** Set the current time
rolf.meyer@arm.com 14:20a79241b4a0 67 *
rolf.meyer@arm.com 14:20a79241b4a0 68 * Initialises and sets the time of the microcontroller Real-Time Clock (RTC)
rolf.meyer@arm.com 14:20a79241b4a0 69 * to the time represented by the number of seconds since January 1, 1970
rolf.meyer@arm.com 14:20a79241b4a0 70 * (the UNIX timestamp).
rolf.meyer@arm.com 14:20a79241b4a0 71 *
emilmont 43:e2ed12d17f06 72 * @param t Number of seconds since January 1, 1970 (the UNIX timestamp)
emilmont 43:e2ed12d17f06 73 *
rolf.meyer@arm.com 14:20a79241b4a0 74 * Example:
emilmont 43:e2ed12d17f06 75 * @code
emilmont 43:e2ed12d17f06 76 * #include "mbed.h"
rolf.meyer@arm.com 14:20a79241b4a0 77 *
emilmont 43:e2ed12d17f06 78 * int main() {
emilmont 43:e2ed12d17f06 79 * set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
emilmont 43:e2ed12d17f06 80 * }
emilmont 43:e2ed12d17f06 81 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 82 */
rolf.meyer@arm.com 14:20a79241b4a0 83 void set_time(time_t t);
rolf.meyer@arm.com 14:20a79241b4a0 84
rolf.meyer@arm.com 14:20a79241b4a0 85 #if 0 // for documentation only
emilmont 43:e2ed12d17f06 86 /** Converts the tm structure in to a timestamp in seconds since January 1, 1970
emilmont 43:e2ed12d17f06 87 * (the UNIX timestamp). The values of tm_wday and tm_yday of the tm structure
emilmont 43:e2ed12d17f06 88 * are also updated to their appropriate values.
emilmont 43:e2ed12d17f06 89 *
emilmont 43:e2ed12d17f06 90 * @param t The tm structure to convert
emilmont 43:e2ed12d17f06 91 *
emilmont 43:e2ed12d17f06 92 * @returns
emilmont 43:e2ed12d17f06 93 * The converted timestamp
rolf.meyer@arm.com 14:20a79241b4a0 94 *
rolf.meyer@arm.com 14:20a79241b4a0 95 * Example:
emilmont 43:e2ed12d17f06 96 * @code
emilmont 43:e2ed12d17f06 97 * #include "mbed.h"
emilmont 43:e2ed12d17f06 98 *
emilmont 43:e2ed12d17f06 99 * int main() {
emilmont 43:e2ed12d17f06 100 * // setup time structure for Wed, 28 Oct 2009 11:35:37
emilmont 43:e2ed12d17f06 101 * struct tm t;
emilmont 43:e2ed12d17f06 102 * t.tm_sec = 37; // 0-59
emilmont 43:e2ed12d17f06 103 * t.tm_min = 35; // 0-59
emilmont 43:e2ed12d17f06 104 * t.tm_hour = 11; // 0-23
emilmont 43:e2ed12d17f06 105 * t.tm_mday = 28; // 1-31
emilmont 43:e2ed12d17f06 106 * t.tm_mon = 9; // 0-11
emilmont 43:e2ed12d17f06 107 * t.tm_year = 109; // year since 1900
rolf.meyer@arm.com 14:20a79241b4a0 108 *
emilmont 43:e2ed12d17f06 109 * // convert to timestamp and display (1256729737)
emilmont 43:e2ed12d17f06 110 * time_t seconds = mktime(&t);
emilmont 43:e2ed12d17f06 111 * printf("Time as seconds since January 1, 1970 = %d\n", seconds);
emilmont 43:e2ed12d17f06 112 * }
emilmont 43:e2ed12d17f06 113 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 114 */
rolf.meyer@arm.com 14:20a79241b4a0 115 time_t mktime(struct tm *t);
rolf.meyer@arm.com 14:20a79241b4a0 116 #endif
rolf.meyer@arm.com 14:20a79241b4a0 117
rolf.meyer@arm.com 14:20a79241b4a0 118 #if 0 // for documentation only
emilmont 43:e2ed12d17f06 119 /** Converts the timestamp pointed to by t to a (statically allocated)
emilmont 43:e2ed12d17f06 120 * tm structure.
emilmont 43:e2ed12d17f06 121 *
emilmont 43:e2ed12d17f06 122 * @param t Pointer to the timestamp
emilmont 43:e2ed12d17f06 123 *
emilmont 43:e2ed12d17f06 124 * @returns
emilmont 43:e2ed12d17f06 125 * Pointer to the (statically allocated) tm structure
rolf.meyer@arm.com 14:20a79241b4a0 126 *
rolf.meyer@arm.com 14:20a79241b4a0 127 * Example:
emilmont 43:e2ed12d17f06 128 * @code
emilmont 43:e2ed12d17f06 129 * #include "mbed.h"
emilmont 43:e2ed12d17f06 130 *
emilmont 43:e2ed12d17f06 131 * int main() {
emilmont 43:e2ed12d17f06 132 * time_t seconds = 1256729737;
emilmont 43:e2ed12d17f06 133 * struct tm *t = localtime(&seconds);
emilmont 43:e2ed12d17f06 134 * }
emilmont 43:e2ed12d17f06 135 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 136 */
rolf.meyer@arm.com 14:20a79241b4a0 137 struct tm *localtime(const time_t *t);
rolf.meyer@arm.com 14:20a79241b4a0 138 #endif
rolf.meyer@arm.com 14:20a79241b4a0 139
rolf.meyer@arm.com 14:20a79241b4a0 140 #if 0 // for documentation only
emilmont 43:e2ed12d17f06 141 /** Converts a timestamp to a human-readable string
rolf.meyer@arm.com 14:20a79241b4a0 142 *
rolf.meyer@arm.com 14:20a79241b4a0 143 * Converts a time_t timestamp in seconds since January 1, 1970 (the UNIX
rolf.meyer@arm.com 14:20a79241b4a0 144 * timestamp) to a human readable string format. The result is of the
rolf.meyer@arm.com 14:20a79241b4a0 145 * format: "Wed Oct 28 11:35:37 2009\n"
rolf.meyer@arm.com 14:20a79241b4a0 146 *
rolf.meyer@arm.com 14:20a79241b4a0 147 * Example:
emilmont 43:e2ed12d17f06 148 * @code
emilmont 43:e2ed12d17f06 149 * #include "mbed.h"
emilmont 43:e2ed12d17f06 150 *
emilmont 43:e2ed12d17f06 151 * int main() {
emilmont 43:e2ed12d17f06 152 * time_t seconds = time(NULL);
emilmont 43:e2ed12d17f06 153 * printf("Time as a string = %s", ctime(&seconds));
emilmont 43:e2ed12d17f06 154 * }
emilmont 43:e2ed12d17f06 155 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 156 *
emilmont 43:e2ed12d17f06 157 * @param t The timestamp to convert
emilmont 43:e2ed12d17f06 158 *
emilmont 43:e2ed12d17f06 159 * @returns Pointer to a (statically allocated) string containing the
rolf.meyer@arm.com 14:20a79241b4a0 160 * human readable representation, including a '\n' character
rolf.meyer@arm.com 14:20a79241b4a0 161 */
rolf.meyer@arm.com 14:20a79241b4a0 162 char *ctime(const time_t *t);
rolf.meyer@arm.com 14:20a79241b4a0 163 #endif
rolf.meyer@arm.com 14:20a79241b4a0 164
rolf.meyer@arm.com 14:20a79241b4a0 165 #if 0 // for documentation only
emilmont 43:e2ed12d17f06 166 /** Converts a tm structure to a custom format human-readable string
rolf.meyer@arm.com 14:20a79241b4a0 167 *
rolf.meyer@arm.com 14:20a79241b4a0 168 * Creates a formated string from a tm structure, based on a string format
rolf.meyer@arm.com 14:20a79241b4a0 169 * specifier provided.
rolf.meyer@arm.com 14:20a79241b4a0 170 *
rolf.meyer@arm.com 14:20a79241b4a0 171 * Format Specifiers:
emilmont 43:e2ed12d17f06 172 * - %S - Second (00-59)
emilmont 43:e2ed12d17f06 173 * - %M - Minute (00-59)
emilmont 43:e2ed12d17f06 174 * - %H - Hour (00-23)
emilmont 43:e2ed12d17f06 175 * - %d - Day (01-31)
emilmont 43:e2ed12d17f06 176 * - %m - Month (01-12)
emilmont 43:e2ed12d17f06 177 * - %Y/%y - Year (2009/09)
emilmont 43:e2ed12d17f06 178 * - %A/%a - Weekday Name (Monday/Mon)
emilmont 43:e2ed12d17f06 179 * - %B/%b - Month Name (January/Jan)
emilmont 43:e2ed12d17f06 180 * - %I - 12 Hour Format (01-12)
emilmont 43:e2ed12d17f06 181 * - %p - "AM" or "PM"
emilmont 43:e2ed12d17f06 182 * - %X - Time (14:55:02)
emilmont 43:e2ed12d17f06 183 * - %x - Date (08/23/01)
rolf.meyer@arm.com 14:20a79241b4a0 184 *
emilmont 43:e2ed12d17f06 185 * @param buffer String buffer to store the result
emilmont 43:e2ed12d17f06 186 * @param max Maximum number of characters to store in the buffer
emilmont 43:e2ed12d17f06 187 * @param format Format specifier string
emilmont 43:e2ed12d17f06 188 * @param t Pointer to the tm structure to convert
emilmont 43:e2ed12d17f06 189 *
emilmont 43:e2ed12d17f06 190 * @returns
emilmont 43:e2ed12d17f06 191 * Number of characters copied
rolf.meyer@arm.com 14:20a79241b4a0 192 *
rolf.meyer@arm.com 14:20a79241b4a0 193 * Example:
emilmont 43:e2ed12d17f06 194 * @code
emilmont 43:e2ed12d17f06 195 * #include "mbed.h"
rolf.meyer@arm.com 14:20a79241b4a0 196 *
emilmont 43:e2ed12d17f06 197 * int main() {
emilmont 43:e2ed12d17f06 198 * time_t seconds = time(NULL);
emilmont 43:e2ed12d17f06 199 *
emilmont 43:e2ed12d17f06 200 * char buffer[32];
emilmont 43:e2ed12d17f06 201 * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
emilmont 43:e2ed12d17f06 202 * printf("Time as a formatted string = %s", buffer);
emilmont 43:e2ed12d17f06 203 * }
emilmont 43:e2ed12d17f06 204 * @endcode
rolf.meyer@arm.com 14:20a79241b4a0 205 */
rolf.meyer@arm.com 14:20a79241b4a0 206 size_t strftime(char *buffer, size_t max, const char *format, const struct tm *t);
rolf.meyer@arm.com 14:20a79241b4a0 207 #endif
rolf.meyer@arm.com 14:20a79241b4a0 208
rolf.meyer@arm.com 14:20a79241b4a0 209 #ifdef __cplusplus
rolf.meyer@arm.com 14:20a79241b4a0 210 }
rolf.meyer@arm.com 14:20a79241b4a0 211 #endif