C ++日期和时间 [英] C++ date and time
问题描述
在谈论日期和时间时,使用字符串更容易,或者我应该写一个日期/时间类吗?
我想要编写一个约会时间和日期的约会类。一旦我编写了类文件,我想将它集成到C ++构建器中的表单应用程序。
我看到有一个TMonthCalendar控件。我想在使用表单应用程序时使用此控件。这样,这个控制使用什么格式的日期?我想在制作课程时使用与控件相同的类型,以便我可以轻松地将其集成在一起。
更新
我发现它使用TDateTime类型。我的问题是:我需要使用什么语句来在控制台应用程序中使用它?
C ++ 11包括日期/时间表示的便利数据类型和功能,以及它们转换为字符串。
可以这样做(很简单,我想):
#include< iostream>
#include< iomanip>
#include< ctime>
int main()
{
std :: time_t t = std :: time(NULL);
std :: tm tm = * std :: localtime(& t);
std :: cout<<< 现在的时间是<< std :: put_time(& tm,%c%Z)<< '\\\
';
}
特别是,有数据类型 std :: time_t
和 std :: tm
,一个非常好的IO操纵器 std :: put_time
为漂亮的打印。其使用的格式字符串在 cppreference 中有详细记录。
这也应该与语言环境一起工作,例如对于日语时间/日期格式:
std :: cout.imbue(std :: locale(ja_JP.utf8) );
std :: cout<<< ja_JP:<< std :: put_time(& tm,%c%Z)<< '\\\
';
C ++包含的 chrono
+11标准库还允许您方便地执行简单的时间/日期算术:
std :: chrono :: time_point< std: :chrono :: system_clock>现在;
now = std :: chrono :: system_clock :: now();
/ *今天前一天:* /
std :: time_t now_c = std :: chrono :: system_clock :: to_time_t(
now - std :: chrono :: hours(24) );
不幸的是,并不是所有的编译器都可用。特别是,在GCC 4.7.1中似乎没有可以使用 std :: put_time
函数。为了获得我最初提供的代码,我不得不使用稍微优雅的 std :: strftime
函数:
#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<< 现在的时间是<< buf< std :: endl;
}
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?
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.
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
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 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';
}
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.
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';
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));
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屋!