mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Oct 05 09:16:41 2012 +0000
Revision:
0:8024c367e29f
Child:
2:e9a661555b58
First release of the mbed libraries for KL25Z

Who changed what in which revision?

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