解析Linux下的时间函数:设置以及获取时间的方法
2019/7/10 23:26:50
本文主要是介绍解析Linux下的时间函数:设置以及获取时间的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、时间函数
time_t time(time_t *t);
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);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
二、设置和获取时间
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t1;
time_t t2;
struct tm *my_tm;
char buf[128] = {0};
//自Epoch (00:00:00 UTC, January 1,1970)的秒数
t1 = time(&t1);
printf("%d\n", t1); //1355905754
t2 = time(&t2);
sleep(1);
printf("%lf\n", difftime(t2, t1)); //t1,t2相差:1.000000,有时候可以用这个函数来做伪定时器
printf("%s\n",ctime(&t1)); //Wed Dec 19 16:29:14 2012
//init tm
my_tm->tm_year = 2012-1900;
my_tm->tm_mon = 12-1;
my_tm->tm_mday = 12;
my_tm->tm_hour = 12;
my_tm->tm_min = 12;
my_tm->tm_sec = 12;
//设置时间
t1 = mktime(my_tm);
//获取时间
my_tm = localtime(&t1);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
my_tm->tm_year + 1900, my_tm->tm_mon + 1, my_tm->tm_mday, my_tm->tm_hour, my_tm->tm_min, my_tm->tm_sec);
printf("%s\n", buf);//2012-12-12 12:12:12
return 0;
}
复制代码 代码如下:
time_t time(time_t *t);
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);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
二、设置和获取时间
复制代码 代码如下:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t1;
time_t t2;
struct tm *my_tm;
char buf[128] = {0};
//自Epoch (00:00:00 UTC, January 1,1970)的秒数
t1 = time(&t1);
printf("%d\n", t1); //1355905754
t2 = time(&t2);
sleep(1);
printf("%lf\n", difftime(t2, t1)); //t1,t2相差:1.000000,有时候可以用这个函数来做伪定时器
printf("%s\n",ctime(&t1)); //Wed Dec 19 16:29:14 2012
//init tm
my_tm->tm_year = 2012-1900;
my_tm->tm_mon = 12-1;
my_tm->tm_mday = 12;
my_tm->tm_hour = 12;
my_tm->tm_min = 12;
my_tm->tm_sec = 12;
//设置时间
t1 = mktime(my_tm);
//获取时间
my_tm = localtime(&t1);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
my_tm->tm_year + 1900, my_tm->tm_mon + 1, my_tm->tm_mday, my_tm->tm_hour, my_tm->tm_min, my_tm->tm_sec);
printf("%s\n", buf);//2012-12-12 12:12:12
return 0;
}
这篇关于解析Linux下的时间函数:设置以及获取时间的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27Sealos Devbox 基础教程:使用 Cursor 从零开发一个 One API 替代品 审核中
- 2024-12-27TypeScript面试真题解析与实战指南
- 2024-12-27TypeScript大厂面试真题详解与解析
- 2024-12-26怎么使用nsenter命令进入容器?-icode9专业技术文章分享
- 2024-12-26导入文件提示存在乱码,请确定使用的是UTF-8编码怎么解决?-icode9专业技术文章分享
- 2024-12-26csv文件怎么设置编码?-icode9专业技术文章分享
- 2024-12-25TypeScript基础知识详解
- 2024-12-25安卓NDK 是什么?-icode9专业技术文章分享
- 2024-12-25caddy 可以定义日志到 文件吗?-icode9专业技术文章分享
- 2024-12-25wordfence如何设置密码规则?-icode9专业技术文章分享