Transform a time into seconds

One Star

Transform a time into seconds

Hi!
I am trying in the tMap to transform a time that I get out from a data source in string like this "12:13:55" to seconds (43200 + 720 + 55 = 43975) in this case.
How can I do that with the expressions given to me in the tMap Expressions?
Thanks
Joakim
One Star

Re: Transform a time into seconds

Hi,
Create a routine :-
public static String CalculateTotalSeconds(String timein) {
String[] date_string = timein.split(":");
int hours = Integer.parseInt(date_string);
int mins = Integer.parseInt(date_string);
int seconds = Integer.parseInt(date_string);

int totalsec = (hours * 3600)+ (mins* 60) + seconds;

return "" + totalsec;
}
Call from tmap :-
Date_Converter.CalculateTotalSeconds(variable);
Returns a string with total seconds.
HTH
One Star

Re: Transform a time into seconds

tnewman: Thanks for the example. I just don´t know how to create a routine, I am in the Navigator mode of the Repository but I dont understand how to create it.
// Joakim
One Star

Re: Transform a time into seconds

I dont have the "code" section in my Repository that is shown on examples I have seen... How do I get that visible?
One Star

Re: Transform a time into seconds

Hi,
I am using TIS and I see the Code branch in the standard view. I have looked around the various menus and cannot see anywhere we you can include/exclude from the repo view.
I am not sure if TOS is different.
Sorry I can't be much more help - maybe someone else can answer.
Highlighted
One Star

Re: Transform a time into seconds

It's in TOS too, at least for java projects.
Seven Stars

Re: Transform a time into seconds

Got an error when creating routine 

 

Capture.PNG

 

 

Thirteen Stars

Re: Transform a time into seconds


@Karuetl wrote:

Got an error when creating routine 

 


you just need chose array element for parse

0 - hours

1 - minutes

2 - seconds

	public static String CalculateTotalSeconds(String timein) {
		String[] date_string = timein.split(":");
		int hours = Integer.parseInt(date_string[0]);
		int mins = Integer.parseInt(date_string[1]);
		int seconds = Integer.parseInt(date_string[2]);

		int totalsec = (hours * 3600)+ (mins* 60) + seconds;

		return "" + totalsec;
		} 

 

 

P.S.

keep in mind - original routine return String, if you need have a long or integer - you need modify it or convert into integer in tMap/tJava

-----------
Seven Stars

Re: Transform a time into seconds

Thanks 

Thirteen Stars

Re: Transform a time into seconds

thanks - it is too much  ...  kudu is enough ;-)

 

welcome to community

-----------

Cloud Free Trial

Try Talend Cloud free for 30 days.

Tutorial

Introduction to Talend Open Studio for Data Integration.

Definitive Guide to Data Integration

Practical steps to developing your data integration strategy.

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.