/*計算當前時間到格林威治時間總共過了多少秒,以當前北京地區東八區時間爲準*/
unsigned long mktime_second(const unsigned int year0, const unsigned int mon0,
const unsigned int day, const unsigned int hour,
const unsigned int min, const unsigned int sec){
int leap_years = 0;
unsigned long days = 0;
unsigned long seconds = 0;
unsigned long resultValue = 0;
int i = 0;
int year = year0 - 1 ;
int TIME_ZONE = 8;//用於表示當前時區,=8表示北京時區東八區,單位小時,因爲比本初子午線時間快8個小時
////
//// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12
const int month_days[] = {31, 28, 31,30,31, 30,31,31, 30,31,30,31};
int isleapyear = 0;
leap_years = year/4 - year/100;///計算普通閏年
leap_years += year/400;///加上世紀閏年
////閏年爲366天,平年爲365天
days = year * 365 + leap_years;////如果當前年份是2000年,則到此便計算出了從公元0年初到1999年尾的天數
///今年是否是閏年
if((year0%4 == 0 && year0 % 100!=0) || year0%400==0){
isleapyear = 1;///今年是閏年
}
////按平年計算,到上個月爲止總共度過的天數
for(i=0;i<mon0 - 1;i++){
days += month_days[i];
}
if(mon0 >2){
days +=isleapyear;////2月份閏年要按29天計算
}
days= days + day - 1;
///days應該減去1970年以前的天數,1970/1/1 0:0:0 0
/// year = 1969 leap_years = 1969/4-1969/100 + 1969/400 = 492 - 19 + 4 = 477
/// isleapyear = 0
/// days = 1969 * 365 + 477 = 719162
///
///
///考慮到時區的問題,實際秒鐘數據應該在當前小時的基礎之上加上時區時間TIME_ZONE
///即在北京時間東八區,實際應該計算當前時間到1970/1/1 08:0:0 0的秒鐘數
///即 seconds = 8 * 60 * 60
seconds = (hour) * 60 * 60 +
(min) * 60 + sec;
resultValue = (days - 719162) * 24 * 60 * 60;
resultValue += seconds;
resultValue -= ((unsigned long)TIME_ZONE)*60*60;
return resultValue;
}
根據系統時間計算格林威治時間
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.