C++ C-style 日期和时间

一 简介

1. 类型

(1) struct tm 

日历时间类型;

被解开( broken down )的日历时间类型。

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};

(2)time_t 

从纪元( epoch 或叫做起始点)起的时间类型;

数值类型的 typedef, 表现为 timepoint。

(3)clock_t 

进程运行时间;

数值类型的 typedef, 表现为elapsed CPU time;

clock()返回。

(4)timespec(C++17)

本次不介绍

2. 时间操作(Time manipulation)

(1)difftime

计算时间( time_t )之间的差 。

double difftime( std::time_t time_end, std::time_t time_beg );

(2)time

返回自纪元起计的系统当前时间 。

std::time_t time( std::time_t* arg );

(3)clock

返回自程序启动时起的原始处理器时钟时间 ;

单位是 1/CLOCKS_PER_SEC 秒。

std::clock_t clock();

(4)timespec_get(C++17)

本次不介绍

3. 格式转换

(1)asctime

转换 tm 对象为文本表示;

转换为一个标准日历时间字符串。格式为 “Www Mmm dd hh:mm:ss yyyy\n”

char* asctime( const std::tm* time_ptr );

(2)ctime

转换 time_t  对象为文本表示;

转换为一个标准日历时间字符串,考虑时区。

如同通过调用 std::asctime(std::localtime(time)) 。

char* ctime( const std::time_t* time );

(3)strftime

转换 tm 对象到自定义的文本表示。

注意各种转换指定符的含义。

std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* time );

(4)wcsftime

转换  tm 对象为定制的宽字符串文本表示。

(5)gmtime

转换纪元起时间为以 UTC 表示的日历时间;

time_t 转换为 tm, 不考虑时区。

std::tm* gmtime( const std::time_t* time );

(6)localtime

转换纪元起时间为以本地时间表示的日历时间。

time_t 转换为 tm,考虑时区。

std::tm* localtime( const std::time_t *time );

(7)mktime

转换日历时间为纪元起的时间;

tm 转换为一个 time_t。

std::time_t mktime( std::tm* time );

3. 常量

(1)CLOCKS_PER_SEC 

每秒的处理器始时钟嘀嗒数 (宏常量)。定义了clock()的单位类型。

二 例子

#include <ctime>
#include <thread> // this_thread
#include <chrono> // chrono
#include <iomanip> // put_time
#include <iostream>

int main() 
{
  {
    // std::time
    auto time = std::time(0);
    std::cout << "time: "<< time << std::endl;
  }
  {
    // std::difftime
    auto start = std::time(0);
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    auto end = std::time(0);
    std::cout << "std:difftime: " << std::difftime(end, start) << std::endl;
  }
  {
    // clock
    auto clock = std::clock();
    std::cout << "clock: " << clock << std::endl;
  }
  {
    // asctime
    auto time = std::time(0);
    std::cout << "std::asctime: " << std::asctime(std::localtime(&time)) << std::endl;
  }
  {
    // ctime
    auto time = std::time(0);
    std::cout << "std::ctime: " << std::ctime(&time) << std::endl;
  }
  {
    // strftime
    auto time = std::time(0);
    char str[200];
    std::strftime(str, sizeof(str), "%A %c", std::localtime(&time));
    std::cout << "std::strftime: " << str << std::endl;
  }
  {
    // gmtime
    // localtime
    auto time = std::time(0);
    std::cout << "std:gmtime: " << std::put_time(std::gmtime(&time), "%c %A") << std::endl;
    std::cout << "std:localtime: " << std::put_time(std::localtime(&time), "%c %A") << std::endl;
  }
  {
    // mktime
     auto time = std::time(0);
     auto tm = *std::localtime(&time);
     tm.tm_mon += 2;
     std::mktime(&tm);
     auto time1 = std::mktime(&tm);
     std::cout << "std:mktime: " << std::put_time(std::localtime(&time1), "%c %A") << std::endl;
  }
  {
    // CLOCKS_PER_SEC
    std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << std::endl;
  }

  std::cin.get();
  return 0;
}

过程中的 this_thread, 请参考 C++11 std::this_thread

猜猜结果中的两个空行怎么来的。

三 参考

C-style 日期和时间

### CC++编程语言中的通用标准头文件 对于CC++这两种紧密关联的语言来说,确实存在一些可以被两者使用的标准库函数接口定义。然而需要注意的是,在某些情况下这些头文件可能具有不同的名称或行为。 #### C语言的标准头文件 在C语言中,常见的标准头文件有`<stdio.h>`用于输入输出操作;`<stdlib.h>`提供内存分配、进程控制等功能;还有像`<string.h>`, `<math.h>`等分别处理字符串以及数学计算等方面的功能[^2]。 #### C++语言的标准头文件 当涉及到C++时,则会发现许多对应的C语言头文件去掉了`.h`后缀,并且为了更好地支持面向对象特性而进行了扩展。例如,`<cstdio>`替代了`<stdio.h>`,它不仅包含了原来所有的功能声明而且还增加了额外的支持以适应新的语言特性命名空间机制(如std::printf)。同样地,也有`<cstdlib>`,`<cstring>`等等[^1]。 #### 可共用的头文件实例 以下是几个可以在两种语言环境中都适用的例子: - **输入/输出流** - C: `#include <stdio.h>` - C++: `#include <cstdio>` 或者更推荐使用 iostream 流类:`#include <iostream>` - **字符处理** - C: `#include <ctype.h>` - C++: `#include <cctype>` - **时间日期管理** - C: `#include <time.h>` - C++: `#include <ctime>` 值得注意的一点是在现代C++实践中,通常建议优先采用专门为该语言设计的新版本头文件而不是旧式的带有 `.h` 的形式,因为后者主要是为了向后兼容早期代码而保留下来的。 ```cpp // 使用C风格头文件 (不推荐) #include <stdio.h> #include <stdlib.h> int main() { printf("Hello from C-style headers\n"); } // 推荐做法——使用C++风格头文件 #include <iostream> #include <cstdlib> int main() { std::cout << "Hello using C++ style headers" << std::endl; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值