Time Conversion

  • + 0 comments

    I touch memory,

    Rust

    use std::env;
    use std::fs::File;
    use std::io::{self, BufRead, Write};
    
    /*
     * Complete the 'timeConversion' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    fn timeConversion(s: &str) -> String {
        let chars :Vec<char> = s.chars().collect();
        let am_pm = chars[8] == 'A'; 
        let mid = chars[0] == '1' && chars[1] == '2';
        return match am_pm {
            true => {
                
                if mid {
                    String::from({
                        format!(
                            "00{}",
                            chars.iter().skip(2).take(6).collect::<String>()
                        )
                    })
                } else {
                    String::from({
                        chars.iter().take(8).collect::<String>()
                    })
                }  
            },
            false => {
                if mid {
                    String::from({
                        chars.iter().take(8).collect::<String>()
                    })
                } else {
                    let first_two = chars[0].to_digit(10).unwrap() * 10 + chars[1].to_digit(10).unwrap() + 12;
                    if first_two == 24 {
                        first_two == 0;
                    }
                    String::from({
                        format!(
                            "{:0>2}{}",
                            first_two,
                            chars.iter().skip(2).take(6).collect::<String>()
                        )
                    })
                }
            }
        }
    }
    
    fn main() {
        let stdin = io::stdin();
        let mut stdin_iterator = stdin.lock().lines();
    
        let mut fptr = File::create(env::var("OUTPUT_PATH").unwrap()).unwrap();
    
        let s = stdin_iterator.next().unwrap().unwrap();
    
        let result = timeConversion(&s);
    
        writeln!(&mut fptr, "{}", result).ok();
    }