?? timeformat.c
字號:
/* $Id: timeformat.c,v 1.3 2006/01/19 20:52:23 simimeie Exp $ * Routines for handling various time representations. * Mainly conversion routines from broken time to timestamps and the other * way round. */ #define BV _BV#include <avr/io.h>#include "timeformat.h"/* The UNIX-Timestamp of 1.1.2000 */static const uint32_t TS01012000 = 946681200UL;/* On which day does a new month start in non-leap-years */static const uint16_t monthstarts[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };static const uint32_t SECONDSINADAY = (60UL * 60 * 24);/* Converts a brokentime-struct to a timestamp */uint32_t bttots(struct brokentime * bt) { uint32_t res = TS01012000 + SECONDSINADAY; /* 2000 was leap year */ if ((bt->month == 0) || (bt->day == 0)) { /* Not a valid date */ return 0; } res += SECONDSINADAY * bt->year * 365; /* Now account for the leap years. Yes, 2000 was a leap year too. */ res += SECONDSINADAY * ((bt->year - 1) >> 2); res += SECONDSINADAY * monthstarts[bt->month - 1]; if ((!(bt -> year & 3)) && (bt->month >= 3)) { /* We are in a leap year and past february */ res += SECONDSINADAY; } res += SECONDSINADAY * (bt->day - 1); res += 3600UL * bt->hour; res += 60 * bt->min; res += bt->sec; return res;}/* Converts a timestamp to a brokentime-struct */void tstobt(uint32_t ts, struct brokentime * bt) { uint8_t c = 0; if (ts < TS01012000) { bt->year = bt->month = bt->day = bt->hour = bt->min = bt->sec = 0; return; } ts -= TS01012000; while (1) { if (!(c & 3)) { /* Leap year */ if (ts < (SECONDSINADAY * 366)) break; ts -= SECONDSINADAY * 366; } else { if (ts < (SECONDSINADAY * 365)) break; ts -= SECONDSINADAY * 365; } c++; } bt->year = c; for (c = 11; c; c--) { if ((SECONDSINADAY * monthstarts[c]) <= ts) break; } bt->month = c + 1; ts -= SECONDSINADAY * monthstarts[c]; bt->day = (ts / SECONDSINADAY) + 1; ts = ts % SECONDSINADAY; if ((!(bt->year & 3)) && (bt->month >= 3)) { /* Leap year and >= 29.2. */ bt->day--; if (bt->day == 0) { /* Oups, we're at the end of the */ bt->month--; /* previous month */ switch (bt->month) { case 2: bt->day = 29; break; case 3: bt->day = 31; break; case 4: bt->day = 30; break; case 5: bt->day = 31; break; case 6: bt->day = 30; break; case 7: bt->day = 31; break; default: if (bt->day & 1) { bt->day = 30; } else { bt->day = 31; } break; }; } } bt->hour = (ts / 3600); ts = ts % 3600; bt->min = (ts / 60); bt->sec = (ts % 60);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -