linux获取系统时间和时间戳

本文介绍如何在Linux环境下使用C/C++编程获取系统时间和时间戳的方法,包括利用time、localtime、gettimeofday等函数来获取当前时间并用于文件名,以及如何计算时间差。
linux获取系统时间和时间戳

linux下c/c++编程需要系统时间,一个是获取时间戳和计算时间差,一个获取当前时间用来作为文件名等,方法和例子如下:
可以用man命令查询time,localtime,gettimeofday用法,注意struct tm和struct timeval用法;

man localtime

NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time
to broken-down time or ASCII

SYNOPSIS
#include <time.h>

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);

SEE ALSO
date(1), gettimeofday(2), time(2), utime(2), clock(3), difftime(3), strftime(3), strptime(3), timegm(3),
tzset(3), time(7)

man gettimeofday
NAME
gettimeofday, settimeofday - get / set time

SYNOPSIS
#include <sys/time.h>

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

SEE ALSO
date(1), adjtimex(2), time(2), ctime(3), ftime(3), capabilities(7), time(7)

获取当前时间用来作为文件名
#include<time.h>
#include<stdio.h>

int main()
{
	struct tm *t;
	time_t tt;
	time_t ts;

	struct tm tr = {0};

	time(&tt);
	t = localtime(&tt);
	printf("localtime %4d%02d%02d %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);

	localtime_r(&tt, &tr);
	printf("localtime_r %4d%02d%02d %02d:%02d:%02d\n", tr.tm_year + 1900, tr.tm_mon + 1, tr.tm_mday, tr.tm_hour, tr.tm_min, tr.tm_sec);

	ts = tt + 1800;
	t = localtime(&ts);
	printf("localtime %4d%02d%02d %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);

	localtime_r(&ts, &tr);
	printf("localtime_r %4d%02d%02d %02d:%02d:%02d\n", tr.tm_year + 1900, tr.tm_mon + 1, tr.tm_mday, tr.tm_hour, tr.tm_min, tr.tm_sec);

	return 0;
}



$ ./timet1
localtime 20161108 15:03:46
localtime_r 20161108 15:03:46
localtime 20161108 15:33:46
localtime_r 20161108 15:33:46


获取时间戳和计算时间差
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

int main()
{
	struct timeval tv;
	struct timezone tz;

	struct timeval ts;
	int i = 0;

	gettimeofday(&tv, &tz);
	printf("tv_sec %ld\n", tv.tv_sec);
	printf("tv_usec %ld\n", tv.tv_usec);
	printf("tz_minuteswest %d\n", tz.tz_minuteswest);
	printf("tz_dsttime %d\n",tz.tz_dsttime);

	gettimeofday(&tv, &tz);
	printf("%ld.%ld \n", tv.tv_sec, tv.tv_usec / 1000);

	//timestamp
	gettimeofday(&tv, NULL);
	printf("%ld.%ld \n", tv.tv_sec, tv.tv_usec / 1000);
	usleep(30000);
	gettimeofday(&ts, NULL);
	printf("%ld.%ld \n", ts.tv_sec, ts.tv_usec / 1000);
	printf("timestamp: %ld ms \n", (ts.tv_sec * 1000 + ts.tv_usec / 1000) - (tv.tv_sec * 1000 + tv.tv_usec / 1000));

	return 0;
}



$ ./timet2
tv_sec 1478589648
tv_usec 329048
tz_minuteswest -480
tz_dsttime 0
1478589648.329
1478589648.329
1478589648.359
timestamp: 30 ms
### Linux 时间戳的长度与表示方式 在 Linux 系统中,时间戳通常以 `time_t` 类型表示。`time_t` 是一个整数类型,用于存储从 1970 年 1 月 1 日 00:00:00 UTC(称为 Unix 纪元)开始计算的秒数[^1]。根据系统架构的不同,`time_t` 的长度表示范围也会有所差异。 #### 32位系统 在 32 位系统中,`time_t` 通常是一个 32 位有符号整数,能够表示的范围是从 -2,147,483,648 到 2,147,483,647 秒。这意味着它可以表示从 1901 年 12 月 13 日到 2038 年 1 月 19 日的时间段[^1]。这一限制被称为“2038 年问题”,类似于 Y2K 问题。 #### 64位系统 在 64 位系统中,`time_t` 通常是一个 64 位有符号整数,能够表示更广泛的范围。它可以从远古时代(约公元前 292277026596 年)到未来极长时间(约公元 292277026596 年)。这种扩展解决了 32 位系统的“2038 年问题”[^1]。 #### 时间戳的精度 除了秒级时间戳外,Linux 还支持更高精度的时间表示。例如,`struct timeval` `struct timespec` 提供了微秒级纳秒级的时间精度。`struct timeval` 包含两个字段:`tv_sec` 表示秒,`tv_usec` 表示微秒[^2]。而 `struct timespec` 则包含 `tv_sec` `tv_nsec`,分别表示秒纳秒[^3]。 以下是一个使用 `gettimeofday` 函数获取微秒级时间戳的代码示例: ```c #include <stdio.h> #include <sys/time.h> void print_microsecond_timestamp() { struct timeval tv; gettimeofday(&tv, NULL); printf("Microsecond timestamp: %ld.%06ld\n", tv.tv_sec, tv.tv_usec); } int main() { print_microsecond_timestamp(); return 0; } ``` 此外,`TIMESTAMP` 数据类型在数据库或特定应用中可能允许指定秒的小数点后精度(`seconds_precision`),其范围为 0 到 9,默认值为 9,即可以精确到纳秒级别[^3]。 ### 总结 - 在 32 位系统中,`time_t` 通常为 4 字节。 - 在 64 位系统中,`time_t` 通常为 8 字节。 - 常见的时间戳表示为秒级精度,但也可以通过 `struct timeval` 或 `struct timespec` 实现微秒或纳秒级精度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值