Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Diff: tm/tm.c
- Revision:
- 37:330b844f54b6
- Parent:
- 35:ba9f575aa3c6
- Child:
- 38:25b2a3c494aa
--- a/tm/tm.c Sat Dec 01 17:24:49 2018 +0000 +++ b/tm/tm.c Sat Dec 01 19:13:29 2018 +0000 @@ -1,12 +1,14 @@ +#include <time.h> #include <stdlib.h> -#include <time.h> #include <stdio.h> #include <string.h> #include <stdbool.h> +#include <stdint.h> #define STD_OFFSET 0 #define DST_OFFSET 1 + static bool isLeapYear(int year) { year += 1900; @@ -81,39 +83,6 @@ *pDayOfYear = dayOfYear; // 0 --> 365 *pDayOfWeek = dayOfWeek % 7; // 0 --> 6 } -void TmIncrement(struct tm* ptm) -{ - ptm->tm_sec++; - if (ptm->tm_sec > 59) - { - ptm->tm_sec = 0; - ptm->tm_min++; - } - if (ptm->tm_min > 59) - { - ptm->tm_min = 0; - ptm->tm_hour++; - } - if (ptm->tm_hour > 23) - { - ptm->tm_hour = 0; - ptm->tm_wday++; - if (ptm->tm_wday > 6) ptm->tm_wday = 0; - ptm->tm_yday++; - ptm->tm_mday++; - if (ptm->tm_mday > monthLength(ptm->tm_year, ptm->tm_mon)) - { - ptm->tm_mon++; - if (ptm->tm_mon > 11) - { - ptm->tm_year++; - ptm->tm_yday = 0; - ptm->tm_mon = 0; - } - ptm->tm_mday = 1; - } - } -} static void normalise(int* pHours, int* pDayOfWeek, int* pDayOfMonth, int* pMonth, int * pDayOfYear, int* pYear) { if (*pHours > 23) @@ -192,7 +161,7 @@ ++*pMonth; } } -static void timeToTm(time_t t, struct tm* ptm, bool local) +static void timeToTm(int64_t t, struct tm* ptm, bool local) { //Extract the seconds, minutes, hours and days from the time_t t div_t divres; @@ -245,17 +214,19 @@ ptm->tm_yday = dayOfYear; // 0 --> 365 ptm->tm_isdst = dst; // +ve if DST, 0 if not DSTime, -ve if the information is not available. Note that 'true' evaluates to +1. } -void TmUtcFromTimeT(time_t time, struct tm* ptm) + + +void TmUtcFromSeconds1970(int64_t time, struct tm* ptm) { timeToTm(time, ptm, false); } -void TmLocalFromTimeT(time_t time, struct tm* ptm) +void TmLocalFromSeconds1970(int64_t time, struct tm* ptm) { timeToTm(time, ptm, true); } -time_t TmUtcToTimeT(struct tm* ptm) +int64_t TmUtcToSeconds1970(struct tm* ptm) { - int days = 0; + int64_t days = 0; for (int y = 70; y < ptm->tm_year; y++) days += isLeapYear(y) ? 366 : 365; @@ -266,6 +237,8 @@ ptm->tm_min * 60 + ptm->tm_sec; } + + int TmSecondsBetween(struct tm* ptmLater, struct tm* ptmEarlier) { int days = 0; @@ -307,3 +280,36 @@ //Fill the day of week and the day of year part of the tm structure calculateDayOfYearAndWeek(ptm->tm_year, ptm->tm_mon, ptm->tm_mday, &ptm->tm_yday, &ptm->tm_wday); } +void TmIncrement(struct tm* ptm) +{ + ptm->tm_sec++; + if (ptm->tm_sec > 59) + { + ptm->tm_sec = 0; + ptm->tm_min++; + } + if (ptm->tm_min > 59) + { + ptm->tm_min = 0; + ptm->tm_hour++; + } + if (ptm->tm_hour > 23) + { + ptm->tm_hour = 0; + ptm->tm_wday++; + if (ptm->tm_wday > 6) ptm->tm_wday = 0; + ptm->tm_yday++; + ptm->tm_mday++; + if (ptm->tm_mday > monthLength(ptm->tm_year, ptm->tm_mon)) + { + ptm->tm_mon++; + if (ptm->tm_mon > 11) + { + ptm->tm_year++; + ptm->tm_yday = 0; + ptm->tm_mon = 0; + } + ptm->tm_mday = 1; + } + } +}