Cognos Two Datetime Prompts Seconds Difference - datetime

I would like to calculate seconds difference between two datetime prompt values. I would like to do it by using query calculation. I'm using DB2 and i have to use DB2 functions. I have somthing like this but it didn't work for datetime prompts, it works only like this:
(DAYS(localtimestamp) - DAYS([FIRSTOCCURRENCE])) * 86400 +
(MIDNIGHT_SECONDS(localtimestamp) - MIDNIGHT_SECONDS([FIRSTOCCURRENCE]))
I want to use it like this :
(DAYS(?endDate?) - DAYS(?beginDate?)) * 86400 +
(MIDNIGHT_SECONDS(?endDate?) - MIDNIGHT_SECONDS(?beginDate?))
How can i do that ? Is there anyway to do this ?
PS: localtimestamp gives Aug 20, 2014 5:26:51 PM kind of result. But prompt gives 2014-08-21T10:53:09.166. Thats the main problem i couldn't convert it.

Try
_days_between(?endDate?;?beginDate?) * 86400 +
(extract(hour; #prompt('endDate', 'timestamp')#) - extract (hour; #prompt('beginDate', 'timestamp')#)) * 3600 +
(extract(minute; #prompt('endDate', 'timestamp')#) - extract (minute; #prompt('beginDate', 'timestamp')#)) * 60 +
extract(second; #prompt('endDate', 'timestamp')#) - extract (second; #prompt('beginDate', 'timestamp')#)

Query calculation should be like this, i finally figured it out:
(hour(substring (?endTime?,1,8))*3600+minute(substring (?endTime?,1,8))*60+second(substring (?endTime?,1,8)))-
(hour(substring (?beginTime?,1,8))*3600+minute(substring (?beginTime?,1,8))*60+second(substring (?beginTime?,1,8)))+_days_between (?endDate?,?beginDate?)*86400

There is Cogos Common Function CAST. It is processed by Cognos itself. You can convert days to seconds:
Cast (_days_between(?endDate?;?beginDate?), INTERVAL DAY TO SECOND)
It would be great if you can update your answer and mark where you need to use data items (in []), parameters (in ??), Cognos functions and separately DB2 functions.

Related

How do I compare two dates(different timezones) that I have in view( I can not set timezone parameter) in snowflake?

How do I compare two dates(different timezones) that I have in view( I can not set timezone parameter) in snowflake ? Examples of dates to be compared is "2021-12-02 06:00:56.000" < "2021-12-02 00:57:56.107 -0800".
The result should be TRUE as "2021-12-02 00:57:56.107 -0800" is "2021-12-02 08:57:56.107" which is greater than "2021-12-02 06:00:56.000".
When I simply try SELECT '2021-12-02 06:00:56.000' < '2021-12-02 00:57:56.107 -0800' this results in FALSE probably because it compares "2021-12-02 06:00:56.000" with ""2021-12-02 00:57:56.107" and does not take -0800 in account.
P.S. : "-0800" is not fixed so I can not hardcode -8 in dateadd function.
Something like this would work:
SELECT '2021-12-02 06:00:56.000' < convert_timezone('America/Los_Angeles', 'UTC','2021-12-02 00:57:56.107 -0800');
Use the convert_timezone function to convert from one timezone to another, depending on your location and preferred timezone that you want to use, eg. local or UTC.
More Details: https://docs.snowflake.com/en/sql-reference/functions/convert_timezone.html#convert-timezone

Groovy: Date and Time comparisons with a slight delay

So I have the following script:
import groovy.time.TimeCategory
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC')) }
assert dueDate == after24Hours
What I'm trying to do with this is take the date and time from a REST request (dueDate - which comes in UTC format and with a 24h delay) and create a new date and time from the timestamp of the moment when that request has been sent, which is registered from my system. I then convert that time to UTC to accommodate the format from dueDate and add 24h to it. At the end I verify that the date and time from dueDate and after24Hours is the same.
The output does return the same time but in certain cases if there is a delay between the time the request is being sent and the time is received then the assertion will fail. This depends on the server, usually there is a difference of like 1 millisecond but I'm thinking that if the server will be slower at some point this will definitely be bigger.
What could I do to allow some margin of error in the assertion, maybe like a few seconds or even a couple of minutes?
Ok, so I managed to do this:
import groovy.time.*
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))
def date1 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", dueDate)
def date2 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", after24Hours)
TimeDuration difference = TimeCategory.minus(date2, date1)
log.info date1
log.info date2
assert difference < 2.minutes
}
The script seems to work and it does return an error only if the time is longer than the one I've set in the assertion.
Unfortunately I have another issue now.
For some reason, my date output looks like this:
Fri Oct 01 16:24:10 EEST 2021: INFO: Sat Oct 02 13:24:10 EEST 2021
Which is not the correct format. That date should appear in the Zulu format, after all when I parsed the dates that was the format that I used.
Am I missing something?
What could I do to allow some margin of error in the assertion, maybe
like a few seconds or even a couple of minutes?
Instead of asserting that they are equal, you could assert that the difference between them is less than a threshold that you get to define.
If you use something like AssertJ, and I'd recommend you do, then you can do something like the following:
assertThat(dueDate).isCloseTo(after24Hours, within(1, ChronoUnit.MINUTE));
This will give a small margin to the comparison of the dates, and should fix your issue.

How to express select with max() and ifnull() in peewee

I have a SQLite-Select statement like this to get the next available number:
SELECT IFNULL(MAX(current_nr)+1, 1) FROM rm_evaluation;
I already have a corresponding model in python peewee:
class RmRiskEvaluation(BaseModel):
...
current_nr = IntegerField(unique=True)
class Meta:
table_name = 'rm_evaluation'
But how can I express the SQL-statement from above.
=> Get the last number, add 1 to it and return the whole thang; if there is no last number at all, calculate with 1 beforehand.
If you weren't so lazy and had spent even a couple minutes reading the docs or searching, you would have found your answer.
fn.IFNULL(fn.MAX(RmRiskEvaluation.current_nr) + 1, 1)

Using VFreebusy iCalendar component

I making a scheduling application and I am using iCalendar format. I am aware that I can use this code to get the free time slots from current calendar:
DateTime start = new DateTime();
DateTime end = new DateTime(start.getTime() + 1000 * 60 * 60 * 24 * 7);
VFreeBusy request = new VFreeBusy(start, end, new Dur(0, 2, 0, 0));
VFreeBusy response = new VFreeBusy(request, myCalendar.getComponents());
And I get the following output from using this code on a couple of events in the calendar.
DTSTAMP:20140323T204423Z
DTSTART:20020202T040023Z
DTEND:20020203T040023Z
DURATION:PT45M
FREEBUSY;FBTYPE=FREE:20020202T040023Z/PT2H,20020202T070023Z/PT4H,20020202T120023Z/PT16H
END:VFREEBUSY
What I don't know is how to use that VFreeBusy object with those free time slots and actually get them out, so I can compare them and use them as dates and times.
I used response.getProperties().getProperty(Property.FREEBUSY) to get the part that I need, but I don't know how to parse all that String. If you have any other ways for me to get those time slots please advise.
Assuming that you are using ical4j. Once you get the property, you can cast it to a FreeBusy property which has a getPeriods() method.

Compare date only (without time) in JPA2 (JPQL)

Im trying to compare Calendars with JPA2. The query looks somewhat like that:
TypedQuery<X> q = em.createQuery("select r from Record r where r.calendar= :calendar", X.class);
Calendar c = foo(); // setting fields and stuff
q.setParameter("calendar", c);
This, however, compares the date + time. I want to know if MM:DD:YYYY is equal and do not care about the time. Is there a nice way to do that in JPA2 or do I have to create a native query?
I tried setting HH:MM:SS:... to zero before saving it in the db but I don't know if this is very wise, regarding time zones and daylight saving and stuff.
q.setParameter("calendar", c, TemporalType.DATE)
You can pass the TemporalType.DATE to setParameter method to truncate the date+time.
There is no mention of DateTime functions allowing to do that in the spec of JPQL, but you could always cheat and do
select r from Record r where r.calendar >= :theDayAtZeroOClock and r.calendar < :theDayAfterAtZeroOClock
Mysql and H2 compatible comparison of dates ignoring time part:
`#Query("SELECT DISTINCT s " +
"FROM Session s " +
"JOIN s.movie m " +
"WHERE m.id = :movie AND CAST(s.time AS date) = CAST(:date AS date) " +
"ORDER BY s.time")
List<Session> getByMovieAndDate(#Param("movie") Long movie, #Param("date") LocalDateTime date);`
When using an Oracle database, you can use the trunc function in your JPQL query, e.g.:
TypedQuery<X> q = em.createQuery("select r from Record r where trunc(r.calendar) = trunc(:calendar)", X.class);
See also https://cirovladimir.wordpress.com/2015/05/18/jpa-trunc-date-in-jpql-query-oracle/
I had to use date_trunc on the where clause:
TypedQuery<X> q = em.createQuery("select r from Record r where date_trunc('day',r).calendar= :calendar", X.class);
Calendar c = foo(); // setting fields and stuff
q.setParameter("calendar", c, TemporalType.DATE);
In Hibernate 6 and above, you can use date_trunc(text, timestamp) to truncate the timestamp more precisely, for example:
date_trunc('hour', timestamp) to truncate the timestamp up to the hour (no minutes and no seconds).

Resources