亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 日期和时间

C++ 标准库没有提供正确的日期类型。 C++ 从 C 继承了用于日期和时间操作的结构和函数。要访问与日期和时间相关的函数和结构,您需要在 C++ 程序中包含 头文件。

与时间相关的类型有四种:clock_t、time_t、size_t , 和 tm .类型 -clock_t、size_t 和 time_t 能够将系统时间和日期表示为某种整数。

结构类型 tm 以具有以下元素的 C 结构形式保存日期和时间 -

struct tm {
   int tm_sec;   // seconds of minutes from 0 to 61
   int tm_min;   // minutes of hour from 0 to 59
   int tm_hour;  // hours of day from 0 to 24
   int tm_mday;  // day of month from 1 to 31
   int tm_mon;   // month of year from 0 to 11
   int tm_year;  // year since 1900
   int tm_wday;  // days since sunday
   int tm_yday;  // days since January 1st
   int tm_isdst; // hours of daylight savings time
}

以下是我们在 C 或 C++ 中处理日期和时间时使用的重要函数。所有这些函数都是标准 C 和 C++ 库的一部分,您可以参考下面给出的 C++ 标准库来查看它们的详细信息。

Sr.No 功能和用途
1

time_t 时间(time_t *time);

这将返回系统的当前日历时间(以 1970 年 1 月 1 日以来经过的秒数为单位)。如果系统没有时间,则返回 0.1。

2

char *ctime(const time_t *time);

这将返回一个指向 day month year hours:minutes:seconds year\n\0 形式的字符串的指针 .

3

struct tm *localtime(const time_t *time);

这将返回一个指向 tm 的指针 代表当地时间的结构。

4

clock_t 时钟(void);

这将返回一个近似于调用程序运行时间的值。如果时间不可用,则返回值 .1。

5

char * asctime (const struct tm * time);

这将返回一个指向字符串的指针,该字符串包含存储在时间指向的结构中的信息,转换为以下形式:日月日时:分:秒年\n\0

6

struct tm *gmtime(const time_t *time);

这以 tm 结构的形式返回指向时间的指针。时间以协调世界时 (UTC) 表示,本质上是格林威治标准时间 (GMT)。

7

time_t mktime(struct tm *time);

这将返回与 time 所指向的结构中的时间等效的日历时间。

8

双difftime (time_t time2, time_t time1);

该函数计算time1和time2之间的秒差。

9

size_t strftime();

此函数可用于将日期和时间格式化为特定格式。

当前日期和时间

假设您要检索当前系统日期和时间,可以是本地时间,也可以是协调世界时 (UTC)。以下是实现相同的示例 -

现场演示
#include <iostream>
#include <ctime>

using namespace std;

int main() {
   // current date/time based on current system
   time_t now = time(0);
   
   // convert now to string form
   char* dt = ctime(&now);

   cout << "The local date and time is: " << dt << endl;

   // convert now to tm struct for UTC
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "The UTC date and time is:"<< dt << endl;
}

当上面的代码编译并执行时,它会产生以下结果 -

The local date and time is: Sat Jan  8 20:07:41 2011

The UTC date and time is:Sun Jan  9 03:07:41 2011

使用 struct tm 格式化时间

tm 在 C 或 C++ 中处理日期和时间时,结构非常重要。如上所述,该结构以 C 结构的形式保存日期和时间。大多数时间相关的功能都使用 tm 结构。以下是一个使用各种日期和时间相关函数和 tm 结构的示例 -

在本章中使用结构时,我假设您对 C 结构以及如何使用箭头 -> 运算符访问结构成员有基本的了解。

现场演示
#include <iostream>
#include <ctime>

using namespace std;

int main() {
   // current date/time based on current system
   time_t now = time(0);

   cout << "Number of sec since January 1,1970 is:: " << now << endl;

   tm *ltm = localtime(&now);

   // print various components of tm structure.
   cout << "Year:" << 1900 + ltm->tm_year<<endl;
   cout << "Month: "<< 1 + ltm->tm_mon<< endl;
   cout << "Day: "<< ltm->tm_mday << endl;
   cout << "Time: "<< 5+ltm->tm_hour << ":";
   cout << 30+ltm->tm_min << ":";
   cout << ltm->tm_sec << endl;
}

当上面的代码编译并执行时,它会产生以下结果 -

Number of sec since January 1,1970 is:: 1588485717
Year:2020
Month: 5
Day: 3
Time: 11:31:57

C语言

  1. 电压和电流计算
  2. C++ 变量、文字和常量
  3. C++ 类和对象
  4. C++ 内存管理:新建和删除
  5. C++ 公共、受保护和私有继承
  6. C++朋友函数和朋友类
  7. 如何在 Python 中获取当前日期和时间?
  8. C++ 中的结构和类
  9. C++ 重载(运算符和函数)
  10. C++ 文件和流
  11. Java 8 - 新的日期/时间 API
  12. Python - 日期和时间