Why there are no + and - operators in QTime? - qt

I have 2 QTimes (intended as time intervals), let's say 1:10:00 and 50:45 (HH:MM:SS).
It seems pretty natural to ask the result of time1 + time2 or time1 - time2.
One has to resort to all sort of very inelegant calls to addMSecs, msecsSinceStartOfDay, secsTo and so on.
Why have they not added simple operator+ and - to QTime?
Maybe QTime is not a time interval but the time part of a datetime, but then, is there any time interval in QT?

Related

Adding time in milliseconds to Time Stamp in R

I am having an issue with adding time in ms to a specific time stamp. My data contain two variables, Start Time (Time, in HH:MM:SS.ss format) and time for a specific event given in ms that have passed since the Start Time (STime, numeric variables, i.e. 640654 ms, which equals 640.654 s or 10.678.. min). For example:
Time <- c("16:44:38.00", "16:44:38.00", "16:44:38.00")
STime <- c(640657, 940640, 955301)
Here on the forum I found the code (thank you #tstev) that should allow calculating FTime - Start Time with STime added, and shown in HH:MM:SS.ss format:
FTime <- strftime(strptime(Time,format="%H:%M:%OS")+(STime %% 1)+0.005,format="%H:%M:%OS3")
However, in my case it does not add STime properly - I've tried to present STime in seconds instead of ms (640.657, 940.640, 955.301) and then add - it still does not provide accurate results, here's what I've got:
"16:44:38.662", "16:44:38.645", "16:44:39.306"
Does anyone know how to fix this?
Cheers
You can use the library hms. I divided the microseconds by 1000 to render them as seconds, then converted the values.
library(hms)
library(tidyverse)
Time <- c("16:44:38.00", "16:44:38.00", "16:44:38.00") %>% as_hms()
STime <- (c(640657, 940640, 955301) / 1000) %>% hms(seconds = .)
(Time + STime) %>% as_hms()
# 16:55:18.657
# 17:00:18.640
# 17:00:33.301
Let me know if you have any questions.

Informix FROM_UNIXTIME alternative

I was searching for a way to group data by interval (ex: every 30 minutes) using the date defined in that table, so i need to convert that date time to milliseconds so that i can divide it by the interval i need like in this query
SELECT FLOOR(UNIX_TIMESTAMP(timestamp)/(15 * 60 * 1000)) AS timekey
FROM table
GROUP BY timekey;
This query is running perfectly on SQL Server but on informix it's giving me the error
Routine (unix_timestamp) can not be resolved.
As it's not defined in IBM Informix server.
So i need a direct way to get epoch unix time from timestamp DATETIME YEAR TO FRACTION(3) column in IBM informix server like 'UNIX_TIMESTAMP' in SQL server.
If the timestamp column is of type DATETIME YEAR TO SECOND or similar, then you can convert it to a DECIMAL(18,5) number of seconds since the Unix Epoch, aka 1970-01-01 00:00:00Z (UTC; time zone offset +00:00) using a procedure such as this:
{
# "#(#)$Id: tounixtime.spl,v 1.6 2002/09/25 18:10:48 jleffler Exp $"
#
# Stored procedure TO_UNIX_TIME written by Jonathan Leffler (previously
# jleffler#informix.com and now jleffler#us.ibm.com). Includes fix for
# bug reported by Tsutomu Ogiwara <Tsutomu.Ogiwara#ctc-g.co.jp> on
# 2001-07-13. Previous version used DATETIME(0) SECOND TO SECOND
# instead of DATETIME(0:0:0) HOUR TO SECOND, and when the calculation
# extended the shorter constant to DATETIME HOUR TO SECOND, it added the
# current hour and minute fields, as documented in the Informix Guide to
# SQL: Syntax manual under EXTEND in the section on 'Expression'.
# Amended 2002-08-23 to handle 'eternity' and annotated more thoroughly.
# Amended 2002-09-25 to handle fractional seconds, as companion to the
# new stored procedure FROM_UNIX_TIME().
#
# If you run this procedure with no arguments (use the default), you
# need to worry about the time zone the database server is using because
# the value of CURRENT is determined by that, and you need to compensate
# for it if you are using a different time zone.
#
# Note that this version works for dates after 2001-09-09 when the
# interval between 1970-01-01 00:00:00+00:00 and current exceeds the
# range of INTERVAL SECOND(9) TO SECOND. Returning DECIMAL(18,5) allows
# it to work for all valid datetime values including fractional seconds.
# In the UTC time zone, the 'Unix time' of 9999-12-31 23:59:59 is
# 253402300799 (12 digits); the equivalent for 0001-01-01 00:00:00 is
# -62135596800 (11 digits). Both these values are unrepresentable in
# 32-bit integers, of course, so most Unix systems won't handle this
# range, and the so-called 'Proleptic Gregorian Calendar' used to
# calculate the dates ignores locale-dependent details such as the loss
# of days that occurred during the switch between the Julian and
# Gregorian calendar, but those are minutiae that most people can ignore
# most of the time.
}
CREATE PROCEDURE to_unix_time(d DATETIME YEAR TO FRACTION(5)
DEFAULT CURRENT YEAR TO FRACTION(5))
RETURNING DECIMAL(18,5);
DEFINE n DECIMAL(18,5);
DEFINE i1 INTERVAL DAY(9) TO DAY;
DEFINE i2 INTERVAL SECOND(6) TO FRACTION(5);
DEFINE s1 CHAR(15);
DEFINE s2 CHAR(15);
LET i1 = EXTEND(d, YEAR TO DAY) - DATETIME(1970-01-01) YEAR TO DAY;
LET s1 = i1;
LET i2 = EXTEND(d, HOUR TO FRACTION(5)) -
DATETIME(00:00:00.00000) HOUR TO FRACTION(5);
LET s2 = i2;
LET n = s1 * (24 * 60 * 60) + s2;
RETURN n;
END PROCEDURE;
Some of the commentary about email addresses is no longer valid – things have changed in the decade and a half since I wrote this.

Dates subtraction: has the event occurred or not?

If I have everyday datetime - how to find out, the event has already occurred or not, by subtraction with datetime.now()
Let we had everyday meeting at 15:35. Today John came earlier - at 12:45, but Alex was late for 2 h. and 15 min. (came at 17:40).
meet_dt = datetime(year=2015, month=8, day=19, hour=15, minute=35)
john_dt = datetime(year=2015, month=8, day=19, hour=12, minute=45)
alex_dt = datetime(year=2015, month=8, day=19, hour=17, minute=40)
print(meat_dt - john_dt) # came before > 2:50:00
print(meat_dt - alex_dt) # came after > -1 day, 21:55:00
If I take away from the big date less - then everything is fine, but conversely I recive -1 day, 21:55:00 why not -2:15:00, what a minus day?
Because timedeltas are normalized
All of the parts of the timedelta other than the days field are always nonnegative, as described in the documentation.
Incidentally, if you want to see what happened first, don't do this subtraction. Just compare directly with <:
if then < datetime.datetime.now():
# then is in the past

time difference's seems like working odd?

I am going to find difference between two times but I am not getting what I want!!!
I have 2 timeEdit components in a form
here is my code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TTime time1=StrToTime(t1->Text);
TTime time2=StrToTime(t2->Text);
//t1->Text=time2-StrToTime("3:00");
ShowMessage((time2-time1).TimeString());
}
If I set t1 = 02:00
and set t2 = 01:00
it shows 1:00
but I expect 23:00
that is 01:00 - 02:00 should be 23:00
where I am wrong ?
You are not taking into account how TTime is encoded. TDateTime is a Double, where the integral portion contains the number of days since Dec 30 1899, and the fractional portion contains a percentage of a 24-hour day (this information is stated in the C++Builder documentation). TTime is just the fractional portion of a TDateTime with its integral portion ignored. Because of this encoding, performing such seemingly simple mathematical operations on date/time values does not usually produce the kind of result you are expecting.
02:00 (2 AM) is represented as 0.083333333333, and 01:00 (1 AM) is represented as 0.041666666667. You are subtracting 2 AM from 1 AM, expecting it to subtract 2 hours to produce 11 PM (which is represented as 0.958333333333333). Subtracting 0.083333333333 from 0.041666666667 produces -0.041666666667. Ignoring the integral portion (the date), the fractional portion is a positive value from 0.0 (midnight), so -0.041666666667 is equivilent to 0.041666666667, which is 1 AM.
In order for your subtraction to work correctly, the starting time needs a positive integral portion (date) attached to it so the result contains the correct fractional portion, eg:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TTime t = (1.0 + StrToTime("01:00")) - StrToTime("02:00");
// (1.0 + 0.041666666667) - 0.083333333333
// = 1.041666666667 - 0.083333333333
// = 0.95833333333
ShowMessage(t.TimeString());
}

Constant Pointer / structs

In my programming class, we have
struct Time {
int hours, min, sec;
}
We are to create a method to compute the difference between two times:
Time *timeDiff(const Time *t1, const Time *t2)
I thought I could create the time difference by getting everything in seconds, and then subtracting the two values, but it seems like extra work to do something like
long hour1 = t1->hours;
long min1 = t1->min;
long sec1 = t1->sec;
And then using these values to get the time in seconds, do something similar for the second time, and then subtract. Any thoughts? Thanks!
The way you've described it sounds exactly right. I might do something like
int sec = t1->sec + 60*(t1->min + 60*t1->hours);
Then similarly for t2, then subtract one from the other and break the result down into minutes and hours.

Resources