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

Timer

Welcome back to another awesome Monday here on PotD!

We'll start off with one that will prove super useful for anyone doing hourly, billable work. Create a program that starts a timer as soon as it starts up. Right before the program exits print how many hours, minutes, and seconds the program was running for.

Permalink: http://problemotd.com/problem/timer/

Comments:

  • Nick Krichevsky - 10 years, 4 months ago

    from time import time,sleep
    
    startTime = time()
    print "Press Ctrl-C to exit."
    try:
        while True:
            sleep(1)
    except KeyboardInterrupt:
        difference = time()-startTime
        hours = int(difference/3600)
        print difference
        difference-=3600*hours
        mins = int(difference/60)
        print difference
        difference -= mins*60
        secs = int(difference)
        print difference
        print "This has been running for:",hours,"Hours,",mins,"Mins, and",secs,"Seconds"
    

    reply permalink

  • Johnathan - 10 years, 4 months ago

    I just found your site today, so this is my first shot at your daily program. Love your riddles. Wrote this one in python.

        import time
    
        stuff = raw_input('Would you like to start the timer? Press either s to start or q to quit: ')
        print ' '
    
        while stuff != 's' and stuff != 'q':
            stuff = raw_input('Please only enter either s to start or q to quit: ')
            print ' '
    
        if stuff == 's':
            start = time.clock()
    
            while stuff != 'q':
                stuff = raw_input('Press p to prinout the time or press q to quit: ')
                current = time.clock() - start
    
                minutes, seconds = divmod(current, 60)
    
                hours, minutes = divmod(minutes, 60)
    
                print int(round(hours, 0)),':',int(round(minutes, 0)),':',round(seconds, 2)
    

    reply permalink

  • Nick - 10 years, 4 months ago

    Java:

    package com.tribool;
    
    import java.io.IOException;
    import java.util.Date;
    
    public class Main {
        public static void main(String[] args) {
            Date startDate = new Date();
    
            System.out.println("Press enter to finish.");
    
            try {
                System.in.read();
            }
            catch (IOException e) {
                System.out.println("Caught IOException: " + e.getMessage());
            }
    
            long millisecondsElapsed = new Date().getTime() - startDate.getTime();
    
            double secondsElapsed = millisecondsElapsed / 1000.0;
            double minutesElapsed = secondsElapsed / 60.0;
            double hoursElapsed = minutesElapsed / 60.0;
    
            System.out.println("Seconds elapsed: " + secondsElapsed);
            System.out.println("Minutes elapsed: " + minutesElapsed);
            System.out.println("Hours elapsed: " + hoursElapsed);
        }
    }
    

    reply permalink

Content curated by @MaxBurstein