打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Linux C 中获取local日期和时间 time()&localtime()函数

1.  time() 函数

/*  time - 获取计算机系统当前的日历时间(Calender Time)
 *         处理日期时间的函数都是以本函数的返回值为基础进行运算
 *
 *  函数原型:
 *      #include <time.h>
 *  
 *      time_t time(time_t *calptr);
 *
 *  返回值:
 *      成功:秒数,从1970-1-1,00:00:00
 *
 *  使用:
 *      time_t now;
 *  
 *      time(&now); // == now = time(NULL); */

 2.  localtime() 函数

/*
 *  localtime - 将时间数值变换成本地时间,考虑到本地时区和夏令时标志
 *
 *  函数声明:
 *      #include <time.h>
 *
 *      struct tm * localtime(const time_t *timer);
 * */

/*  struct tm 结构
 *
 *  此结构体空间由内核自动分配,而且不需要去释放它 */ struct tm {    int tm_sec;     /*秒,    范围从0到59 */
    int tm_min;     /*分,    范围从0到59 */
    int tm_hour;    /*小时,  范围从0到23 */
    int tm_mday;    /*一个月中的第几天,范围从1到31 */
    int tm_mon;     /*月份,  范围从0到11 */
    int tm_year;    /*自 1900起的年数 */
    int tm_wday;    /*一周中的第几天,范围从0到6 */
    int tm_yday;    /*一年中的第几天,范围从0到365 */
    int tm_isdst;   /*夏令时 */};

3. Demo 

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <time.h>#define _DATETIME_SIZE  32// GetDate - 获取当前系统日期/**
 *  函数名称:GetDate
 *  功能描述:取当前系统日期
 *
 *  输出参数:char * psDate  - 系统日期,格式为yyymmdd
 *  返回结果:0 -> 成功 */int GetDate(char * psDate){
    time_t nSeconds;    struct tm * pTM;
    
    time(&nSeconds); // 同 nSeconds = time(NULL);
    pTM = localtime(&nSeconds);    
    /* 系统日期,格式:YYYMMDD */
    sprintf(psDate,"%04d-%02d-%02d", 
            pTM->tm_year + 1900, pTM->tm_mon + 1, pTM->tm_mday);    
    return 0;
}// GetTime  - 获取当前系统时间/**
 *  函数名称:GetTime
 *  功能描述:取当前系统时间
 *
 *  输出参数:char * psTime -- 系统时间,格式为HHMMSS
 *  返回结果:0 -> 成功 */int GetTime(char * psTime) {
    time_t nSeconds;    struct tm * pTM;
    
    time(&nSeconds);
    pTM = localtime(&nSeconds);    
    /* 系统时间,格式: HHMMSS */
    sprintf(psTime, "%02d:%02d:%02d",
            pTM->tm_hour, pTM->tm_min, pTM->tm_sec);           
    return 0;       
}// GetDateTime - 取当前系统日期和时间/**
 *  函数名称:GetDateTime
 *  功能描述:取当前系统日期和时间
 *
 *  输出参数:char * psDateTime -- 系统日期时间,格式为yyymmddHHMMSS
 *  返回结果:0 -> 成功 */int GetDateTime(char * psDateTime) {
    time_t nSeconds;    struct tm * pTM;
    
    time(&nSeconds);
    pTM = localtime(&nSeconds);    /* 系统日期和时间,格式: yyyymmddHHMMSS */
    sprintf(psDateTime, "%04d-%02d-%02d %02d:%02d:%02d",
            pTM->tm_year + 1900, pTM->tm_mon + 1, pTM->tm_mday,
            pTM->tm_hour, pTM->tm_min, pTM->tm_sec);            
    return 0;
}

// 测试代码int main()
{    int ret;    char DateTime[_DATETIME_SIZE];
    
    memset(DateTime, 0, sizeof(DateTime));    
    /* 获取系统当前日期 */
    ret = GetDate(DateTime);    if(ret == 0) 
        printf("The Local date is %s\n", DateTime);    else 
        perror("GetDate error!");
    
    memset(DateTime, 0, sizeof(DateTime));    /* 获取当前系统时间 */
    ret = GetTime(DateTime);    if(ret == 0) 
        printf("The Local time is %s\n", DateTime);    else 
        perror("GetTime error!");
    
    memset(DateTime, 0, sizeof(DateTime));    /* 获取系统当前日期时间 */
    ret = GetDateTime(DateTime);    if(ret == 0) 
        printf("The Local date and time is %s\n", DateTime);    else 
        perror("GetDateTime error!");    
    
    
    return 0;
}

运行结果

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Windows获取当前系统时间函数总结
LINUX 时间和日期
Python常用时间函数
Python 日期和时间
PHP中的日期相关函数(三)
C/C++时间函数的使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服