问题:
1 utc时间是什么?
世界时间, 统一的时间.
2 什么是时区?
时区有正有负
3 如何表示负数?
对应的正数的二进制的每一位取反后, 然后再加1
4 如何将负数转换为对应的正数?
将负数减去 1后, 再取反. ~(负数-1)
5 如何计算utc时间?
当时区为正数时候:
a) 当前hour - 时区 >= 0
b)当前hour -时区< 0时候
如果当天是1月1日, 需要特殊处理
当时区为负数时候:
将负数转化为正数.
c) 当前hour + 正数 <= 24
d)当前hour + 正数 > 24
如果是当月的最后一天, 也需要特殊处理.
void TransfertoUTCtimeformat(struct tm *curTime, char timeZone, struct tm * UTCtime)
{
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if((curTime == NULL) || (UTCtime == NULL))
return;
memcpy(UTCtime,curTime,sizeof(struct tm));
if(isLeapYear(curTime->tm_year))
month_day[1] = 29;
if((timeZone & 0x80) == 0)
{
if((curTime->tm_hour - timeZone ) < 0 )
{
UTCtime->tm_hour = 24 + curTime->tm_hour - timeZone;
if(curTime->tm_mday == 1)
{
if( curTime->tm_mon == 1)
{
UTCtime->tm_mon = 12;
UTCtime->tm_year = curTime->tm_year - 1 ;
UTCtime->tm_mday =31;
}
else
{
UTCtime->tm_mon = curTime->tm_mon - 1;
UTCtime->tm_mday = month_day[UTCtime->tm_mon -1 ];
}
}
else
UTCtime->tm_mday = curTime->tm_mday - 1;
}
else
UTCtime->tm_hour = curTime->tm_hour - timeZone;
}
else
{
timeZone = ~(timeZone -1);
if((curTime->tm_hour + timeZone ) >= 24 )
{
UTCtime->tm_hour = curTime->tm_hour + timeZone - 24;
if(curTime->tm_mday == month_day[curTime->tm_mon -1])
{
if( curTime->tm_mon == 12)
{
UTCtime->tm_mon = 1;
UTCtime->tm_mday = 1;
UTCtime->tm_year = curTime->tm_year + 1 ;
}
else
{
UTCtime->tm_mon = curTime->tm_mon + 1;
UTCtime->tm_mday = 1;
}
}
else
UTCtime->tm_mday = curTime->tm_mday + 1;
}
else
{
UTCtime->tm_hour = curTime->tm_hour + timeZone;
}
}
}