PatchworkOS
Loading...
Searching...
No Matches
mktime.c
Go to the documentation of this file.
1#include <time.h>
2
3#include "common/time_utils.h"
4
5time_t mktime(struct tm* timePtr)
6{
7 if (!timePtr)
8 {
9 return (time_t)(-1);
10 }
11
12 _time_normalize(timePtr);
13 _time_day_of_week(timePtr);
14 _time_day_of_year(timePtr);
15
16 int64_t totalDays = 0;
17 int32_t year = timePtr->tm_year + 1900;
18
19 for (int32_t y = 1970; y < year; y++)
20 {
21 totalDays += 365 + (_time_is_leap_year(y) ? 1 : 0);
22 }
23
24 totalDays += timePtr->tm_yday;
25
26 time_t epochTime = (time_t)(totalDays * 86400LL + timePtr->tm_hour * 3600 + timePtr->tm_min * 60 + timePtr->tm_sec);
27
28 if (timePtr->tm_isdst > 0)
29 {
30 epochTime -= 3600;
31 }
32
33 return epochTime;
34}
time_t mktime(struct tm *timePtr)
Definition mktime.c:5
int64_t y
Definition main.c:153
__INT32_TYPE__ int32_t
Definition stdint.h:14
__INT64_TYPE__ int64_t
Definition stdint.h:16
Definition time.h:21
int tm_year
Definition time.h:27
int tm_hour
Definition time.h:24
int tm_sec
Definition time.h:22
int tm_isdst
Definition time.h:30
int tm_yday
Definition time.h:29
int tm_min
Definition time.h:23
long long unsigned time_t
Definition time_t.h:4
bool _time_is_leap_year(int32_t year)
Definition time_utils.c:24
void _time_normalize(struct tm *timePtr)
Definition time_utils.c:38
void _time_day_of_year(struct tm *timePtr)
Definition time_utils.c:124
void _time_day_of_week(struct tm *timePtr)
Definition time_utils.c:105