luxos/Kernel/library/time.c

99 lines
2.1 KiB
C
Raw Normal View History

#include <time.h>
extern volatile TimeSystem _internal_time;
extern uint32 _internal_frequency_hz;
// Length of months (summed)
const int16 MonthLen[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
TimeSystem ConvertTimeToTimeSystem (Time t)
{
TimeSystem sys = {0,0};
t.Year--; t.Month--; t.Day--; t.WeekDay--;
sys.Time = (uint32)(t.Hour * 3600000) + (uint32)(t.Minute * 60000) + (uint32)(t.Second * 1000) + (uint32)t.Milisecond;
if (t.Year < 0) return sys;
sys.Date = (uint32)(t.Day) + (uint32)(MonthLen[t.Month])
+ (uint32)((t.Year/4) * (365*4 + 1)) + (uint32)(t.Year%4 * 365);
return sys;
}
Time ConvertTimeSystemToTime (TimeSystem InternalTime)
{
Time t;
t.Milisecond = InternalTime.Time % 1000;
t.Second = (InternalTime.Time / 1000) % 60;
t.Minute = (InternalTime.Time / 60000) % 60;
t.Hour = (InternalTime.Time / 3600000);
uint32 DayOfYear = (InternalTime.Date % 1461) % 365;
t.Year = (InternalTime.Date / 1461)*4 + (InternalTime.Date % 1461)/365 + 1;
t.Month = 11;
while ((int32)DayOfYear < (MonthLen[t.Month] + (t.Year % 4 == 0 && t.Month>1))) t.Month--;
t.WeekDay = InternalTime.Date % 7;
t.Day = DayOfYear - MonthLen[t.Month] - (t.Year % 4 == 0 && t.Month>1);
t.Month++; t.WeekDay++; t.Day++;
return t;
}
uint16 TimeCalculateWeekday (Time t)
{
t.Year--;
uint32 d = (uint32)(t.Day-1) + (uint32)(MonthLen[t.Month-1]) +
+ (uint32)((t.Year/4) * (365*4 + 1)) + (uint32)(t.Year%4 * 365);
return 1 + (d%7);
}
TimeSystem TimeGetInternalTime()
{
return _internal_time;
}
void TimeSetInternalTime(TimeSystem t)
{
_internal_time = t;
}
uint32 TimeGetInternalFrequency ()
{
return _internal_frequency_hz;
}
void TimeSetInternalFrequency (uint32 f)
{
_internal_frequency_hz = f;
}
TimeSystem _timer;
#define MILISECONDS_IN_DAY 86400000
void TimerStart (uint32 ms)
{
_timer = TimeGetInternalTime();
_timer.Time += ms;
if (_timer.Time >= MILISECONDS_IN_DAY)
{
_timer.Date++;
_timer.Time-=MILISECONDS_IN_DAY;
}
}
uint8 TimerIsDone ()
{
TimeSystem now = TimeGetInternalTime();
if (_timer.Date >= now.Date && _timer.Time > now.Time) return 0;
return 1;
}