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.
One Star

Re: Transform a time into seconds

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

Re: Transform a time into seconds

Got an error when creating routine 

 

Capture.PNG

 

 

Forteen 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

-----------
Eight Stars

Re: Transform a time into seconds

Thanks 

Forteen Stars

Re: Transform a time into seconds

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

 

welcome to community

-----------

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Downloads and Trials

Test drive Talend's enterprise products.

Downloads