Lab 1 Program C
Fork of mbed by
rtc_time.h@43:aff670d0d510, 2012-10-24 (annotated)
- Committer:
- screamer
- Date:
- Wed Oct 24 10:44:49 2012 +0000
- Revision:
- 43:aff670d0d510
- Parent:
- 27:7110ebee3484
Conversion of the classes documentation to Doxygen format
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 43:aff670d0d510 | 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: |
screamer | 43:aff670d0d510 | 8 | * @code |
screamer | 43:aff670d0d510 | 9 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 10 | * |
screamer | 43:aff670d0d510 | 11 | * int main() { |
screamer | 43:aff670d0d510 | 12 | * set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
screamer | 43:aff670d0d510 | 13 | * |
screamer | 43:aff670d0d510 | 14 | * while(1) { |
screamer | 43:aff670d0d510 | 15 | * time_t seconds = time(NULL); |
screamer | 43:aff670d0d510 | 16 | * |
screamer | 43:aff670d0d510 | 17 | * printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
screamer | 43:aff670d0d510 | 18 | * |
screamer | 43:aff670d0d510 | 19 | * printf("Time as a basic string = %s", ctime(&seconds)); |
screamer | 43:aff670d0d510 | 20 | * |
screamer | 43:aff670d0d510 | 21 | * char buffer[32]; |
screamer | 43:aff670d0d510 | 22 | * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
screamer | 43:aff670d0d510 | 23 | * printf("Time as a custom formatted string = %s", buffer); |
screamer | 43:aff670d0d510 | 24 | * |
screamer | 43:aff670d0d510 | 25 | * wait(1); |
screamer | 43:aff670d0d510 | 26 | * } |
screamer | 43:aff670d0d510 | 27 | * } |
screamer | 43:aff670d0d510 | 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 |
screamer | 43:aff670d0d510 | 42 | /** Get the current time |
rolf.meyer@arm.com | 14:20a79241b4a0 | 43 | * |
screamer | 43:aff670d0d510 | 44 | * Returns the current timestamp as the number of seconds since January 1, 1970 |
screamer | 43:aff670d0d510 | 45 | * (the UNIX timestamp). The value is based on the current value of the |
screamer | 43:aff670d0d510 | 46 | * microcontroller Real-Time Clock (RTC), which can be set using <set_time>. |
screamer | 43:aff670d0d510 | 47 | * |
screamer | 43:aff670d0d510 | 48 | * @param t Pointer to a time_t to be set, or NULL if not used |
screamer | 43:aff670d0d510 | 49 | * |
screamer | 43:aff670d0d510 | 50 | * @returns |
screamer | 43:aff670d0d510 | 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: |
screamer | 43:aff670d0d510 | 54 | * @code |
screamer | 43:aff670d0d510 | 55 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 56 | * |
screamer | 43:aff670d0d510 | 57 | * int main() { |
screamer | 43:aff670d0d510 | 58 | * time_t seconds = time(NULL); |
screamer | 43:aff670d0d510 | 59 | * printf("It is %d seconds since January 1, 1970\n", seconds); |
screamer | 43:aff670d0d510 | 60 | * } |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 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 | * |
screamer | 43:aff670d0d510 | 72 | * @param t Number of seconds since January 1, 1970 (the UNIX timestamp) |
screamer | 43:aff670d0d510 | 73 | * |
rolf.meyer@arm.com | 14:20a79241b4a0 | 74 | * Example: |
screamer | 43:aff670d0d510 | 75 | * @code |
screamer | 43:aff670d0d510 | 76 | * #include "mbed.h" |
rolf.meyer@arm.com | 14:20a79241b4a0 | 77 | * |
screamer | 43:aff670d0d510 | 78 | * int main() { |
screamer | 43:aff670d0d510 | 79 | * set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37 |
screamer | 43:aff670d0d510 | 80 | * } |
screamer | 43:aff670d0d510 | 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 |
screamer | 43:aff670d0d510 | 86 | /** Converts the tm structure in to a timestamp in seconds since January 1, 1970 |
screamer | 43:aff670d0d510 | 87 | * (the UNIX timestamp). The values of tm_wday and tm_yday of the tm structure |
screamer | 43:aff670d0d510 | 88 | * are also updated to their appropriate values. |
screamer | 43:aff670d0d510 | 89 | * |
screamer | 43:aff670d0d510 | 90 | * @param t The tm structure to convert |
screamer | 43:aff670d0d510 | 91 | * |
screamer | 43:aff670d0d510 | 92 | * @returns |
screamer | 43:aff670d0d510 | 93 | * The converted timestamp |
rolf.meyer@arm.com | 14:20a79241b4a0 | 94 | * |
rolf.meyer@arm.com | 14:20a79241b4a0 | 95 | * Example: |
screamer | 43:aff670d0d510 | 96 | * @code |
screamer | 43:aff670d0d510 | 97 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 98 | * |
screamer | 43:aff670d0d510 | 99 | * int main() { |
screamer | 43:aff670d0d510 | 100 | * // setup time structure for Wed, 28 Oct 2009 11:35:37 |
screamer | 43:aff670d0d510 | 101 | * struct tm t; |
screamer | 43:aff670d0d510 | 102 | * t.tm_sec = 37; // 0-59 |
screamer | 43:aff670d0d510 | 103 | * t.tm_min = 35; // 0-59 |
screamer | 43:aff670d0d510 | 104 | * t.tm_hour = 11; // 0-23 |
screamer | 43:aff670d0d510 | 105 | * t.tm_mday = 28; // 1-31 |
screamer | 43:aff670d0d510 | 106 | * t.tm_mon = 9; // 0-11 |
screamer | 43:aff670d0d510 | 107 | * t.tm_year = 109; // year since 1900 |
rolf.meyer@arm.com | 14:20a79241b4a0 | 108 | * |
screamer | 43:aff670d0d510 | 109 | * // convert to timestamp and display (1256729737) |
screamer | 43:aff670d0d510 | 110 | * time_t seconds = mktime(&t); |
screamer | 43:aff670d0d510 | 111 | * printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
screamer | 43:aff670d0d510 | 112 | * } |
screamer | 43:aff670d0d510 | 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 |
screamer | 43:aff670d0d510 | 119 | /** Converts the timestamp pointed to by t to a (statically allocated) |
screamer | 43:aff670d0d510 | 120 | * tm structure. |
screamer | 43:aff670d0d510 | 121 | * |
screamer | 43:aff670d0d510 | 122 | * @param t Pointer to the timestamp |
screamer | 43:aff670d0d510 | 123 | * |
screamer | 43:aff670d0d510 | 124 | * @returns |
screamer | 43:aff670d0d510 | 125 | * Pointer to the (statically allocated) tm structure |
rolf.meyer@arm.com | 14:20a79241b4a0 | 126 | * |
rolf.meyer@arm.com | 14:20a79241b4a0 | 127 | * Example: |
screamer | 43:aff670d0d510 | 128 | * @code |
screamer | 43:aff670d0d510 | 129 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 130 | * |
screamer | 43:aff670d0d510 | 131 | * int main() { |
screamer | 43:aff670d0d510 | 132 | * time_t seconds = 1256729737; |
screamer | 43:aff670d0d510 | 133 | * struct tm *t = localtime(&seconds); |
screamer | 43:aff670d0d510 | 134 | * } |
screamer | 43:aff670d0d510 | 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 |
screamer | 43:aff670d0d510 | 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: |
screamer | 43:aff670d0d510 | 148 | * @code |
screamer | 43:aff670d0d510 | 149 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 150 | * |
screamer | 43:aff670d0d510 | 151 | * int main() { |
screamer | 43:aff670d0d510 | 152 | * time_t seconds = time(NULL); |
screamer | 43:aff670d0d510 | 153 | * printf("Time as a string = %s", ctime(&seconds)); |
screamer | 43:aff670d0d510 | 154 | * } |
screamer | 43:aff670d0d510 | 155 | * @endcode |
rolf.meyer@arm.com | 14:20a79241b4a0 | 156 | * |
screamer | 43:aff670d0d510 | 157 | * @param t The timestamp to convert |
screamer | 43:aff670d0d510 | 158 | * |
screamer | 43:aff670d0d510 | 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 |
screamer | 43:aff670d0d510 | 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: |
screamer | 43:aff670d0d510 | 172 | * - %S - Second (00-59) |
screamer | 43:aff670d0d510 | 173 | * - %M - Minute (00-59) |
screamer | 43:aff670d0d510 | 174 | * - %H - Hour (00-23) |
screamer | 43:aff670d0d510 | 175 | * - %d - Day (01-31) |
screamer | 43:aff670d0d510 | 176 | * - %m - Month (01-12) |
screamer | 43:aff670d0d510 | 177 | * - %Y/%y - Year (2009/09) |
screamer | 43:aff670d0d510 | 178 | * - %A/%a - Weekday Name (Monday/Mon) |
screamer | 43:aff670d0d510 | 179 | * - %B/%b - Month Name (January/Jan) |
screamer | 43:aff670d0d510 | 180 | * - %I - 12 Hour Format (01-12) |
screamer | 43:aff670d0d510 | 181 | * - %p - "AM" or "PM" |
screamer | 43:aff670d0d510 | 182 | * - %X - Time (14:55:02) |
screamer | 43:aff670d0d510 | 183 | * - %x - Date (08/23/01) |
rolf.meyer@arm.com | 14:20a79241b4a0 | 184 | * |
screamer | 43:aff670d0d510 | 185 | * @param buffer String buffer to store the result |
screamer | 43:aff670d0d510 | 186 | * @param max Maximum number of characters to store in the buffer |
screamer | 43:aff670d0d510 | 187 | * @param format Format specifier string |
screamer | 43:aff670d0d510 | 188 | * @param t Pointer to the tm structure to convert |
screamer | 43:aff670d0d510 | 189 | * |
screamer | 43:aff670d0d510 | 190 | * @returns |
screamer | 43:aff670d0d510 | 191 | * Number of characters copied |
rolf.meyer@arm.com | 14:20a79241b4a0 | 192 | * |
rolf.meyer@arm.com | 14:20a79241b4a0 | 193 | * Example: |
screamer | 43:aff670d0d510 | 194 | * @code |
screamer | 43:aff670d0d510 | 195 | * #include "mbed.h" |
rolf.meyer@arm.com | 14:20a79241b4a0 | 196 | * |
screamer | 43:aff670d0d510 | 197 | * int main() { |
screamer | 43:aff670d0d510 | 198 | * time_t seconds = time(NULL); |
screamer | 43:aff670d0d510 | 199 | * |
screamer | 43:aff670d0d510 | 200 | * char buffer[32]; |
screamer | 43:aff670d0d510 | 201 | * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
screamer | 43:aff670d0d510 | 202 | * printf("Time as a formatted string = %s", buffer); |
screamer | 43:aff670d0d510 | 203 | * } |
screamer | 43:aff670d0d510 | 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 |