linux下时间的处理

1 篇文章 0 订阅
在linux下获取时间 ,首先需要获取时刻,再通过时刻来获取具体的年/月/日/时/分/秒等信息。

1 采用time方法获取当前时间的方法如下
    函数说明
#include <time.h>
//返回当前时间相对于1970年1月1日0时0分0秒的秒数
//其中time_t 是long int类型,表示某一时刻;如果参数为NULL值,则表示当前时间的时刻,否则参数和返回值都会给值。

time_t time (time_t *__timer)
//通过时刻获取具体的时间。
struct tm *localtime (__const time_t *__timer)

两者结合,获取系统当前时间的代码如下:
    time_t tim=time(NULL);
    tm* now=localtime(&tim);
    printf("\n@Time: %04d:%02d:%02d  %02d:%02d:%02d \n"
           ,(now->tm_year+1900),(now->tm_mon+1),now->tm_mday
           ,now->tm_hour,now->tm_min,now->tm_sec);

2 通过clock_gettime()获取当前时间的方法
      函数说明
int clock_gettime (clockid_t __clock_id, struct timespec *__tp)
其中 clockid_t 的值主要如下:
//绝对时间,表示系统当前时间
#   define CLOCK_REALTIME        0
//相对时间,它不受系统时间修改的影响,相对某个时刻的时间
#   define CLOCK_MONOTONIC        1
/* High-resolution timer from the CPU.  */
#   define CLOCK_PROCESS_CPUTIME_ID    2
/* Thread-specific CPU-time clock.  */
#   define CLOCK_THREAD_CPUTIME_ID    3

支持至纳秒
struct timespec
  {
    __time_t tv_sec;        /* Seconds.  */
    long int tv_nsec;        /* Nanoseconds.  */

  };
代码如下:
    struct timespec ts;
    clock_gettime (CLOCK_REALTIME, &ts); //获取时刻
    tm* now=localtime(&ts.tv_sec);
    printf("\n@Time: %04d:%02d:%02d  %02d:%02d:%02d \n"
           ,(now->tm_year+1900),(now->tm_mon+1),now->tm_mday
           ,now->tm_hour,now->tm_min,now->tm_sec);

3 通过gettimeofday()获取当前时间的方法
//此方法只支持至微
  struct timeval
  {
    __time_t tv_sec;        /* Seconds.  */
    __suseconds_t tv_usec;    /* Microseconds.  */
  };
代码如下:
  struct timeval tv;
  gettimeofday(&tv, NULL);
  tm* now=localtime(&ts.tv_sec);
    printf("\n@Time: %04d:%02d:%02d  %02d:%02d:%02d \n"
           ,(now->tm_year+1900),(now->tm_mon+1),now->tm_mday
           ,now->tm_hour,now->tm_min,now->tm_sec);

4 时间如何转为时刻,如:
    tm* now;
    time_t tick=mktime(now);

5 时刻转为UTC时间用gmtime


相关函数的说明
1time_t time(time_t *time);
This returns the current calendar time of the system in number of seconds elapsed since January 1, 1970. If the system has no time, .1 is returned.
2char *ctime(const time_t *time);
This returns a pointer to a string of the form day month year hours:minutes:seconds year\n\0.
3struct tm *localtime(const time_t *time);
This returns a pointer to the tm structure representing local time.
4clock_t clock(void);
This returns a value that approximates the amount of time the calling program has been running. A value of .1 is returned if the time is not available.
5char * asctime ( const struct tm * time );
This returns a pointer to a string that contains the information stored in the structure pointed to by time converted into the form: day month date hours:minutes:seconds year\n\0
6struct tm *gmtime(const time_t *time);
This returns a pointer to the time in the form of a tm structure. The time is represented in Coordinated Universal Time (UTC), which is essentially Greenwich Mean Time (GMT).
7time_t mktime(struct tm *time);
This returns the calendar-time equivalent of the time found in the structure pointed to by time.
8double difftime ( time_t time2, time_t time1 );
This function calculates the difference in seconds between time1 and time2.
9size_t strftime();
This function can be used to format date and time a specific format.







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值