Compare time
Hello, guys.
Say I have two horologs or timestamps, how can I compare them? I know that just time1 > time2 doesn't work because it will compare them as strings which is not correct.
Comments
To find way how to compare time, you should remember format for $horolog, which is has two parts separated with comma. First of part it is a date, which is actually numbers of days since 1841 year, so $horolog=1 it is 01/01/1841. Next part is a seconds in a day, no more than 86400, and may have milliseconds after point.
Know it, you can easily convert $horolog to seconds and compare them
SAMPLES>set date1=$zdth("01/02/2016 10:00")
SAMPLES>set date2=$zdth("01/02/2016 20:00")
SAMPLES>zwrite date1,date2
date1="63919,36000"
date2="63919,72000"
SAMPLES>write ($piece(date1,",")*86400+$p(date1,",",2))>($p(date2,",")*86400+$p(date2,",",2))
0
SAMPLES>write ($piece(date1,",")*86400+$p(date1,",",2))<($p(date2,",")*86400+$p(date2,",",2))
1You can also use SQL function DATEDIFF, to see difference in seconds between to dates
SAMPLES>write $system.SQL.DATEDIFF("ss",date1,date2)
36000
SAMPLES>write $system.SQL.DATEDIFF("ss",date2,date1)
-36000
SAMPLES>write $system.SQL.DATEDIFF("ss",date1,date1)
0
Thanks, the first option seems to be what I need
it depends what format your date-time is currently in but I just find it hard work to start messing about with $piece etc, I use the FOLLOWS command " ] "
USER>set date1=$zdth("01/02/2016 10:00")
USER>set date2=$zdth("01/02/2016 20:00")
USER>zwrite date1,date2
date1="63919,36000"
date2="63919,72000"
USER>write date1]date2
0
USER>write date2]date1
1
USER>it works in external format as well
USER>s date1="2016-02-01 10:00" USER>s date2="2016-02-01 20:00" USER>w date1]date2 0 USER>w date2]date1 1
kevin
very interesting solution
FOLLOWS gives wrong result in some cases:
USER>set date1 = $zdth("2016-01-02 00:15",3)
USER>set date2 = $zdth("2016-01-02 01:00",3)
USER>zwrite date1,date2
date1="63919,900"
date2="63919,3600"
USER>write date1]date2
1
If you do not care about fractions of seconds, convert the $H values to count of seconds .. $zdt(datetime,-2) .. and then compare the integers:
set date1 = $zdth("2016-01-02 00:15",3)
set date2 = $zdth("2016-01-02 01:00",3)
zw date1,date2
w $zdt(date1,-2)>$zdt(date2,-2)
If you do care about the seconds fractions you can use the follows operator with some of the external formats:
s d1=$zts h .001 s d2=$zts
zw d1,d2
d1="64180,33196.069"
d2="64180,33196.07"
w $zdt(d1,3,1,6)]$zdt(d2,3,1,6)
0
w $zdt(d2,3,1,6)]$zdt(d1,3,1,6)
1
/// note: comparing the count of seconds would consider the d1 and d2 equal.