迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

c语言 获取当前日期和时间的方法

作者:迹忆客 最近更新:2022/12/26 浏览次数:

头文件 time.h

#include <time.h>

要获取时间,就不得不说一个函数 time()

原型: time_t time(time_t * timer)

php语言中就有time() 这个函数,返回一个时间戳——也就是一个整数。然后再通过 date() 函数进行格式化,转换成我们想要的时间格式。 在C中,该函数的功能是获取当前的系统时间,返回的是一个time_t 类型的值。 也是一个时间戳。其实就是一个大整数。这个整数值是怎么算出来的呢,就是从 UTC(Coordinate Universal Time) 时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。

仅仅有这个秒数还是不够的,还需要一种方式将其转化为我们需要的格式。在C中可以调用localtime()函数将time_t表示的UTC时间转换为本地时间(我们是+8区,比UTC多8个小时)并转成 struct tm类型。

time_t的数字是按UTC算的,跟时区无关,同一个时刻全球所有计算机上的time(NULL)返回值都相同。 用localtime()转换成可显示的格式时才需要考虑时区。

time_t 转化为 struct tm 的函数有两个 —— localtime()gmtime()

原型: struct tm * localtime(time_t *timer)struct tm * gmtime(time_t *timer)

这两个函数的区别是 gmtime() 转换出来的是 0 时区的时间。而 localtime() 是将本地的时区计算在内的。 什么意思呢,就是说我们的时区是+8区,比UTC多8个小时。假设当前时间是 15:40:56。 使用 gmtime() 转出来之后就是 7:40:56; 使用 localtime() 转出来之后就是 15:40:56

下面再来介绍struct tm

struct tm {
    int tm_sec;  // 秒
    int tm_min;  // 分
    int tm_hour; // 时
    int tm_mday; // 日
    int tm_mon;  // 月
    int tm_year; // 年
    int tm_wday; // 表示周几  0-6 (0:周日 1:周一 2:周二  3:周三 4:周四 5:周五 6:周六)
    int tm_yday; // 从 一月一日开始算到现在是一年中的第几天 取值 0-365
    int tm_isdst; // 表示是否是夏令时  1是  0否   在mktime() 函数中使用  有时候为-1,则由mktime() 自己去判断是否是夏令时
};

需要注意的是 tm_mon 是从 0 开始的,也就是说月份要加 1; tm_year 是表示从1900到现在过了多少年,也就是今年和1900年的一个差值。所以要使用 tm_year+1900 才表示今年的年份。

下面是完整的例子

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

int main(void)
{
    time_t time_ptr;
    struct tm *tmp_ptr = NULL;
    int year,month,day,hour,minute,sec;
    // 获取当前时间
    time(&time_ptr);
    tmp_ptr = localtime(&time_ptr);

    year = tmp_ptr->tm_year + 1900;
    month = tmp_ptr->tm_mon + 1;
    day = tmp_ptr->tm_mday;

    hour = tmp_ptr->tm_hour;
    minute = tmp_ptr->tm_min;
    sec = tmp_ptr->tm_sec;
    
    printf(date_time,"[%d-%d-%d %d:%d:%d] ",year,month,day,hour,minute,sec);
    return 0;
}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Vue - An In-Depth Guide to Lifecycle Hooks

发布时间:2025/02/21 浏览次数:117 分类:Vue

Vue has many lifecycle hooks, and it can be confusing to understand the meaning or purpose of each one. In this article, we will explain the function of each lifecycle hook and how to use them.

Solution for Flickering During Vue Template Parsing

发布时间:2025/02/21 浏览次数:103 分类:Vue

Solution for Flickering During Vue Template Parsing, Recently, while working on a project, I noticed that when the internet speed is slow, the screen flickers and the expression message appears. This happens because when the internet speed i

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便