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