Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
JABELLA1634831127
Contributor III
Contributor III

Is it possible to create a timestamp in tmap output that iterates per 5 minutes?

So currently I have a date that I ingested from a filename and it has a format of "dd-MM-yyyy HH:mm" and the HH:mm is currently 00:00. Is it possible that it iterates per 5 minutes 00:00, 00:05 up to 23:55?

0695b00000L0ZpmAAF.png

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

This will do it....

 

public class DateIteration {

 

 

private static Date date;

private static int initialDay;

 

public static Date iterateDates(Date sourceDate, int minutesToAdd) {

 

if(date==null) {

 

date = sourceDate;

initialDay = routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate);

}else{ //New section

if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){

date = routines.TalendDate.addDate(date,-1,"dd");

 

}

}

 

date = routines.TalendDate.addDate(date, 5, "mm");

 

/* Code moved to the "else" of the first if condition

if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){

date = routines.TalendDate.addDate(date,-1,"dd");

 

} */

 

return date;

}

 

}

 

I simply moved the if condition which deals with the amendment of the day in the date to the "else" of the first if condition. What this does is leave the very first time the day ticks over to the next day. Then the next time the method is called it checks the original day against the current value's day. When they do not match, it automatically removes a day.

 

You will need to ensure that all dates supplied have 00:00:00.000 as the time. If not, you will need to add something to this to ensure that the time is changed to 00:00:00.000.

View solution in original post

11 Replies
Anonymous
Not applicable

Hi @JOHN ZACHARY ABELLA​,

 

Yes, this is possible using a routine. You create the routine (basically this is a Java class) and call the method that you have created to do this in the tMap for every row.

 

An example routine for this can be seen below.....

 

public class DateIteration {

 

private static Date date;

public static Date iterateDates(Date sourceDate, int minutesToAdd) {

if(date==null) {

date = sourceDate;

}

date = routines.TalendDate.addDate(date, 5, "mm");

return date;

}

 

}

 

You'd call this in your tMap like this.....

 

routines.DateIteration.iterateDates({Your Date Column}, 5)

 

Where I have added {Your Date Column} you would place your date column there.

JABELLA1634831127
Contributor III
Contributor III
Author

Thank you so much!. It works perfectly 🙂

Anonymous
Not applicable

Glad to be able to help 🙂

JABELLA1634831127
Contributor III
Contributor III
Author

Hi rhall, I overlooked the data in my desired output table. So for my output table right now using your solution it goes 05-11- 2021 23:55 then 06-11-2021 00:00. instead of 05-11-2021 00:00. Would it be possible if only the timestamp iterates while the initial date remains static? So after reaching 00:00 again, the date should still be 05-11-2021

Anonymous
Not applicable

This is a quick fix to the routine I sent you. It might not be perfect, but should work for you....

 

public class DateIteration {

 

 

private static Date date;

public static Date iterateDates(Date sourceDate, int minutesToAdd) {

if(date==null) {

date = sourceDate;

}

date = routines.TalendDate.addDate(date, 5, "mm");

if(routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate)!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){

date = routines.TalendDate.addDate(date,-1,"dd");

}

return date;

}

 

}

JABELLA1634831127
Contributor III
Contributor III
Author

Thanks again! It works. May I ask the logic behind the code?

Anonymous
Not applicable

OK, so the first time this method is called in a Job, it will set the "date" static variable. If it has already been set, the date being passed in ("sourceDate") does nothing to the "date" variable. Then we add 5 minutes to the "date" variable. This means that every time this method is called, 5 minutes will be added to the "date" variable which is maintained as a static variable. This was the logic of the original code I sent.

 

The next bit was a bit of a quick fix to keep it to the same day. This is an ideal solution, but works in your scenario. To make sure that the day remains the same, we use the "sourceDate" param value and check its day of the week against the updated "date" variable's day of the week. If they are different, we subtract 1 day from the updated "date" value.

 

As I said, this is not perfect. If you send through a different date in subsequent calls, it will cause 1 day to be removed from the output date. As such, I have updated this below to give a better solution in case your date does change. The "initialDay" variable will now store the very first day of the week supplied during the run of the job.

 

public class DateIteration {

 

 

private static Date date;

private static int initialDay;

 

public static Date iterateDates(Date sourceDate, int minutesToAdd) {

 

if(date==null) {

 

date = sourceDate;

initialDay = routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate);

}

 

date = routines.TalendDate.addDate(date, 5, "mm");

 

 

if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){

date = routines.TalendDate.addDate(date,-1,"dd");

 

}

 

return date;

}

 

}

 

 

 

JABELLA1634831127
Contributor III
Contributor III
Author

Oh thank you so much, really appreciate all the help. It is much clearer now 🙂

JABELLA1634831127
Contributor III
Contributor III
Author

Hi rhall, I just did UAT for my job and the desired output for this row should be

static like what I mentioned previously except for the last timestamp so once it reaches 00:00 again the date should be date + 1. For ex.

Date

05 - 11 - 2021 00:05

05 - 11 - 2021 00:10

05 - 11 - 2021 00:15

...

05 - 11 - 2021 23:55

06 - 11- 2021 00:00

05 - 11 - 2021 00:05

 

Am currently trying to experiment with the routine but am struggling 😞 .

Is there a quick fix for this?