Problem of the Day
A new programming or logic puzzle every Mon-Fri

Time Conversion

Your goal is to write a function that converts the time something is scheduled to happen in to a unix time stamp. The format you receive is shown below:

  • 1d 6h
  • 10h 14m
  • 7m 8s
  • 32s

Thus, if your input was "7m 8s" you'd add 7 minutes and 8 seconds to the current unix time stamp and return that value.

Permalink: http://problemotd.com/problem/time-conversion/

Comments:

  • Keemo - 10 years, 5 months ago

    import time
    
    def parseTime(tString):
        if tString[-1] is 'h':
            return int(tString[:-1]) * 60 * 60
        elif tString[-1] is 'm':
            return int(tString[:-1]) * 60
        elif tString[-1] is 's':
            return int(tString[:-1])
        return 0
    
    ti = int(round(time.time()))
    
    usertime = raw_input()
    splits = usertime.split(' ')
    for item in splits:
        ti = ti + parseTime(item)
    print 'result: ' + str(ti)
    

    reply permalink

  • Max Burstein - 10 years, 5 months ago

    That was quick. Nice job!

    reply permalink

  • Nick Krichevsky - 10 years, 5 months ago

    public class Main {
        public static void main(String[] args){
            String s = "1d 23m";
            String[] sections = s.split(" ");
            long time = System.currentTimeMillis();
            for (String section : sections){
                char unit = section.charAt(section.length()-1);
                int num = Integer.valueOf(section.substring(0, section.length()-1));
                int millis = 0;
                if (unit=='d')
                    millis= num*24*60*60*1000;
                else if (unit=='h')
                    millis = num*60*60*1000;
                else if (unit=='m')
                    millis = num*60*1000;
                else if (unit=='s')
                    millis = num*1000;
                else{
                    System.out.println("Invalid input!");
                    System.exit(0);
                }
                time+=millis;
            }
            System.out.println(time);
        }
    }
    

    reply permalink

Content curated by @MaxBurstein