C语言–获取当前时间
使用C语言的函数接口,获取系统的当前时间,组装成一定格式的字符串
获取系统当前时间 demo
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
char len[20] = {0};
time_t timep;
time(&timep);
struct tm *p;
p = gmtime(&timep);
snprintf(len, 20, "%d-%d-%d %d:%d:%d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, 8 + p->tm_hour, p->tm_min, p->tm_sec);
printf("\n%s\n", len);
return 0;
}
运行结果:
baoshaohua:test bao$ ./test
2020-3-25 9:51:42
baoshaohua:test bao$
获取系统当前时间,时间格式 yyyy-MM-dd HH:mm:ss
在C语言中,获取日期时间的函数 不只是有这一个,还有其他几种
1、time(取得目前的时间)
#include <stdio.h>
#include <time.h>
/** @brief
* #include <time.h>
*
* time_t time(time_t *tloc);
*
* The time() function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without including leap seconds.
* If an error occurs, time() returns the value (time_t)-1.
*/
int main(int argc, char *argv[]){
int sec = time((time_t *)NULL);
//time_t * tloc;
//int sec = time(tloc);
printf("%d\n", sec);
return 0;
}
运行结果:
baoshaohua:test bao$ gcc -o time time.c
baoshaohua:test bao$ ./time
1585106435
此函数会返回从公元 1970 年 1 月 1 日的 UTC 时间从 0 时 0 分 0 秒 算起到现在所经过的秒数。如果 tloc 并非空指针的话,此函数也会将 返回值存到 tloc 指针所指的内存。
2、ctime(将时间和日期以字符串格式表示)
#include <stdio.h>
#include <string.h>
#include <time.h>
/**
* @brief
* #include <time.h>
* char * ctime(const time_t *clock);
*
* ctime()将参数 clock 所指的 time_t 结构中的信息转换成真实世界 所使用的时间日期表示方法,然后将结果以字符串形态返回。
* 此函 数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
*/
int main(int argc, char *argv[]) {
int sec = 0;
char str[30] = {0};
time_t *clock;
//获取时间,保存到time_t结构体中。在time的返回值(sec)里面有全部秒数
sec = time(clock);
strcpy(str, ctime(clock)); //将time_t类型的结构体中的时间,按照一定格式保存成字符串,
printf("sec = %d\n", sec);
printf("str = %s\n", str);
return 0;
}
运行结果:
baoshaohua:test bao$ gcc -o ctime ctime.c
baoshaohua:test bao$ ./ctime
sec = 1585122623
str = Wed Mar 25 15:50:23 2020
这种方式获取到的时间,格式是固定的,不能满足需求。
3、gmtime(取得目前时间和日期)
#include <time.h>
#include <stdio.h>
/**
* @brief
*
* #include <time.h>
*
* struct tm * gmtime(const time_t *clock);
*
* gmtime()将参数 clock 所指的 time_t 结构中的信息转换成真实世界 所使用的时间日期表示方法,然后将结果由结构 tm 返回。结构 tm 的定义为:
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为 0-59,但允许至 61 秒
int tm_min 代表目前分数,范围 0-59
int tm_hour 从午夜算起的时数,范围为 0-23
int tm_mday 目前月份的日数,范围 01-31
int tm_mon 代表目前月份,从一月算起,范围从 0-11
int tm_year 从 1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为 0-6
int tm_yday 从今年 1 月 1 日算起至今的天数,范围为 0-365 int tm_isdst 日光节约时间的旗标 此函数返回的时间日期未经时区转换,而是 UTC 时间。
*/
int main(int argc, char *argv[])
{
time_t timep;
time(&timep);
// printf("%s\n", ctime(&timep));
struct tm *p;
p = gmtime(&timep);
printf("%d\n", p->tm_sec); /*获取当前秒*/
printf("%d\n", p->tm_min); /*获取当前分*/
printf("%d\n", 8 + p->tm_hour); /*获取当前时,这里获取西方的时间,刚好相差八个小时*/
printf("%d\n", p->tm_mday); /*获取当前月份日数,范围是1-31*/
printf("%d\n", 1 + p->tm_mon); /*获取当前月份,范围是0-11,所以要加1*/
printf("%d\n", 1900 + p->tm_year); /*获取当前年份,从1900开始,所以要加1900*/
printf("%d\n", p->tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/
return 0;
}
运行结果:
baoshaohua:test bao$ gcc -o gmtime gmtime.c
baoshaohua:test bao$ ./gmtime
10
56
19
25
3
2020
84
如果需要组成你需要的时间格式,那么就可以借助 snprintf 函数