C ++日期和时间 [英] C++ date and time
问题描述
我在C ++中开发一个预约应用程序,并希望使用一些日期和时间功能。
I am developing an Appointment application in C++ and want to use some Date and Time features.
在谈论日期和时间时,更容易使用字符串,或者我应该写或获得一个日期/时间类?
Is it easier to just use strings when talking about Date and Time, or should I write or get a Date/Time class?
我想编写一个约会类,同时保存约会的时间和日期。一旦我编写了类文件,我想要将它集成到C ++生成器中的表单应用程序。
I am wanting to code an appointment class that holds both the time and date of an appointment. Once I have coded the class file, I am wanting to integrate it into a forms application in C++ builder.
我看到有一个TMonthCalendar控件。我想在表单应用程序时使用此控件。因此,此控件使用的日期的格式是什么?我想在使用类时使用与控件类型相同的类型,以便我可以轻松地将它集成在一起。
I see that there is a TMonthCalendar control. I would like to use this control when making the forms application. As such, what format for the date does this control use? I would like to use the same type as the control when making the class so that I can easily integrate it together.
UPDATE
我发现它使用TDateTime类型。我的问题是这个:我需要使用什么include语句在控制台应用程序中使用它?
I have found that it uses the TDateTime type. My question is this: What include statement do I need to use to use this in a console application?
推荐答案
C ++ 11包括方便的数据类型和日期/时间表示的函数,以及它们转换为字符串。
C++11 includes convenience data types and functions for date/time representations, as well as their conversion to strings.
这样,你可以做这样的事情我想):
With that, you can do things like this (pretty self-explanatory, I think):
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
std::cout << "Time right now is " << std::put_time(&tm, "%c %Z") << '\n';
}
特别是有数据类型 std :: time_t
和 std :: tm
和一个非常好的IO操纵器 std :: put_time
为漂亮的打印。其使用的格式字符串已在 cppreference 中详细记录。
In particular, there are data types std::time_t
and std::tm
, and a very nice IO manipulator std::put_time
for pretty printing. The format strings used by it are well-documented at cppreference.
这也应该与locales一起工作,例如对于日语时间/日期格式:
This is also supposed to work together well with locales, e.g. for a Japanese time/date format:
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
chrono
+11标准库还允许你方便地进行简单的时间/日期算术:
The chrono
library included in the C++11 standard library also allows you to do simple time/date arithmetic conveniently:
std::chrono::time_point<std::chrono::system_clock> now;
now = std::chrono::system_clock::now();
/* The day before today: */
std::time_t now_c = std::chrono::system_clock::to_time_t(
now - std::chrono::hours(24));
不幸的是,并非所有编译器都可用。特别的, std :: put_time
函数似乎在GCC 4.7.1中不可用。要获得我最初工作的代码,我不得不使用稍微不太优雅的 std :: strftime
函数:
Unfortunately, not all of this is available in all compilers yet. In particular, the std::put_time
function does not seem to be available in GCC 4.7.1 yet. To get the code I gave initially to work, I had to use the slightly less elegant std::strftime
function:
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
constexpr int bufsize = 100;
char buf[bufsize];
if (std::strftime(buf,bufsize,"%c %Z",&tm) != 0)
std::cout << "Time right now is " << buf << std::endl;
}
这篇关于C ++日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!