time.cpp
来自「SimpleGraphicOperatingSystem 32位图形化操作系统 」· C++ 代码 · 共 228 行
CPP
228 行
#include <OsDef.h>
#include <System.h>
#include <Api.h>
namespace System{
/* Lib */
#define SECS_PER_MIN 60
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
#define SECS_PER_YEAR (365*SECS_PER_DAY)
#define __isleap(year) ( (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0) )
static const unsigned short int __mon_yday[2][13] =
{
/* Normal years. */
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
/* Leap years. */
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
};
Time::Time()
{
//GetSystemTime();
}
Time::Time( unsigned long ticks )
{
myTicks = ticks;
MakeTime();
}
Time::~Time()
{
}
Time& Time::operator= ( unsigned long ticks )
{
myTicks = ticks;
MakeTime();
return *this;
}
Time& Time::operator= ( Time& t )
{
this->myTicks = t.myTicks;
MakeTime();
return *this;
}
int Time::GetSystemTime()
{
myTicks = KGetSystemTime(0);
MakeTime();
}
int Time::MakeTime()
{
MakeTime( myTicks );
}
int Time::MakeTime( unsigned long ticks )
{
long int days, rem, y;
const unsigned short int *ip;
days = ticks / SECS_PER_DAY;
rem = ticks % SECS_PER_DAY;
rem += 0; // offset
while (rem < 0)
{
rem += SECS_PER_DAY;
--days;
}
while (rem >= SECS_PER_DAY)
{
rem -= SECS_PER_DAY;
++days;
}
_hour = rem / SECS_PER_HOUR;
rem %= SECS_PER_HOUR;
_min = rem / 60;
_sec = rem % 60;
_wday = (4 + days) % 7;
if (_wday < 0)
_wday += 7;
y = 1970;
#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
while (days < 0 || days >= (__isleap (y) ? 366 : 365))
{
/* Guess a corrected year, assuming 365 days per year. */
long int yg = y + days / 365 - (days % 365 < 0);
/* Adjust DAYS and Y to match the guessed year. */
days -= ((yg - y) * 365
+ LEAPS_THRU_END_OF (yg - 1)
- LEAPS_THRU_END_OF (y - 1) );
y = yg;
}
_isleap = __isleap(y);
_year = y - 1900;
if (_year != y - 1900){
/* The year cannot be represented due to overflow. */
return 0;
}
_yday = days;
ip = __mon_yday[__isleap(y)];
for (y = 11; days < (long int) ip[y]; --y)
continue;
days -= ip[y];
_mon = y;
_mday = days + 1;
return 1;
}
Time& Time::operator = ( const void* buf )
{
TIME *t = (TIME*) buf;
short month, year;
time_t res;
month = t->mon;
if( t->year<70 )
year = t->year + month / 12 + 2000;
else
year = t->year + month / 12 + 1900;
month %= 12;
if (month < 0)
{
year -= 1;
month += 12;
}
res = (year - 1970) * 365 + (year - 1969) / 4 + __mon_yday[0][month];
res = (year - 1970) * 365 + __mon_yday[0][month];
if (month <= 1)
year -= 1;
res += (year - 1968) / 4;
res -= (year - 1900) / 100;
res += (year - 1600) / 400;
res += t->mday;
res -= 1;
res *= 24;
res += t->hour;
res *= 60;
res += t->min;
res *= 60;
res += t->sec;
myTicks = res;
MakeTime();
return *this;
}
int Time::Year()
{
return _year;
}
int Time::Month()
{
return _mon;
}
int Time::Day()
{
return _mday;
}
int Time::Minute()
{
return _min;
}
int Time::Second()
{
return _sec;
}
int Time::Week()
{
return _wday;
}
int Time::DayOfYear()
{
return _yday;
}
int Time::Hour()
{
return _hour;
}
int Time::IsLeapYear()
{
return _isleap;
}
string Time::ToString(int i)
{
if(i==1){
static const char* weeks[]={"星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六" };
string strTime = string().FromInteger( 1900+Year(), 10 ) + string("年") +
string().FromInteger( Month()+1, 10 ) + string("月") + string().FromInteger( Day(), 10 ) +
string("日 ");
string hour = string().FromInteger( Hour(), 10 );
if( hour.Length()==1 ) hour = string("0") + hour;
string min = string().FromInteger( Minute(), 10 );
if( min.Length()==1 ) min = string("0") + min;
string sec = string().FromInteger( Second(), 10 );
if( sec.Length()==1 ) sec = string("0") + sec;
strTime = strTime + hour + string(":") + min + string(":") + sec + string(" ");
strTime = strTime + string(weeks[Week()]);
return strTime;
}else{
string strTime;
string hour = string().FromInteger( Hour(), 10 );
if( hour.Length()==1 ) hour = string("0") + hour;
string min = string().FromInteger( Minute(), 10 );
if( min.Length()==1 ) min = string("0") + min;
strTime = strTime + hour + string(":") + min + string(" ");
return strTime;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?