Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendl...

Human readable duration format solution

 Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of yearsdayshoursminutes and seconds.

It is much easier to understand with an example:

TimeFormatter.formatDuration(62)   //returns "1 minute and 2 seconds"
TimeFormatter.formatDuration(3662) //returns "1 hour, 1 minute and 2 seconds"

For the purpose of this Kata, a year is 365 days and a day is 24 hours.

Note that spaces are important.

Detailed rules

The resulting expression is made of components like 4 seconds1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.

The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.

A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.


Solution 1 :




 
public class TimeFormatter {
    
    public static String formatDuration(int sec) {
        
      int year=0, days=0, hour=0, minute=0, second=0;
    String ans = "";
    
    if(sec == 0) return "now";
    
    if(sec>=31536000) {
      year = sec/31536000;
      sec = sec%31536000;
      ans += (ans.equals("") ? "" : (sec == 0 ? " and " : ", "))
              +year+" year" +((year>1)?"s":"");
    } 
    if(sec>=86400) {
      days = sec/86400;
      sec = sec%86400;
      ans += (ans.equals("") ? "" : (sec == 0 ? " and " : ", "))
              +days+" day" +((days>1)?"s":"");
    }
    if(sec>=3600) {
      hour = sec/3600;
      sec = sec%3600;
      ans += (ans.equals("") ? "" : (sec == 0 ? " and " : ", "))
              +hour+" hour"+((hour>1)?"s":"");
    }
    if(sec>=60) {
      minute = sec/60;
      sec = sec%60;
      ans += (ans.equals("") ? "" : (sec == 0 ? " and " : ", "))
              +minute+" minute"+((minute>1)?"s":"");
    }
    if(sec>0) {
      second = sec;
      ans += (ans.equals("") ? "" : (sec>0 ? " and " : ""))
              +second+" second"+((second>1)?"s":"");
    }
    
    return ans;
    }
}

Solution 2 :



 public class TimeFormatter {
    
    public static String formatDuration(int sec) {
      

    int year=0, days=0, hour=0, minute=0, second=0, cnt=0;
    
    if(sec>=31536000) {
      year = sec/31536000;
      sec = sec%31536000;
      cnt++;
    } 
    if(sec>=86400) {
      days = sec/86400;
      sec = sec%86400;
      cnt++;
    }
    if(sec>=3600) {
      hour = sec/3600;
      sec = sec%3600;
      cnt++;
    }
    if(sec>=60) {
      minute = sec/60;
      sec = sec%60;
      cnt++;
    }
    if(sec>0) {
      second = sec;
      cnt++;
    }
    cnt--;
    String ans = "";
    
    if(year>0) {
      ans += year+" year";
      if(year>1)
        ans += "s";
      if(cnt>1) {
        ans +=", ";
        cnt--;
      }
      else if(cnt--==1)
        ans +=" and ";
    }
    if(days>0) {
      ans += days+" day";
      if(days>1)
        ans += "s";
      if(cnt>1) {
        ans +=", ";
        cnt--;
      }
      else if(cnt--==1)
        ans +=" and ";
    }
    if(hour>0) {
      ans += hour+" hour";
      if(hour>1)
        ans += "s";
      if(cnt>1) {
        ans +=", ";
        cnt--;
      }
      else if(cnt--==1)
        ans +=" and ";
    }
    if(minute>0) {
      ans += minute+" minute";
      if(minute>1)
        ans += "s";
      if(cnt>1) {
        ans +=", ";
        cnt--;
      }
      else if(cnt--==1)
        ans +=" and ";
    }
    if(second>0) {
      ans += second+" second";
      if(second>1)
        ans += "s";
    }
    
    if(ans.isEmpty()) {
      ans = "now";
    }
    
    return ans;
  
    }
}
}

0 comments:

Do not spam here.