Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: The Cloudera Community will undergo maintenance on Saturday, August 17 at 12:00am PDT. See more info here.

Divide HH:MM by 2

Highlighted

Divide HH:MM by 2

New Contributor

I have some string value with time format ie 20:30(hh:mm) and I want to divide it by 2 and expected result must be 10:15.

 

Is there any hive function to achive this?

 

Examples 

Input:

13:10

1:30

Expected output

6:35

0:45

 

1 REPLY 1

Re: Divide HH:MM by 2

Guru
Hi @babaravi,

This should be done via custom UDF. The logic can be something like below:

1. convert the time to minutes since mid night

13*60+10 = 790
1*60+30 = 90

2. divide the value by 2:

790 / 2 = 395
90 / 2 = 45

3. convert the number of minutes back to time format:

395 minutes passed mid-night is
"(395 / 60)" + ":" + "(395 % 60)" = 06:35

45 minutes passed mid-night is
"(45 / 60)" + ":" + "(45 % 60)" = 0:45

I am not sure if Java has any built-in functions to convert number of minutes to time, but it should be easy to do so yourself.

I might missed some edge cases, but you can build test cases to cover them.

Cheers
Eric