Time Conversion

Sort by

recency

|

5155 Discussions

|

  • + 0 comments
    char* timeConversion(char* str) {
        int h,m,s;
        sscanf(str, "%d:%d:%d", &h, &m, &s);
        if (h == 12) h = 0;
        if (str[8] == 'P') h += 12;
        
        static char out[9];
        sprintf(out, "%02d:%02d:%02d", h, m, s);
        return out;
    }
    
  • + 0 comments

    Is this code good??

    def timeConversion(s):
        hour = int(s[:2])
        minute = s[3:5]
        second = s[6:8]
        am_pm = s[8:]
    
        if am_pm == "AM":
            if hour == 12:
                print(f"0{hour - 12}:{minute}:{second}")
            else:
                print(f"0{hour}:{minute}:{second}")
    
        elif am_pm == "PM":
            if hour == 12:
                print(f"{hour}:{minute}:{second}")
            else:
                print(f"{hour + 12}:{minute}:{second}")
    
  • + 0 comments

    Any tips for coming up with simpler solutions? I keep coming up with complicated solutions. my solution to this problem was:

            String amOrPm = parts[parts.length - 2];
            if(amOrPm.equals("A")){
                if(parts[0].equals("1") && parts[1].equals("2")) return s.replaceFirst("12", "00").substring(0, parts.length - 2);
                 else return s.substring(0, parts.length - 2);
                
            } else if(amOrPm.equals("P")) {
                if(parts[0].equals("1") && parts[1].equals("2")) return s.substring(0, parts.length - 2);
                else return s.replaceFirst((s.substring(0,2)), (Integer.parseInt(s.substring(0,2)) + 12) + "").substring(0, parts.length - 2);
            }
            
            return s;
    
  • + 0 comments
    string timeConversion(string s)
    {
        if(s[8] == 'A')
        {
            if(s[0] == '1' && s[1] == '2')
            {
                s[0] = '0';
                s[1] = '0';
            }
        }
        else if(s[8] == 'P')
        {
            if(s[0] != '1' || s[1] != '2')
            {
                s[0] = ((((((s[0] - '0') * 10) + (s[1] - '0')) + 12) / 10) + '0');
                s[1] = ((((((s[0] - '0') * 10) + (s[1] - '0')) + 12) % 10) + '0');
            }
        }
        s.erase(8, 2);
        return s;
    }
    
  • + 0 comments

    Yale door lock ensures advanced security with modern technology and trusted durability, making it a reliable choice for homes and businesses. Designed with convenience in mind, it offers easy installation and smooth operation, giving you peace of mind daily. Its sleek design complements any door style while enhancing safety. Just like accurate time conversion simplifies planning across regions, a Yale door lock simplifies security with dependable performance and long-lasting protection.