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