获取linux系统时间命令,获取linux系统当前时间函数实现

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值