link mingle home | logged in as: guest | login/register| submit link


IndiaDiscuss.com : Social Bookmarkings and News Networking Site for India
interview_questions
Bookmarks
Adding Time
-12
Votes

Given time in format "[HH:MM {AM|PM}" and number of minutes. Return the time after adding the number of minutes in the same time format . For example with time as "10:23 AM" and period as 13 your program should return "10:36 AM"

saved under Microsoft Interview Questions by interview_questions

 
1. First convert time into 
   minutes from Midnight.
2. Add time
3. Convert this back

Converting to minutes from midnight

1. If XX:YY then time = XX*60+YY
2. If PM add t=t+12*60
3. return t

comment by bollywood_news on 2008-06-28 20:21:22
Here is a solution. char * time = "12:45 AM"; char t; int extH, h, m, extTime = 10; sscanf(time, "%d:%d %c", &h, &m, &t); extH = (m + extTime) / 60; m = (m + extTime) % 60; if(h < 12 && (h + extH) >= 12) { t = (t == 'A')?'P':'A'; } h = ((h + extH) > 12)? (h + extH) % 12 : (h + extH); printf("%d:%02d %cM\n", h, m, t);
comment by Aam on 2008-07-10 08:12:58
In the above program, one condition is missing. say given minutes is 2000 mintues, then number of hours will be more than 30. In such case, we need to determine AM/PM correctly. temp = extH / 12; swap = (temp % 2)? true : false; if(swap) then swap AM to PM or PM to AM
comment by sachidanand on 2009-09-30 10:03:28
 



Enter the string above
 
IndiaDiscuss | Published News | Hot
Indian Social News and Links Network
IndiaDiscuss | Published News
Indian Social News and Links Network