Linux下获取/设置时间的一些函数

一. time

头文件:#include <time.h>

原型:time_t time(time_t *t)

time_t的定义:

typedef __darwin_time_t  time_t; 
typedef long __darwin_time_t;
返回值:UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数

例1

#include <stdio.h>
#include <time.h>
 
int main ()
{
  time_t seconds;
 
  seconds = time(NULL);
  printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);
  
  return(0);
}

二、localtime  localtime_r

头文件:#include <time.h>

原型:struct tm *localtime(const time_t *timep)

原型:struct tm *localtime_r(const time_t *timep, struct tm *result);

结构体tm

struct tm {
   int tm_sec;    /* Seconds (0-60) */
   int tm_min;    /* Minutes (0-59) */
   int tm_hour;   /* Hours (0-23) */
   int tm_mday;   /* Day of the month (1-31) */
   int tm_mon;    /* Month (0-11) */
   int tm_year;   /* Year - 1900 */
   int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
   int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
   int tm_isdst;  /* Daylight saving time */ 
   /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ 
};

localtime 与 localtime_r区别

localtime 不是可重入 localtime_r 可重入

例2

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    time_t tNow =time(NULL);
    time_t tEnd = tNow + 1800;
    //注意下面两行的区别
    struct tm* ptm = localtime(&tNow);
    struct tm* ptmEnd = localtime(&tEnd);
 
    char szTmp[50] = {0};
    strftime(szTmp,50,"%H:%M:%S",ptm);
    char szEnd[50] = {0};
    strftime(szEnd,50,"%H:%M:%S",ptmEnd);
 
    printf("%s /n",szTmp);
    printf("%s /n",szEnd);
    
    return 0;
}

最后出来的结果是:
21:18:39
21:18:39

例3:

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <stdio.h>
 
 
int main(int argc, char *argv[])
{
    time_t tNow =time(NULL);
    time_t tEnd = tNow + 1800;
	
    struct tm ptm = { 0 };
    struct tm ptmEnd = { 0 };
    localtime_r(&tNow, &ptm);
    localtime_r(&tEnd, &ptmEnd);
    
    char szTmp[50] = {0};
    strftime(szTmp,50,"%H:%M:%S",&ptm);
    char szEnd[50] = {0};
    strftime(szEnd,50,"%H:%M:%S",&ptmEnd);
    printf("%s /n",szTmp);
    printf("%s /n",szEnd);
    
 
    return 0;
}

最后出来的结果是:
10:29:06 
10:59:06


三、gettimeofday

头文件:#include <sys/time.h>

原型:int gettimeofday(struct timeval*tv, struct timezone *tz)

tv:是保存获取时间结果的结构体

struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
}
tz:用于保存时区结果

struct timezone{
    int tz_minuteswest;/*格林威治时间往西方的时差*/
    int tz_dsttime;/*DST 时间的修正方式*/
}
返回值:成功返回0,失败返回-1,错误代码保存于errno中

四、mktime

头文件:#include <time.h>

原型:time_t mktime(struct tm *tm)

返回值:1970年1月1日以来持续时间的秒数,发生错误时返回-1。

注意:和localtime刚好相反,是将struct tm格式转换为UTC的秒,注意转换的过程会根据本地时区来转换到UTC的秒。

例4

#include<stdio.h>
#include<time.h>
 
int main(void)
{
    time_t timep;
    struct tm* p;
    time(&timep);
    printf("time():%d\n",timep);
    p=localtime(&timep);
    timep=mktime(p);
    printf("time()->localtime()->mktime():%d\n",timep);
    return 0;
}

五、stime(设置linux系统时间,精确到秒)

#define _SVID_SOURCE /*如果你使用的是glib2的话,必须先定义这个宏才能使用*/

#include <time.h>

int stime(time_t *t);

参数说明:

t是以秒为单位的时间值,从GMT1970年1月1日0时0分0秒开始计算。

返回值:

成功返回0,错误返回-1,errno错误码,EFAULT表示传递的参数错误,如时间值是无效的值,EPERM表示权限不够,注意只有root用户才有修改系统时间的权限。如果要让普通程序修改系统时间,可以先切换到root用户操作,修改完成后,再切换到普通用户,或者用命令chmod +s给执行文件加上root用户的权限。


六、settimeofday(设置linux系统时间,精确到微秒)

#include <time.h>

int settimeofday(const struct timeval *tv , const struct timezone *tz);

struct timeval {undefined

    time_t      tv_sec;     /* seconds */

    suseconds_t tv_usec;    /* microseconds */

};

struct timezone {undefined

    int tz_minuteswest;     /* minutes west of Greenwich */

    int tz_dsttime;         /* type of DST correction */

};

tz参数为时区,时区结构中tz_dsttime在linux中不支持,应该置为0,通常将参数tz设置为NULL,表示使用当前系统的时区。该函数是glib中的,但在mingw中没有实现。

实例:

 1 #include <stdio.h>
  2 #include <sys/time.h>
  3 #include <unistd.h>
  4 
  5 int main(void)
  6 {
  7         struct timeval stime;
  8 
  9         gettimeofday(&stime,NULL);
 10         printf("现在的时间秒数是:%ld,毫秒数是:%ld\n现在的时间是:",stime.tv_sec,stime.tv_usec);
 11         fflush(stdout);
 12         system("date");
 13 
 14         stime.tv_sec = 123456789;
 15         settimeofday(&stime,NULL);
 16         printf("现在的时间秒数是:%ld,毫秒数是:%ld\n现在的时间是:",stime.tv_sec,stime.tv_usec);
 17         fflush(stdout);
 18         system("date");
 19 
 20 
 21 
 22         return 0;
 23 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值