#include
#include
#include
/// @brief 返回1970年1月1日到当前的时间,以毫秒为单位显示
time_t GetTimerInterval(void)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec*1000 + tz.tz_minuteswest*60*1000 + tv.tv_usec/1000 );
}
/// @brief 返回1970年1月1日到当前的时间,以秒为单位显示
time_t GetTimeSecond(void)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return tv.tv_sec + tz.tz_minuteswest*60;
}
/// @brief UTC时间转换成当前时间,以秒为单位
time_t UTCToLocalTime(time_t utcTime)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return utcTime + tz.tz_minuteswest*60;
}
/// @brief 当前时间转换成UTC时间,以秒为单位
time_t LocalToUTCTime(time_t localTime)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return localTime + tz.tz_minuteswest*60;
}
/// @brief 获得当前系统时间
void GetSystemTime(int *year, int *month, int *day, int *week, int *hour, int *minute, int *second)
{
struct tm *tm_time;
time_t curTime = GetTimeSecond();
tm_time = gmtime(&curTime);
if(year != NULL)
{
*year = tm_time->tm_year + 1900;
}
if(month != NULL)
{
*month = tm_time->tm_mon+1;
}
if(day != NULL)
{
*day = tm_time->tm_mday;
}
if(week != NULL)
{
*week = tm_time->tm_wday;
}
if(hour != NULL)
{
*hour = tm_time->tm_hour;
}
if(minute != NULL)
{
*minute = tm_time->tm_min;
}
if(second != NULL)
{
*second = tm_time->tm_sec;
}
}
void LocalSetSystemTime(int year,int month,int day,int hour,int minute,int second,int tmieZone)
{
struct timeval tv;
struct timezone tz;
struct tm tm_time;
tm_time.tm_sec = second;
tm_time.tm_min = minute;
tm_time.tm_hour = hour;
tm_time.tm_mday = day;
tm_time.tm_mon = month;
tm_time.tm_year = year;
tm_time.tm_wday = 0;
tm_time.tm_yday = 0;
tm_time.tm_isdst = 0;
tv.tv_sec = mktime(&tm_time) - timezone*60*60;
tv.tv_usec = 0;
tz.tz_minuteswest = timeZone*60
tz.tz_dsttime = 0;
settimeofday(&tv,&tz);
}
void UTCSetSystemTime(int year, int month, int day, int hour, int minute, int second, int timeZone)
{
struct timeval tv;
struct timezone tz;
struct tm tm_time;
tm_time.tm_sec = second;
tm_time.tm_min = minute;
tm_time.tm_hour = hour;
tm_time.tm_mday = day;
tm_time.tm_mon = month;
tm_time.tm_year = year;
tm_time.tm_wday = 0;
tm_time.tm_yday = 0;
tm_time.tm_isdst = 0;
tv.tv_sec = mktime( &tm_time);
tv.tv_usec = 0;
tz.tz_minuteswest = timeZone * 60;
tz.tz_dsttime = 0;
settimeofday( &tv, &tz);
}
int main(int argc, char *argv[])
{
//time_t t;
int t;
#if 0
t = GetTimerInterval();
t = GetTimeSecond();
printf("now is %d\n",t);
#endif
int year,month,day,week,hour,minute,second;
GetSystemTime(&year,&month,&day,&week,&hour,&minute,&second);
printf("%d-%d-%dweek[%d][%d:%d:%d]\n",year,month,day,week,hour,minute,second);
return 0;
}