在linux下获取时间 ,首先需要获取时刻,再通过时刻来获取具体的年/月/日/时/分/秒等信息。
1 采用time方法获取当前时间的方法如下:
函数说明
#include <time.h>
#include <time.h>
//返回当前时间相对于1970年1月1日0时0分0秒的秒数
//其中time_t 是long int类型,表示某一时刻;如果参数为NULL值,则表示当前时间的时刻,否则参数和返回值都会给值。
//其中time_t 是long int类型,表示某一时刻;如果参数为NULL值,则表示当前时间的时刻,否则参数和返回值都会给值。
time_t time (time_t *__timer)
//通过时刻获取具体的时间。
struct tm *localtime (__const time_t *__timer)
两者结合,获取系统当前时间的代码如下:
time_t tim=time(NULL);
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);
,(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)
//相对时间,它不受系统时间修改的影响,相对某个时刻的时间
# 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. */
其中 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
{
__time_t tv_sec; /* Seconds. */
long int tv_nsec; /* Nanoseconds. */
};
代码如下:
struct timespec ts;
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);
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);
,(now->tm_year+1900),(now->tm_mon+1),now->tm_mday
,now->tm_hour,now->tm_min,now->tm_sec);
3
通过gettimeofday()获取当前时间的方法:
gettimeofday(&tv, NULL);
//此方法只支持至微
秒
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
代码如下:
struct timeval tv;
代码如下:
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
相关函数的说明
1 | time_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. |
2 | char *ctime(const time_t *time); This returns a pointer to a string of the form day month year hours:minutes:seconds year\n\0. |
3 | struct tm *localtime(const time_t *time); This returns a pointer to the tm structure representing local time. |
4 | clock_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. |
5 | char * 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 |
6 | struct 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). |
7 | time_t mktime(struct tm *time); This returns the calendar-time equivalent of the time found in the structure pointed to by time. |
8 | double difftime ( time_t time2, time_t time1 ); This function calculates the difference in seconds between time1 and time2. |
9 | size_t strftime(); This function can be used to format date and time a specific format. |