Checking if OSX is on a 24 hour clock - c++

I would prefer to do this with Qt Methods if at all possible.
Currently in our code, we can distinguish that Windows is on a 24 hour clock; however not on Mac.
We have a method that returns a string such as: 1/9/2012 9:53:42 AM - Which is giving us a previous time, not the current one (Which is what we want), I do not want to mess with this method though.
I've been playing around with a way to determine if the current system clock is in military time; and to adjust the previous time returned from the string to reflect that. I can get this to work on Windows, but on Mac - it displays a normal 12-hour time regardless of whether we're on a 24-hour clock.
Ignore my crude-debugging messages or if I'm not particularly going at the problem correctly - I haven't been able to test it yet and tweak as necessary: (Explanation after code)
QLocale *ql = new QLocale();
QString qlTF = ql->timeFormat();
QString fileTime = QString::fromUtf8(str.GetSafeStringPtr());
if (qlTF.left(1) == (QString("H"))) // Our system clock is set to military time
{
QString newTime;
QStringList fileTimeDateSplit = fileTime.split(" ");
QStringList fileTimeSplit = fileTimeDateSplit.at(1).split(":");
m_editModified->setText(qlTF);
if (fileTimeSplit.at(0).toInt() < 12 && (fileTimeDateSplit.at(2) == "PM"))
{
int newHour = 12 + (fileTimeSplit.at(0).toInt()%12);
newTime.append(QString::number(newHour));
newTime.append(":");
newTime.append(fileTimeSplit.at(1));
newTime.append(":");
newTime.append(fileTimeSplit.at(2));
m_editModified->setText(QString("military after noon"));
}
}
else m_editModified->setText(qlTF);
Basically I'm grabbing the locale of the current machine to retrieve the system's time format.
fileTime is set to a string such as "1/9/2012 9:53:42 AM".
qlTF returns a format such as: HH:mm:ss , H:mm:ss, hh:mm:ss, or h:mm:ss - capital meaning it's a 24 hour clock.
I tokenize the different strings by the delimiters and then check to see if the time was greater than 12 and PM; then add the additional time and combine the new time string.
You can see that I did:
m_editModified->setText(qlTF);
for debugging purposes. On Windows, this will be set to HH:mm:ss; however even with a 24-hour clock enabled on a mac, it still returns h:mm:ss - which completely defeats the purpose.
Any ideas would be very much appreciated!

Why don't you just convert the string you have ("1/9/2012 9:53:42 AM") to QDateTime and then convert that QDateTime back to string in the format you want (I use ISODate in the example):
QString timeFormat = "M/d/yyyy h:m:s AP";
QDateTime dt = QDateTime::fromString("1/9/2012 9:53:42 AM", timeFormat);
QString text = "";
if (dt.isValid())
text = dt.toString(Qt::ISODate);

Related

Datetime parse and format in C++

I'm using time_point the first time.
I want to parse datetime from string and found a difference when returning back to string from 1 hour.
std::chrono::system_clock::time_point timePoint;
std::stringstream ss("2021-01-01 00:00:09+01");
std::chrono::from_stream(ss, "%F %T%z", timePoint);
// timePoint == {_MyDur={_MyRep=16094556090000000 } }
std::string timePointStr = std::format("{:%Y/%m/%d %T}", floor<std::chrono::seconds>(timePoint));
// timePointStr = "2020/12/31 23:00:09"
I don't know what is wrong: the timepoint and the parsing or the formatting to string?
How can I get the same format as the parsed one?
This is the expected behavior.
Explanation:
system_clock::time_point, and more generally, all time_points based on system_clock, have the semantics of Unix Time. This is a count of time since 1970-01-01 00:00:00 UTC, excluding leap seconds.
So when "2021-01-01 00:00:09+01" is parsed into a system_clock::time_point, the "2021-01-01 00:00:09" is interpreted as local time, and the "+01" is used to transform that local time into UTC. When formatting back out, there is no corresponding transformation back to local time (though that is possible with additional syntax1). The format statement simply prints out the UTC time (an hour earlier).
If you would prefer to parse "2021-01-01 00:00:09+01" without the transformation to UTC, that can be done by parsing into a std::chrono::local_time of whatever precision you desire. For example:
std::chrono::local_seconds timePoint;
std::stringstream ss("2021-01-01 00:00:09+01");
from_stream(ss, "%F %T%z", timePoint);
...
Now when you print it back out, you will get "2021/01/01 00:00:09". However the value in the rep is now 1609459209 (3600 seconds later).
1 To format out at sys_time as a local_time with a UTC offset of 1h it is necessary to choose a time_zone with a UTC offset of 1h at least at the UTC time you are formatting. For example the IANA time zone of "Etc/GMT-1" always has an offset of 1h ... yes, the signs of the offset are reversed. Using this to transform 2020-12-31 23:00:09 UTC back to 2021-01-01 00:00:09 would look like:
std::chrono::sys_seconds timePoint;
std::stringstream ss("2021-01-01 00:00:09+01");
std::chrono::from_stream(ss, "%F %T%z", timePoint);
// timePoint == 2020-12-31 23:00:09 UTC
std::string timePointStr = std::format("{:%Y/%m/%d %T}",
std::chrono::zoned_time{"Etc/GMT-1", timePoint});
cout << timePointStr << '\n'; // 2021/01/01 00:00:09
Disclaimer: I do not currently have a way to verify that MSVC supports the "Etc/GMT-1" std::chrono::time_zone.
Fwiw, using "Africa/Algiers" or "Europe/Amsterdam" in place of "Etc/GMT-1" should give the same result for this specific time stamp. And if your computer has its local time zone set to something that has a 1h UTC offset for this timestamp, then std::chrono::current_zone() in place of of "Etc/GMT-1" will also give the same result.

How to get Current Date in Ballerina

Is there a way to get the current date in ballerina?
As I was browsing through some code examples I came across the syntax to get the current time. Shown below is how to get the current date in Ballerina:
Note: first you have to import the time package given below for this to work.
import ballerina/time;
Then put the following lines of code:
time: Time currentTime = time:[currentTime][2]();
string customTimeString = currentTime.format("dd-MM-yyyy");
This will give the following output:
08-07-2018
This is work for ballerina 0.991 and 1.0 first you have to import the time package
Then it will give the current date if you want to get in a format it will included the code
import ballerina/time;
To get current time
time:Time time = time:currentTime();
string standardTimeString = time:toString(time);
io:println("Current system time in ISO format: ", standardTimeString);
To format the time
string|error customTimeString = time:format(time, "yyyy-MM-dd-E");
if (customTimeString is string) {
io:println("Current system time in custom format: ", customTimeString);
}
y -Years
M -months
d -date
E -day
h -hour
m -Minuit
s -seconds
For Swan Lake Update 3 they seem to have removed the time:currentTime() function.
It seems they have replaced it with time:utcNow().
According to the ballerina documentation,
"The time:Utc is the tuple representation of the UTC. The UTC represents the number of seconds from a specified epoch. Here, the epoch is the UNIX epoch of 1970-01-01T00:00:00Z."
So you can convert this above tuple representation to RFC 3339 timestamp by using,
time:Utc currTime = time:utcNow();
string date = time:utcToString(currTime);
io:println(date);
Then you will get a result like below,
2023-01-14T17:04:15.639510400Z
Using ballerina time library you can convert to other different representations as well.

Getting build date and time in RedHat 7.2

I'm trying to add the build date and time to my Qt 5.6 project file, so far I have added:
win32 {
DEFINES += BUILDTIME=\\\"$$system('echo %time%')\\\"
DEFINES += BUILDDATE=\\\"$$system('echo %date%')\\\"
} else {
DEFINES += BUILDTIME=\\\"$$system(date '+%H:%M')\\\"
DEFINES += BUILDDATE=\\\"$$system(date '+%d/%m/%y')\\\"
}
And in the source code:
QString strBuildDT = QString::fromLocal8Bit(BUILDDATE)
+ ", " + QString::fromLocal8Bit(BUILDTIME);
Using this as an example I would get:
12/10/16, 17:39
I would like to reformat the date to display:
12 October 2016, 17:39
From research it looks like the correct date format to use would be:
DEFINES += BUILDDATE=\\\"$$system(date '+%d %B %Y')\\\"
But this doesn't work and returns and empty string for BUILDDATE.
I found a mailing list thread about this. This works (the purpose of $$quote is to prevent Qt from munging spaces, it actually should still produce a non-empty string without $$quote, the real key is the outer \"s)
DEFINES += \"BUILDDATE=\\\"$$quote($$system(date /T))\\\"\"
That works on Windows. I can't test on Linux right now but should be something like:
DEFINES += \"BUILDDATE=\\\"$$quote($$system(date '+%d %B %Y'))\\\"\"
This essentially puts quotes around the whole thing on the compiler command line and lets it work with spaces in the string. Example (Windows, mingw, Qt 4.8.1):
g++ ... -D"BUILDDATE=\"Wed 10/12/2016\"" ...
That said you still may want to just use date '+%s' to get epoch time then format on display with a QDateTime to use the current locale and timezone. Unfortunately, though, I do not know the command to get epoch time on Windows (cursory research does not bode well).
Solution for RedHat:
DEFINES += BUILDDATE=\\\"$$system(date '+%s')\\\"
In code:
QString strBuildDT = QString::fromLocal8Bit(BUILDDATE);
QDateTime qDT = QDateTime::fromMSecsSinceEpoch(strBuildDT.toLong() * 1000);
strBuildDT = qDT.toString("dd MMMM yyyy, HH:mm");
This works well, thank you to https://stackoverflow.com/users/616460/jason-c for the suggestion to try +s

Convert string to date object in Visual Studio

Need to convert a string, which stores the date in format like such: "Apr 23 2014 12:39:17" to a number or an object; Working with visual studio in a MS-specific environment.
In C++ is there a easy to use function that can achieve this?
I am doing this in order to do a comparison between the string date and now().
Thanks.
Here is the solution I found for my problem.
To clarify: A string representation of a date needed to be converted into some sort of date object, so that I could find the difference between 2 dates.
This works on MS VisualStudio2010 & uses the microsoft classes. (Basically; It won't work on a unix box!).
// Create 2 COleDateTime objects:
COleDateTime DateTime1;
COleDateTime DateTime2;
// 'Get' 2 string dates:
BSTR time1 = L"Apr 24 2014 09:20:20";
BSTR time2 = L"Apr 23 2014 12:39:17";
// Parse the string dates into the date objects (See! Its alot easier then I thought!)
DateTime1.ParseDateTime(time1);
DateTime2.ParseDateTime(time2);
// Calculate the time difference with a COleDateTimeSpan Object...
COleDateTimeSpan timeSpan = DateTime2 - DateTime1;
// Create integer with the difference in time in seconds...
CString str = timeSpan.Format(_T("%S"));
int differenceInSeconds = _tstoi(str);
Hope this helps someone!

Display Timezone (e.g. PST) in ColdFusion

I am trying to display client's timezone besides the timestamp.
E.g 4:13 PST
I tried using GetTimeZoneInfo() but the only way I could think of is by getting the offset in hours and then determining through an array of hard coded values.
Other way around I found was using java.util.TimeZone class. Following is the code I have tried ---
<cfset tz = CreateObject("java", "java.util.TimeZone")>
<cfset tz = tz.getDefault()>
<cfoutput>TimeZone:#tz.getDisplayName(false, 1)#</cfoutput>
This gives me output as Central Standard Time.
Any further help...
The code you mention above gets the server's TZ, not the client's.
If you want the client's TZ, you should read the comments against this other, similar question. These all revolve around using the Date.getTimezoneOffset() method. This does only give you the offset from UTC though, not the more familiar GMT / BST etc.
If you are allowing your users to select their time zone instead of getting it from the browser which potentially could be inaccurate, or they are coming from database values such as time zone per city, etc, or you simply need to extract the abbreviation from any datetime value, you can parse it out of the return value from LSDateTimeFormat() with the "long" mask.
function tzabbr(required date dttm, string tz = "", string locale = GetLocale()) {
var str = tz == ""
? LSDateTimeFormat(dttm, "long", locale)
: LSDateTimeFormat(dttm, "long", locale, tz)
return ListLast(str, " ")
}
// Usage Examples
dttm = Now()
tzServ = tzabbr(dttm)
tzWest = tzabbr(dttm, "US/Pacific")
tzEast = tzabbr(dttm, "US/Eastern")
https://trycf.com/gist/144aa0399ea80127a3aa1d11a74fc79b/acf2021?theme=monokai