forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoHAL_Time.cpp
123 lines (98 loc) · 3.94 KB
/
nanoHAL_Time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
#include "stdafx.h"
#include <stdint.h>
#include <nanoHAL_Time.h>
const int CummulativeDaysForMonth[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
#define IS_LEAP_YEAR(y) (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
// Number of leap years until base year, not including the target year itself.
#define NUMBER_OF_LEAP_YEARS(y) ((((y - 1) / 4) - ((y - 1) / 100) + ((y - 1) / 400)) - BASE_YEAR_LEAPYEAR_ADJUST)
#define NUMBER_OF_YEARS(y) (y - BASE_YEAR)
#define YEARS_TO_DAYS(y) ((NUMBER_OF_YEARS(y) * DAYS_IN_NORMAL_YEAR) + NUMBER_OF_LEAP_YEARS(y))
#define MONTH_TO_DAYS(y, m) (CummulativeDaysForMonth[m - 1] + ((IS_LEAP_YEAR(y) && (m > 2)) ? 1 : 0))
#define DAYS_IN_MONTH(y, m) (DaysInMonth[m - 1] + ((IS_LEAP_YEAR(y) && (m == 2)) ? 1 : 0))
#define TIMEUNIT_TO_MINUTES 600000000
#define TIMEUNIT_TO_MILLISECONDS 10000
#define MILLISECONDS_TO_SECONDS 1000
#define SECONDS_TO_MINUTES 60
#define MINUTES_TO_HOUR 60
#define HOURS_TO_DAY 24
uint64_t HAL_Time_CurrentTime()
{
return HAL_Time_SysTicksToTime(HAL_Time_CurrentSysTicks());
};
/// <summary>
/// Converts a SYSTEMTIME value to HAL time value
/// </summary>
uint64_t HAL_Time_ConvertFromSystemTime(const SYSTEMTIME *systemTime)
{
uint64_t r =
YEARS_TO_DAYS(systemTime->wYear) + MONTH_TO_DAYS(systemTime->wYear, systemTime->wMonth) + systemTime->wDay - 1;
r = (((((r * HOURS_TO_DAY) + systemTime->wHour) * MINUTES_TO_HOUR + systemTime->wMinute) * SECONDS_TO_MINUTES +
systemTime->wSecond) *
MILLISECONDS_TO_SECONDS +
systemTime->wMilliseconds) *
TIMEUNIT_TO_MILLISECONDS;
return r;
}
/// <summary>
/// Converts a SYSTEMTIME value to HAL time value, with extra ticks
/// </summary>
uint64_t HAL_Time_ConvertFromSystemTimeWithTicks(const SYSTEMTIME *systemTime, const uint32_t extraTicks)
{
uint64_t r =
YEARS_TO_DAYS(systemTime->wYear) + MONTH_TO_DAYS(systemTime->wYear, systemTime->wMonth) + systemTime->wDay - 1;
r = (((((r * HOURS_TO_DAY) + systemTime->wHour) * MINUTES_TO_HOUR + systemTime->wMinute) * SECONDS_TO_MINUTES +
systemTime->wSecond) *
MILLISECONDS_TO_SECONDS +
systemTime->wMilliseconds) *
TIMEUNIT_TO_MILLISECONDS +
extraTicks;
return r;
}
bool HAL_Time_ToSystemTime(uint64_t time, SYSTEMTIME *systemTime)
{
uint32_t ytd = 0;
uint32_t mtd = 0;
time /= TIMEUNIT_TO_MILLISECONDS;
systemTime->wMilliseconds = time % MILLISECONDS_TO_SECONDS;
time /= MILLISECONDS_TO_SECONDS;
systemTime->wSecond = time % SECONDS_TO_MINUTES;
time /= SECONDS_TO_MINUTES;
systemTime->wMinute = time % MINUTES_TO_HOUR;
time /= MINUTES_TO_HOUR;
systemTime->wHour = time % HOURS_TO_DAY;
time /= HOURS_TO_DAY;
systemTime->wDayOfWeek = (time + BASE_YEAR_DAYOFWEEK_SHIFT) % 7;
systemTime->wYear = (unsigned short)(time / DAYS_IN_NORMAL_YEAR + BASE_YEAR);
ytd = YEARS_TO_DAYS(systemTime->wYear);
if (ytd > time)
{
systemTime->wYear--;
ytd = YEARS_TO_DAYS(systemTime->wYear);
}
time -= ytd;
systemTime->wMonth = (unsigned short)(time / 31 + 1);
mtd = MONTH_TO_DAYS(systemTime->wYear, systemTime->wMonth + 1);
if (time >= mtd)
{
systemTime->wMonth++;
}
mtd = MONTH_TO_DAYS(systemTime->wYear, systemTime->wMonth);
systemTime->wDay = (unsigned short)(time - mtd + 1);
return true;
}
HRESULT HAL_Time_AccDaysInMonth(signed int year, signed int month, signed int *days)
{
*days = MONTH_TO_DAYS(year, month);
return S_OK;
}
HRESULT HAL_Time_DaysInMonth(signed int year, signed int month, signed int *days)
{
*days = DAYS_IN_MONTH(year, month);
return S_OK;
}