vlambda博客
学习文章列表

获取系统当前时间(C语言)

C语言--获取当前时间

使用C语言的函数接口,获取系统的当前时间,组装成一定格式的字符串

获取系统当前时间 demo

 1#include <stdio.h>
2#include <time.h>
3
4int main(int argc, char *argv[]) {
5    char len[20] = {0};
6
7    time_t timep;
8    time(&timep);
9
10    struct tm *p;
11    p = gmtime(&timep);
12
13    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);
14
15    printf("\n%s\n", len);
16    return 0;
17}

运行结果:

1baoshaohua:test bao$ ./test 
2
32020-3-25 9:51:42
4baoshaohua:test bao$ 

获取系统当前时间,时间格式 yyyy-MM-dd HH:mm:ss


在C语言中,获取日期时间的函数 不只是有这一个,还有其他几种

1、time(取得目前的时间)

 1#include <stdio.h>
2#include <time.h>
3
4
5/**  @brief 
6 *   #include <time.h>
7 *   
8 *   time_t time(time_t *tloc);
9 * 
10 *       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.  
11 *       If an error occurs, time() returns the value (time_t)-1.
12 */

13
14int main(int argc, char *argv[]){
15    int sec = time((time_t *)NULL);
16
17    //time_t * tloc;
18    //int sec = time(tloc);
19
20    printf("%d\n", sec);
21
22    return 0;
23}

运行结果:

1baoshaohua:test bao$ gcc -o time time.c 
2baoshaohua:test bao$ ./time 
31585106435

此函数会返回从公元 1970 年 1 月 1 日的 UTC 时间从 0 时 0 分 0 秒 算起到现在所经过的秒数。如果 tloc 并非空指针的话,此函数也会将 返回值存到 tloc 指针所指的内存。

2、ctime(将时间和日期以字符串格式表示)

 1#include <stdio.h>
2#include <string.h>
3#include <time.h>
4
5/**
6 *  @brief 
7 *  #include <time.h>
8 *  char * ctime(const time_t *clock); 
9 * 
10 *  ctime()将参数 clock 所指的 time_t 结构中的信息转换成真实世界 所使用的时间日期表示方法,然后将结果以字符串形态返回。
11 *  此函 数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
12 */

13int main(int argc, char *argv[]) {
14    int sec = 0;
15    char str[30] = {0}; 
16    time_t *clock;
17
18    //获取时间,保存到time_t结构体中。在time的返回值(sec)里面有全部秒数
19    sec = time(clock);
20    strcpy(str, ctime(clock)); //将time_t类型的结构体中的时间,按照一定格式保存成字符串,
21
22    printf("sec = %d\n", sec);
23    printf("str = %s\n", str);
24
25    return 0;
26}

运行结果:

1baoshaohua:test bao$ gcc -o ctime ctime.c 
2baoshaohua:test bao$ ./ctime 
3sec = 1585122623
4str = Wed Mar 25 15:50:23 2020

这种方式获取到的时间,格式是固定的,不能满足需求。


3、gmtime(取得目前时间和日期)

 1#include <time.h>
2#include <stdio.h>
3
4/**
5 * @brief 
6 * 
7 * #include <time.h>
8 * 
9 * struct tm * gmtime(const time_t *clock);
10 * 
11 * gmtime()将参数 clock 所指的 time_t 结构中的信息转换成真实世界 所使用的时间日期表示方法,然后将结果由结构 tm 返回。结构 tm 的定义为:
12    struct tm {
13        int tm_sec; 
14        int tm_min; 
15        int tm_hour; 
16        int tm_mday; 
17        int tm_mon; 
18        int tm_year; 
19        int tm_wday; 
20        int tm_yday; 
21        int tm_isdst;
22    };
23
24int tm_sec 代表目前秒数,正常范围为 0-59,但允许至 61 秒 
25int tm_min 代表目前分数,范围 0-59
26int tm_hour 从午夜算起的时数,范围为 0-23
27int tm_mday 目前月份的日数,范围 01-31
28int tm_mon 代表目前月份,从一月算起,范围从 0-11
29int tm_year 从 1900 年算起至今的年数
30int tm_wday 一星期的日数,从星期一算起,范围为 0-6
31int tm_yday 从今年 1 月 1 日算起至今的天数,范围为 0-365 int tm_isdst 日光节约时间的旗标 此函数返回的时间日期未经时区转换,而是 UTC 时间。
32
33*/

34
35int main(int argc, char *argv[])
36
{
37    time_t timep;
38    time(&timep);
39
40    // printf("%s\n", ctime(&timep));
41
42    struct tm *p;
43    p = gmtime(&timep);
44
45    printf("%d\n", p->tm_sec);         /*获取当前秒*/
46    printf("%d\n", p->tm_min);         /*获取当前分*/
47    printf("%d\n"8 + p->tm_hour);    /*获取当前时,这里获取西方的时间,刚好相差八个小时*/
48    printf("%d\n", p->tm_mday);        /*获取当前月份日数,范围是1-31*/
49    printf("%d\n"1 + p->tm_mon);     /*获取当前月份,范围是0-11,所以要加1*/
50    printf("%d\n"1900 + p->tm_year); /*获取当前年份,从1900开始,所以要加1900*/
51    printf("%d\n", p->tm_yday);        /*从今年1月1日算起至今的天数,范围为0-365*/
52
53    return 0;
54}

运行结果:

1baoshaohua:test bao$ gcc -o gmtime gmtime.c
2baoshaohua:test bao$ ./gmtime 
310
456
519
625
73
82020
984

如果需要组成你需要的时间格式,那么就可以借助 snprintf 函数

    传送门:


作者的github:

https://github.com/Miss1xiaobao/Mygit


CSDN个人主页:

https://blog.csdn.net/weixin_44966900