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