linux获取系统时间

想要得到系统时间需要调用Linux系统函数获取。
#include 
    
    
     
     
#include 
     
     
      
      

/// 系统时间结构
typedef struct SYSTEM_TIME
{
    int  year;///< 年。   
    int  month;///< 月,January = 1, February = 2, and so on.   
    int  day;///< 日。   
    int  wday;///< 星期,Sunday = 0, Monday = 1, and so on   
    int  hour;///< 时。   
    int  minute;///< 分。   
    int  second;///< 秒。   
    int  isdst;///< 夏令时标识。   
}SYSTEM_TIME;

int SystemGetCurrentTime(SYSTEM_TIME *ptime)
{
    time_t      t = time(NULL);
    struct tm   time;
    
    localtime_r(&t, &time);
    
    ptime->second   = time.tm_sec; 			
    ptime->minute   = time.tm_min;			
    ptime->hour     = time.tm_hour;			
    ptime->wday     = time.tm_wday;			
    ptime->day      = time.tm_mday;			
    ptime->month    = time.tm_mon + 1;		
    ptime->year     = time.tm_year + 1900;		
    ptime->isdst    = 0;
    
    return 0;
}

int main(int argc, char **argv)
{
    SYSTEM_TIME myTime;
    SystemGetCurrentTime(&myTime);
    printf("%d-%d-%d %d:%d:%d weekday:%d dst:%d\n", myTime.year, myTime.month, myTime.day, myTime.hour, myTime.minute, myTime.second, myTime.wday, myTime.isdst);
    return 0;
}
     
     
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值