Custom Format for Current Date and Time in C++ - c++

I am trying to save a file into a directory of files based on the current date and time. I am trying to get the format of the following:
"FullMonth-FullYear" example:
"April-2011"
"FullMonth-littleDay-year" example:
"March-7-11"
hour-minutes-seconds. example: "18:05:09" in 24 hour format

It depends on the format you have the time in right now. I'm a big fan of sprintf(), and since I mostly deal with big piles of seconds, milliseconds, or nanoseconds, I do a lot of modulus arithmetic to get what I want.

boost.date_time can do arbitrary formatting, to a higher a degree of precision than the standard functions are typically capable of. Specifically, see Date Time Input/Output Format Flags.

Related

Parsing a date and time from a string in the current system format on Windows

I need to parse a string that is supposed to consist of a date and time in the system's current date/time format settings. I can use, for example, std::get_time(), but the problem is that I don't know what date/time format is set in the system.
I have found several ways to get the date/time format using:
GetLocaleInfo/GetLocaleInfoEx with LOCALE_SSHORTDATE and LOCALE_SSHORTDATE
or std::time_get<char>::dateorder()
but in this way, I get "incompatible format" with functions that parse date/time (std::get_time()) and it needs to be converted:
GetLocaleInfo() gives me "MM/dd/yyyy H:mm:ss", and this needs to be converted to something like "%m/%d/%Y %H:%M:%S".
std::time_get<char>::dateorder() gives a more accurate order in date format:
std::time_get<char>::dmy
std::time_get<char>::mdy
std::time_get<char>::ymd
std::time_get<char>::ydm
but it is still necessary to define the separator and the time format.
In general, I don't mind manual conversion, but I'm wondering if there is a more direct way that gives the system date/time format that is compatible with the date/time parsing functions.
std::get_time uses the std::time_get facet.
std::time_get uses the POSIX strptime interface for its flags.
The POSIX strptime specification states that:
%x The date, using the locale's date format.
%X The time, using the locale's time format.
Whether or not this will actually work in your environment is another matter. However, it is worth giving this a try.

How to deal with time in C++?

Basically I need to write a program that will ask the user to enter arrival and departure time in the 24 hour format and it needs to calculate the difference between them.
I know how to do everything else, but i'm not sure how to make it so that it lets you enter the time.
You can use std::get_time to parse the input into a std::tm structure. You can use std::chrono::system_clock::from_time_t to convert the std::tm object into a time point. You can subtract the time points to calculate the difference.
In C++20, you may streamline the code using std::chrono::parse.
Validate your input with a regex like this regex time.
Use Posix time to create arrival and departure objects.
Use diffTime to get the difference. Example here

extract local time in a specific format using boost library

I try to extract local time including time zone and format this in a format similar to the following
2019-11-06T00:39:21.25+02:00 with precision of milliseconds.
Any idea how to do this?

A one line function to convert a hh:mm:ss.zzz time QString to milliseconds?

I'm using Qt and I have a time QString of format hh:mm:ss:zzz, such as 01:59:25.345. I am wondering if there is already some handy functions in Qt or c++ that can easily convert this to milliseconds. As to the QTime::fromMSecsSinceStartOfDay(12334).toString("hh:mm:ss.zzz"); for the other way around.
I think your format of Qt::ISODateWithMs is considered valid ISO 8601. Just prepend an aribitrary date with a T delimiter between date a and time.
QString timestamp = "2020-01-01T01:59:25.345";
int milliseconds = QDateTime::fromString(timestamp, Qt::ISODateWithMs).time().msecsSinceStartOfDay();
Also, if the current day is a daylight savings start/end day, I'm not sure how that impacts the calculation, of it it even matters.

How to convert imported date variable to the original format in Stata?

My original date variable is like this 19jun2015 16:52:04. After importing, it looks like this: 1.77065e+12
The storage type for the new imported variable is str11 and display format is %11s
I wonder how I can restore it back to date format?
William Lisowski gives excellent advice in his comment. For anyone using date-times in Stata, there is a minimal level of understanding without which confusion and outright error are unavoidable. Only study of the help so that your specific needs are understood can solve your difficulty.
There is a lack of detail in the question which makes precise advice difficult (imported -- from what kind of file? using which commands and/or third party programs?), except to diagnose that your dates are messed up and can only be corrected by going back to the original source.
Date strings such as "19jun2015 16:52:04" can be held in Stata as strings but to be useful they need to be converted to double numeric variables which hold the number of milliseconds since the beginning of 1960. This is a number that people cannot interpret, but Stata provides display formats so that displayed dates are intelligible.
Your example is when converted a number of the order of a trillion but if held as a string with only 6 significant figures you have, at a minimum, lost detail irretrievably.
These individual examples make my points concrete. di is an abbreviation for the display command.
clock() (and also Clock(), not shown or discussed here: see the help) converts string dates to milliseconds since Stata's origin. With a variable, you would use generate double.
. di %23.0f clock("19jun2015 16:52:04", "DMY hms")
1750351924000
If displayed with a specific format, you can check that Stata is interpreting your date-times correctly. There are also many small variations on the default %tc format to control precise display of date-time elements.
. di %tc clock("19jun2015 16:52:04", "DMY hms")
19jun2015 16:52:04
The first example shows that even date-times which are recent dates (~2016) and in integer seconds need 10 significant figures to be accurate; the default display gives 4; somehow you have 6, but that is not enough.
. di clock("19jun2015 16:52:04", "DMY hms")
1.750e+12
You need to import the dates again. If you import them exactly as shown, the rest can be done in Stata.
See https://en.wikipedia.org/wiki/Significant_figures if that phrase is unfamiliar.