Implemented from and to epoch

This commit is contained in:
Ali Mashtizadeh 2014-11-24 22:21:37 -08:00
parent bcc331a3d8
commit f58694ca12
4 changed files with 103 additions and 5 deletions

View File

@ -22,6 +22,7 @@
#include "../dev/console.h"
extern void KTime_Init();
extern void RTC_Init();
extern void PS2_Init();
extern void PCI_Init();
@ -134,6 +135,7 @@ void Machine_Init()
PMap_Init();
MachineBoot_AddMem();
XMem_Init();
KTime_Init();
IRQ_Init();
LAPIC_Init();

View File

@ -69,6 +69,9 @@ RTC_ReadTime()
tm.year += 2000;
tm.yday = -1;
tm.wday -= 1;
tm.month -= 1;
KTime_SetTime(&tm);
KTime_ToEpoch(&tm);
}

View File

@ -15,11 +15,16 @@ typedef struct KTime {
int yday;
} KTime;
typedef uint64_t UnixEpoch;
typedef uint64_t UnixEpochNS;
void KTime_Fixup(KTime *tm);
uint64_t KTime_ToEpoch(const KTime *tm);
void KTime_FromEpoch(uint64_t time, KTime *tm);
UnixEpoch KTime_ToEpoch(const KTime *tm);
void KTime_FromEpoch(UnixEpoch time, KTime *tm);
void KTime_SetTime(const KTime *tm);
void KTime_Tick(int rate);
UnixEpoch KTime_GetEpoch();
UnixEpochNS KTime_GetEpochNS();
#endif /* __SYS_KTIME_H__ */

View File

@ -75,7 +75,7 @@ KTime_Fixup(KTime *tm)
}
}
uint64_t
UnixEpoch
KTime_ToEpoch(const KTime *tm)
{
uint64_t days = 0;
@ -109,8 +109,57 @@ KTime_ToEpoch(const KTime *tm)
}
void
KTime_FromEpoch(uint64_t epoch, KTime *tm)
KTime_FromEpoch(UnixEpoch epoch, KTime *tm)
{
uint64_t secs, mins, hours;
uint64_t days;
uint64_t y, m;
// Compute seconds
secs = epoch % (60 * 60 * 24);
days = epoch / (60 * 60 * 24);
mins = secs / 60;
secs = secs % 60;
// Compute minutes
hours = mins / 60;
mins = mins % 60;
// Compute hours
hours = hours % 24;
tm->sec = secs;
tm->min = mins;
tm->hour = hours;
tm->wday = (days + 3) % 7;
for (y = 1970; ; y++) {
uint64_t daysOfYear;
if (KTime_IsLeapYear(y)) {
daysOfYear = 366;
} else {
daysOfYear = 365;
}
if (days < daysOfYear) {
tm->yday = days;
tm->year = y;
break;
}
days -= daysOfYear;
}
for (m = 0; ; m++) {
uint64_t daysOfMonth = KTime_DaysInMonth(tm->year, m);
if (days < daysOfMonth) {
tm->mday = days;
tm->month = m;
break;
}
days -= daysOfMonth;
}
}
void
@ -128,6 +177,40 @@ KTime_GetTime(const KTime *tm)
{
}
UnixEpoch
KTime_GetEpoch()
{
uint64_t tscDiff;
uint64_t epoch;
Spinlock_Lock(&ktimeLock);
tscDiff = Time_GetTSC() - ktimeLastTSC;
if (ticksPerSecondStable)
epoch = ktimeLastEpoch + tscDiff / ticksPerSecondStable;
else
epoch = ktimeLastEpoch + tscDiff / ticksPerSecond;
Spinlock_Unlock(&ktimeLock);
return epoch;
}
UnixEpochNS
KTime_GetEpochNS()
{
uint64_t tscDiff;
uint64_t epoch;
Spinlock_Lock(&ktimeLock);
tscDiff = Time_GetTSC() - ktimeLastTSC;
if (ticksPerSecondStable)
epoch = ktimeLastEpoch + tscDiff / (ticksPerSecondStable / 1000000);
else
epoch = ktimeLastEpoch + tscDiff / (ticksPerSecond / 1000000);
Spinlock_Unlock(&ktimeLock);
return epoch;
}
void
KTime_Tick(int rate)
{
@ -159,11 +242,16 @@ KTime_Tick(int rate)
void
Debug_Date()
{
UnixEpoch epoch = KTime_GetEpoch();
KTime tm;
KTime_FromEpoch(epoch, &tm);
kprintf("%s %s %d %02d:%02d:%02d %04d\n",
dayOfWeek[tm.wday - 1], months[tm.month - 1],
dayOfWeek[tm.wday], months[tm.month],
tm.mday, tm.hour, tm.min, tm.sec, tm.year);
kprintf("Epoch: %lu\n", epoch);
}
REGISTER_DBGCMD(date, "Print date", Debug_Date);