Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - C++ date and time

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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") << '
';
}

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") << '
';

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...