2010年10月28日 星期四

時間轉換函式~秒<---->日期 (從2000年)


bit isleap(int y)
{
return y%4==0&&y%100!=0 || y%400==0;
}

int DaysOfMonth( int y, int m )
{
switch(m)
{
case 1: return 31;
case 2: return isleap(y)?29:28;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
}
return 30;
}


int DaysOfYear( int y )
{
if( isleap(y) )
return 366;
return 365;
}

unsigned long SecsFrom2000( int y, int m, int d, int hh, int mm, int ss )
{
int i;
unsigned long day = 0;
for( i=2000; i<y; i++ )
day += DaysOfYear(i);
for( i=1; i<m; i++ )
day += DaysOfMonth(y,i);
day += d;
day = day*86400L + hh*3600L + mm*60L + ss*1L;
return day;
}

void SecToDateTime( unsigned long sec, cdate * pdate, ctime * ptime )
{
unsigned long day;

day = sec/86400;

pdate->yy = 2000;
while( day > DaysOfYear( pdate->yy ) )
{
day-= DaysOfYear( pdate->yy );
pdate->yy++;
}

pdate->mm = 1;
while( day > DaysOfMonth( pdate->yy, pdate->mm ) )
{
day-= DaysOfMonth( pdate->yy, pdate->mm );
pdate->mm++;
}

pdate->dd = day;

ptime->hh = (sec/3600)%24;
ptime->mm = (sec/60)%60;
ptime->ss = (sec)%60;
}

沒有留言:

VirtualBox 空間減肥

sdelete64 -z c: VBoxManage  modifymedium  disk  "/Users/fellow/VirtualBox VMs/Win10/Win10.vdi"  --compact *.vdi 路徑可以在 VirtualBox 儲...