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
Related
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.
i'm using the following var on my script to send output to one
output = "/opt/output"
i want to adjust it to make the output relative to the date current date of trigger the script it should be structured like this
output = "/opt/output/year/month/day"
i'm not sure if i'm using the correct way here i used the following approach
output = "/opt/output/" + today.strftime('%Y%m%d')
any tips here
I recommend you use the full timestamp instead of just using the date:
import os
mydir = os.path.join(output, datetime.datetime.now().strftime('%Y/%m/%d_%H-%M-%S'))
It's recommended do it this way because what'd happen if your script runs more than once a day ? You should at least add a counter or something (if you don't want the full timestamp) which will increment some variable if the folder already exist.
You can read more about os.path.join here
As per creating a folder, you can do it like this:
if not os.path.exists(directory):
os.makedirs(mydir)
i figure it by
today = datetime.datetime.now()
year = today.strftime("%Y")
month=today.strftime("%m")
day=today.strftime("%d")
output = "/opt/output/" + year +"/" + month + "/" + day
thats worked fine to me
I will suggest using os.path.join and os.path.sep:
import os
.
.
.
full_dir = os.path.join(base_dir, today.strftime('%Y{0}%m{0}%d').format(os.path.sep))
today.strftime('%Y%m%d') would print todays date as 20170607. But I guess you want it printed as 2017/06/07. You could explicitly add the slashes and print it something like this?
output = "/opt/output/" + today.year +"/" + today.month + "/" + today.date
I have a very specific C++ project, and I use a NetBeans.
Reason for it is because we need to have a specific timestamps, and I found NetBeans templates a great tool for inserting an automatic header with all the relevant stuff.
I manage set everything up nicely, but I can't figure out how to set up the time format in the header template.
Currently it shows this:
Created on April 6, 2017, 2:18 PM
But since I work in Central Europe, I need a 24h hour format so I could have something like
Created on 06.04.2017. at 14:18
I found on how to change a date format here, but it doesn't work for times for some reason.
I even tried with using FreeMaker's template language reference, so I created a variable time that looks like this:
<#assign dateTime = .now>
<#assign time = dateTime?time>
${time?iso("Europe/Zagreb")}
But it still didn't change anything.
Now my template looks like this:
// -*- C++ -*-
<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#assign aDateTime = .now>
<#assign time = aDateTime?time>
<#include "${project.licensePath}">
/*
* File: ${NAME}.${EXTENSION}
* Author: ${user}
*
* Created on ${DATE} at ${time?iso("Europe/Zagreb")}
*/
#ifndef ${GUARD_NAME}
#define ${GUARD_NAME}
#endif /* ${GUARD_NAME} */
Is this possible to be changed at all, and how?
Any help is appreciated.
In your question you link a resource (THANKS for that!!!) suggesting the following for dates:
${date?date?string("dd.MM.yyyy")}
I tried the same for time and it works well:
${time?time?string("HH.mm.ss")}
BTW I also tried producing some errors and got some nice error messages stating what NB expects and what it gets pretty clearly:
${date?time?string("hh.mm.ss")}
${time?date?string("hh.mm.ss")}
${time?datetime?string("hh.mm.ss")}
${date?datetime?string("hh.mm.ss")}
produced:
Error: on line 20, column 6 in Templates/Classes/Class.java
The string doesn't match the expected date/time format. The string to parse was: "11-Jan-2018". The expected format was: "HH:mm:ss".
Error: on line 21, column 6 in Templates/Classes/Class.java
The string doesn't match the expected date/time format. The string to parse was: "13:40:27". The expected format was: "dd-MMM-yyyy".
Error: on line 22, column 6 in Templates/Classes/Class.java
The string doesn't match the expected date/time format. The string to parse was: "13:40:27". The expected format was: "dd-MMM-yyyy HH:mm:ss".
Error: on line 23, column 6 in Templates/Classes/Class.java
The string doesn't match the expected date/time format. The string to parse was: "11-Jan-2018". The expected format was: "dd-MMM-yyyy HH:mm:ss".
I've tried to convert datetime string into datetime of an SArray (uses C++ booster library), but it does not seem to understand the %p format specifier. http://www.boost.org/doc/libs/1_43_0/doc/html/date_time/date_time_io.html
This documentation says specifiers marked with ! do not currently work for input.
Does that mean that you cannot parse anything with pm or PM?
I was able to get the string-to-datetime conversion to work by making two small changes:
Use %I for the hour on a 12-hour clock (%H is for a 24 hour clock).
Use %P (upper case) for the AM/PM flag.
Here's what works for me:
sf = gl.SFrame({'date': ['2015-11-06 02:12:42 pm',
'2015-11-05 03:43:11 pm']})
sf['date2'] = sf['date'].str_to_datetime('%Y-%m-%d %I:%M:%S %p')
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);