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

Is it easier to just use strings when talking about Date and Time?
No. Even simple things like calculating a duration if you have a meeting start and end date need to account for complex things such as leap years.
should I write or get a Date/Time class?
No. Writing a Date/Time Library seems simple, but it's rather difficult to get right and extremely easy to get wrong. Also, others have done it before - boost is a collection of free libraries with stellar reputation. So many others in fact, that it's become a cliche that newbie programmers want to write a Date/Time library, failing horribly at it.
I see that there is a TMonthCalendar control. [...] I have found that it uses the TDateTime type.
Relying on the same Date/Time class as your GUI framework is ok, but if you later change the GUI framework it can become an issue. Since it's not terribly hard to swap a sensible Date/Time library for another sensible Date/Time library later on, just use one that you find easy to use.

Related

How to get all the dates between two dates in c++ standard library?

how can i get all dates between two dates in c++ .The input format is like that 2022-01-14"
i need to get for example dates between "2022-05-13" and "2021-12-12". i need all dates between this two in easier way cause it's really hard if I will compare the months,days and years . and I want to store them in a vector<string> to deal with them. i try alot with the library "ctime.h" but i didn't know how to work with it.
Unfortunately your post was edited in such a way as to hide the fact that you are not interested in a C++20 solution. The reason that this is relevant is that C++20 has a very elegant and easy to use solution. And, for pre-C++20, there exists a free, open-source, header-only preview of this part of C++20 which will work with C++11/14/17.
Here is what the solution looks like with this preview:
#include "date/date.h"
#include <iostream>
#include <vector>
int
main()
{
using namespace date;
using namespace std;
vector<sys_days> v;
for (sys_days d = 2021_y/12/12; d <= sys_days{2022_y/05/13}; d += days{1})
v.push_back(d);
for (auto d : v)
cout << d << '\n';
}
Output:
2021-12-12
2021-12-13
2021-12-14
...
2022-05-11
2022-05-12
2022-05-13
sys_days is a std::chrono::time_point based on system_clock but with a precision of days. Under the hood this is nothing more than a count of days since 1970-01-01. And there exists easy conversions between this date data structure and a {year, month, day} data structure. And there are nice printing facilities.

Converting ISO8601 string to milliseconds since epoch

Sample string:
2018-10-31T14:45:21.778952-07:00
I would like to convert it to int64_t representing milliseconds (or even microseconds) since epoch. The timezone can vary. The code will be executing on a linux box, but I only have access to std and folly (cannot use any arbitrary 3P libraries).
I searched for this and found a few different ways that do not work for me:
strptime() and std::get_time() lose the millisecond precision
More importantly, neither of those can deal with timezone offsets
Some other solutions depend on 3P libraries
Is there some easy way to do this?
From the comments above:
I am looking into using Howard's library. However, that it makes a web call gives me pause. I assume that if the call fails it will just use the locally stored timezone name data? We won't be dealing with timezone names, so I don't care about those. However, making a network call might be an issue.
Howard's library is layered:
A foundational layer that does not need the IANA time zone database and thus never makes networking calls. This is a single header, header-only library.
A time zone layer that is aware of the IANA time zone database. This layer can be configured to make network calls or not (depending on build flags), or even use your OS's time zone database (except on Windows).
Your application does not require the time zone layer as it only deals with UTC offsets, and not time zone names or rules.
Here is a function that converts a std::string with your sample format into a std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>. That type is a big verbose mouthful for a system_clock::time_point except guaranteed to have microseconds precision. The foundational layer has a type alias for this type called date::sys_time<std::chrono::microseconds>.
#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>
auto
to_sys_time(std::string s)
{
using namespace date;
using namespace std;
using namespace std::chrono;
istringstream in{move(s)};
in.exceptions(ios::failbit);
sys_time<microseconds> tp;
in >> parse("%FT%T%z", tp);
return tp;
}
Put the string into a istringstream.
Optionally set the istringstream to throw an exception if it fails to parse something (you may choose to handle errors differently).
Declare your time_point with the desired precision.
Parse it with your desired format.
Return it.
You can exercise this function like so:
int
main()
{
auto tp = to_sys_time("2018-10-31T14:45:21.778952-07:00");
using date::operator<<;
std::cout << tp << " UTC\n";
std::cout << tp.time_since_epoch() << '\n';
}
Call to_sys_time with the desired input string.
Make the streaming operators in namespace date available.
Print out the time_point (this is a UTC time_point).
Extract and print out the duration of the time_point.
The output of this program is:
2018-10-31 21:45:21.778952 UTC
1541022321778952µs
This program will port to C++20 by removing #include "date/date.h", using namespace date; and using date::operator<<;.

Using std::chrono / date::gps_clock for converting a double gps timestamp to utc/tai

I get a timestamp from a GPS device in a gps_data struct as a double.
I'd like to convert this GPS timestamp to UTC and TAI times, something simple as:
void handle_gps_timestamp(double timestamp)
{
double utc = utc_from_gps(timestamp);
double tai = tai_from_gps(timestamp);
do_stuff(gps, utc, tai);
}
Luckily I found Howard Hinnant's date and timezone library (proposed for C++20) that seems to provide this exact functionality. Unfortunately, at least from what I can see, the date/tz/chrono library has no convenient methods that allow this simple usage.
I must first somehow "transfer" my double into a known chrono/date type. But OK, since I understand the overall concept, namely that the timepoint is defined as a duration after (or before) the epoch of a clock, and I think that this is a beautiful model.
Assumption
I should be able to very easily translate that model to fit my problem, right?
In my case, I have a timestamp that is a point in time, specified as the duration since the gps epoch. Now, there should be a class type of a clock that abstracts and handles all of this for me, I'd like to think. And yes! There is a date::gps_clock and a date::gps_time, which surely should do the work.
Problem
I cannot make it work for me. I'm sure the solution is trivial.
Question
Can someone give me a helping hand, showing how I should use Howard's date library applied to my problem?
It is difficult to answer this question precisely because the input to the problem is underspecified:
I get a timestamp from a GPS device in a gps_data struct as a double ... specified as the duration since the gps epoch.
Therefore I'm going to make some assumptions. I'll state all of my assumptions, and hopefully it will be clear how to alter my answer for other guesses/facts about what that double represents.
Let's say that the double is a non-integral count of milliseconds since the gps epoch. Let's furthermore assume that I want to capture the precision of this input down to microseconds.
#include "date/tz.h"
#include <cstdint>
#include <iostream>
int
main()
{
double gps_input = 1e+12 + 1e-3;
using namespace date;
using namespace std::chrono;
using dms = duration<double, std::milli>;
gps_time<microseconds> gt{round<microseconds>(dms{gps_input})};
auto utc = clock_cast<utc_clock>(gt);
auto tai = clock_cast<tai_clock>(gt);
std::cout << gt << " GPS\n";
std::cout << utc << " UTC\n";
std::cout << tai << " TAI\n";
}
I've arbitrarily created an example input and stored it in gps_input.
Some using directives make the code a lot less verbose.
A custom chrono::duration type that exactly matches the documented specification for what the double represents makes things much simpler, and lessens the chance for errors. In this case I've made a chrono::duration that stores milliseconds in a double and named that type dms.
Now you simply convert the double to dms, and then using round, convert the dms to microseconds, and store those microseconds in a gps time point with precision microseconds or finer. One could use duration_cast in place of round, but when converting from floating point to integral, I usually prefer round, which means round-to-nearest-and-to-even-on-tie.
Now that you have a gps_time, one can use the clock_cast function to convert to other times such as utc_time and tai_time.
This program outputs:
2011-09-14 01:46:40.000001 GPS
2011-09-14 01:46:25.000001 UTC
2011-09-14 01:46:59.000001 TAI
Adjust the milliseconds and microseconds units above as needed. For example if the input represents seconds, the easiest thing to do is to default the second template argument on dms:
using dms = duration<double>;
This library works with C++11/14/17. And with minor modifications it is now part of the official C++20 specification.
The other answer isn't bad but it does require you to have c++17, curl, run cmake, and acquire some custom libraries.
Something that is much easier to drop in as a .h and .cpp would be http://www.leapsecond.com/tools/gpsdate.c.
That doesn't handle the TAI conversion but that might also be on that list.

Representing dates before epoch

We have a bespoke datetime C++ class which represents time in number of seconds passed since epoch. This is stored as int64. This class provides number of helper functions to read and write various types of datetime formats.
Unfortunately it cant handle dates before epoch because its methods rely on gmtime() and mktime() for many operations, which on our windows system does not support dates before epoch. Does anyone knows of replacement of gmtime and mktime which support negative values on windows.
An example of this limitation is our applicatio's inability to store birthdays before 1970, that is because every date has to use this class.
I amy not be clear on what I am asking, this is because of my limited knowledge of datetime implementation/use and my reluctance to understand that huge legacy class, so if you feel this question can be framed in another way or I might look for something different feel free to suggest.
You could use Boost.DateTime, or use the Win32 APIs directly rather than the CRT.
It's likely that you have a lot of testing ahead of you to ensure that handling of data does not change in your rework. Make sure you have exhaustive unit tests in place for the library as it stands before you begin any refactoring.
If you have to consider your values being valid across multiple different locations in the world, use UTC time as your canonical form and translate to/from local time as needed for sensible input/display.
Maybe you've solved this problem already since it was years ago, but you could also use ICU. Examples at: http://userguide.icu-project.org/datetime/calendar/examples
Coming soon to a std::lib implementation near you:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
std::cout << "Valid range is ["
<< sys_days{year::min()/January/1} + 0us << ", "
<< sys_days{year::max()/December/31} + 23h + 59min + 59s + 999'999us
<< "]\n";
}
Output:
Valid range is [-32767-01-01 00:00:00.000000, 32767-12-31 23:59:59.999999]
Preview available here.

Is there a standard date/time class in C++?

Does C++ stl have a standard time class? Or do I have to convert to c-string before writing to a stream. Example, I want to output the current date/time to a string stream:
time_t tm();
ostringstream sout;
sout << tm << ends;
In this case I get the current date/time written out as a number without any formatting. I can use
c- runtime function strftime to format tm first, but that seems like it should not be necessary if the stl has a time class that can be instantiated from time_t value
Not part of STL but well known library is boost.
I would go the way of using boost::date. Here are some examples: http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial.
If you did not try out boost yet I encourage you to do so as it saves you from a lot of nasty issues, as it masks most OS dependent things like threading for example. Many things in boost are header only (template libraries). However datetime requires a lib or dll.
EDIT
The standard "datetime" class is std::chrono::time_point since C++11. The code in the question should be roughly equivalent to
const auto now = std::chrono::system_clock::now();
const auto t_c = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&t_c), "%F %T.\n");
OLD ANSWER
Nitpicking: The STL being the Standard Template Library deals with generic container and algorithms etc. and is unlikely to incorporate classes for date handling and calculation even in the future…
The C++ Standard Library itself includes the STL and a previous version of the C standard library. The latter offers some date and time related functions via #include <ctime> which has already been mentioned above.
If wrapping (or simply using) these functions is sufficient (and quicker) than pulling in boost, go with these. There is nothing wrong with them.
C++ now has the chrono libraries for date and time.
This is documented on http://en.cppreference.com/w/cpp/chrono and http://www.cplusplus.com/reference/chrono/
There are get_time and put_time in <iomanip> header (i guess these came with C++11) which effectively does string formatting or parsing jobs.
There is also a ctime(&time_t) method which outputs string (char*).
OK. Here is closest I have found about directly writing time to a stream:
time_t t(time(NULL)); // current time
tm tm(*localtime(&t));
std::locale loc(""); // current user locale
ostringstream sout;
const std::time_put<TCHAR> &tput =
std::use_facet<std::time_put<TCHAR> >(loc);
tput.put(sout.rdbuf(), sout, _T('\0'), &tm, _T('x'));
sout << ends;
CString sTest(sout.str().c_str());
A very helpful guide is the Apache C++ Standard Library Reference Guide
http://stdcxx.apache.org/doc/stdlibref/time-put.html#sec13
Well, it's been a dozen years since this question was asked. And now (in C++20) it finally has a better answer.
Yes, there are several standard date/time classes in C++20 (not just one). Each serves different purposes in a strongly typed system. For example std::chrono::zoned_time represents a pairing of a std::chrono::time_zone and a std::chrono::time_point<system_clock, SomeDuration>, and represents the local time in some geographic area. Here is how you might create and print the local time in your current time zone to the finest sub-second precision your OS allows.
cout << zoned_time{current_zone(), system_clock::now()} << '\n';
If you need the local time somewhere else, that is just as easily obtained:
cout << zoned_time{"Africa/Casablanca", system_clock::now()} << '\n';
Unlike in previous C++ standards, time_points based on system_clock are now guaranteed to represent UTC, neglecting leap seconds (aka Unix Time). So to get the current time in UTC it is simply:
cout << system_clock::now() << '\n';
Though if you really wanted to use a zoned_time instead (for example the code may be generic), this also works:
cout << zoned_time{"UTC", system_clock::now()} << '\n';
See https://en.cppreference.com/w/cpp/chrono for many more standard date/time classes. All of them are thread-safe. And you are no longer limited to seconds precision.