C语言--获取当前时间(不推荐使用gmtime)

本文介绍了C语言中获取当前时间的方法,包括通过获取秒数、转换为分解时间和ASCII字符串,强调了gmtime可能导致的问题,并提供了线程安全的解决方案,适合需要处理时区时间的开发者。
摘要由CSDN通过智能技术生成

C语言–获取当前时间

前两天写了一个获取时间的文章, 但是为什么又写一篇文章.

1、上一篇 总结的函数少.

2、遇到了问题.
有一天晚上使用gmtime函数,获取时间出现了26点的情况. 白天的时候就正常了.

2020-4-16 26:35:23
实际时间是:
2020-4-17 2:35:23

3、再次测试

baoshaohua:获取系统当前时间 bao$ ./ctime 
sec = Sun Apr 19 01:07:27 2020

str = Sun Apr 19 01:07:27 2020

baoshaohua:获取系统当前时间 bao$ ./gmtime 
2020-04-18 25:07:28
2020-04-18 25:07:28
baoshaohua:获取系统当前时间 bao$ ./localtime 
2020-04-19 01:07:30
2020-04-19 01:07:30

gmtime转出来的是0时区的标准时间, 输出时间的时候“我” +8了,

使用localtime函数获取·分解时间, 当前时区的时间.


获取系统当前时间 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


+++BUG+++

2020-4-16 26:35:23

实际时间是:

2020-4-17 2:35:23

+++BUG+++


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

一、time(get time in seconds)

  • 此函数会返回从公元 1970 年 1 月 1 日的 UTC 时间从 0 时 0 分 0 秒 算起到现在所经过的秒数。

  • 如果 tloc 并非空指针的话,此函数也会将 返回值存到 tloc 指针所指的内存。

  • 参数time_t就是long

  • get time in seconds 以秒为单位获取时间

#include <stdio.h>
#include <time.h>

/**
 * 	#include <time.h>
 *  time_t time(time_t *tloc);   //参数是 time_t 实际是  long
 *  
 *  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. 
 */


int main(int arrgc, char const *argv[]){
   
	time_t tloc = 0;

	// time_t tt = time(NULL);
	time_t tt = time((time_t *)&tloc);

	printf("tt [%ld]\n", tt);
	printf("tloc [%ld]\n", tloc);

	return 0;
}

运行结果:

baoshaohua:test bao$ gcc -o time time.c 
baoshaohua:test bao$ ./time 
tt [1587116499]
tloc [1587116499]

1.1 小插曲

我不是定义为变量 time_t tloc = 0 ,

而是定义为指针 time_t *tloc = NULL;,

定义为变量 time_t tloc = 0 是有空间的.

定义为指针 time_t *tloc = NULL; 这个空间是指针的空间,不是变量的空间.

int main(int arrgc, char const *argv[]){
	time_t *tloc = NULL;

	time_t tt = time((time_t *)tloc);

	printf("tt [%ld]\n", tt);

	printf("tloc [%s]\n", tloc);
	printf("tloc [%ld]\n", *tloc);
}

输出为:

baoshaohua:获取系统当前时间 bao$ ./time 
tt [1587117447]
tloc [(null)]
Segmentation fault: 11

二、将日期和时间转换为分解时间或ASCII

broken-down time Calendar Time
分解时间 日历时间
tm数据类型 time_t数据类型
用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日…
#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);



//tm 结构体
struct tm {
            int tm_sec;    /* Seconds (0-60) */
            int tm_min;    /* Minutes (0-59) */
            int tm_hour;   /* Hours (0-23) */
            int tm_mday;   /* Day of the month (1-31) */
            int tm_mon;    /* Month (0-11) */
            int tm_year;   /* Year - 1900 */
            
            int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
            int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
            int tm_isdst;  /* Daylight saving time */
};


The members of the tm structure are:

       tm_sec    The  number  of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap
                 seconds.

       tm_min    The number of minutes after the hour, in the range 0 to 59.

       tm_hour   The number of hours past midnight, in the range 0 to 23.

       tm_mday 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值