跳转到内容

C 语言时间日期

来自代码酷


C语言时间日期是C标准库中用于处理时间和日期的功能模块,主要通过头文件 `<time.h>` 提供。这些功能允许程序获取当前时间、计算时间差、格式化时间输出以及进行时间转换等操作。本指南将详细介绍C语言中时间日期的相关概念、函数及其使用方法。

概述[编辑 | 编辑源代码]

在C语言中,时间日期功能基于日历时间(Calendar Time)处理器时间(Processor Time)两种概念:

  • 日历时间:表示从某个固定时间点(通常是1970年1月1日00:00:00 UTC,称为Unix纪元)到当前时间的秒数。
  • 处理器时间:表示程序执行所消耗的CPU时间,通常以时钟滴答(clock ticks)为单位。

C标准库提供了一系列函数来处理这些时间值,并将其转换为人类可读的格式。

主要数据类型[编辑 | 编辑源代码]

C语言中与时间日期相关的主要数据类型包括:

  • time_t:用于存储日历时间,通常是整数类型(如long)。
  • struct tm:用于存储分解的时间(年、月、日、时、分、秒等)。
  • clock_t:用于存储处理器时间。

struct tm 结构体[编辑 | 编辑源代码]

struct tm 包含以下字段:

struct tm {
    int tm_sec;   // 秒 [0, 59]
    int tm_min;   // 分钟 [0, 59]
    int tm_hour;  // 小时 [0, 23]
    int tm_mday;  // 月份中的日期 [1, 31]
    int tm_mon;   // 月份 [0, 11](0 = 一月)
    int tm_year;  // 年份(从1900年开始)
    int tm_wday;  // 星期几 [0, 6](0 = 星期日)
    int tm_yday;  // 一年中的第几天 [0, 365]
    int tm_isdst; // 夏令时标志(正数表示启用,0表示禁用,负数表示未知)
};

常用时间函数[编辑 | 编辑源代码]

以下是C语言中处理时间日期的常用函数:

time()[编辑 | 编辑源代码]

获取当前日历时间(Unix时间戳)。

#include <time.h>
time_t time(time_t *timer);

示例:

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

int main() {
    time_t now;
    time(&now);
    printf("当前时间戳:%ld\n", now);
    return 0;
}

输出:

当前时间戳:1712345678

localtime()gmtime()[编辑 | 编辑源代码]

time_t转换为本地时间或UTC时间(struct tm)。

#include <time.h>
struct tm *localtime(const time_t *timer);  // 本地时间
struct tm *gmtime(const time_t *timer);     // UTC时间

示例:

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

int main() {
    time_t now;
    time(&now);
    struct tm *local = localtime(&now);
    printf("本地时间:%d-%02d-%02d %02d:%02d:%02d\n",
           local->tm_year + 1900, local->tm_mon + 1, local->tm_mday,
           local->tm_hour, local->tm_min, local->tm_sec);
    return 0;
}

输出:

本地时间:2024-04-05 14:30:45

mktime()[编辑 | 编辑源代码]

struct tm转换为time_t,并修正非法日期。

#include <time.h>
time_t mktime(struct tm *timeptr);

示例:

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

int main() {
    struct tm timeinfo = {0};
    timeinfo.tm_year = 124;  // 2024年(2024 - 1900)
    timeinfo.tm_mon = 3;     // 4月(0 = 1月)
    timeinfo.tm_mday = 5;    // 5日
    timeinfo.tm_hour = 14;
    timeinfo.tm_min = 30;
    timeinfo.tm_sec = 45;
    
    time_t custom_time = mktime(&timeinfo);
    printf("自定义时间戳:%ld\n", custom_time);
    return 0;
}

输出:

自定义时间戳:1712345445

strftime()[编辑 | 编辑源代码]

格式化时间为字符串。

#include <time.h>
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);

示例:

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

int main() {
    time_t now;
    time(&now);
    struct tm *local = localtime(&now);
    char buffer[100];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local);
    printf("格式化时间:%s\n", buffer);
    return 0;
}

输出:

格式化时间:2024-04-05 14:30:45

时间格式化符号[编辑 | 编辑源代码]

strftime() 支持的常用格式化符号:

符号 描述 示例
%Y 四位年份 2024
%m 两位月份(01-12) 04
%d 两位日期(01-31) 05
%H 两位小时(00-23) 14
%M 两位分钟(00-59) 30
%S 两位秒(00-59) 45
%A 星期全名 Friday
%B 月份全名 April

实际应用案例[编辑 | 编辑源代码]

计算程序运行时间[编辑 | 编辑源代码]

使用clock()函数测量代码执行时间:

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

int main() {
    clock_t start = clock();
    
    // 模拟耗时操作
    for (int i = 0; i < 100000000; i++);
    
    clock_t end = clock();
    double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
    printf("程序运行时间:%.3f秒\n", elapsed);
    return 0;
}

输出:

程序运行时间:0.312秒

倒计时程序[编辑 | 编辑源代码]

实现一个简单的倒计时功能:

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

void countdown(int seconds) {
    time_t start = time(NULL);
    time_t end = start + seconds;
    
    while (time(NULL) < end) {
        int remaining = (int)(end - time(NULL));
        printf("\r倒计时:%02d:%02d", remaining / 60, remaining % 60);
        fflush(stdout);
        sleep(1);
    }
    printf("\n时间到!\n");
}

int main() {
    countdown(10); // 10秒倒计时
    return 0;
}

输出:

倒计时:00:10
(每秒更新,直到00:00)
时间到!

时间处理流程图[编辑 | 编辑源代码]

以下是一个简单的时间处理流程:

graph TD A[获取当前时间 time()] --> B[转换为本地时间 localtime()] B --> C[格式化时间 strftime()] C --> D[输出或使用时间]

总结[编辑 | 编辑源代码]

C语言的时间日期功能提供了强大的工具来处理时间相关的任务。通过time.h中的函数,开发者可以轻松获取当前时间、转换时间格式、计算时间差以及实现定时功能。掌握这些知识对于开发日志系统、计时器、调度任务等应用至关重要。