Raw Seconds to 00:00:00 Format

Discussion in 'iOS Development' started by Axis, Jul 9, 2009.

  1. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I have an NSTimer that spits out a float value every 1/16th of a second. (Every 1/16th of a second, 1/16 of a second is taken off the float value).

    The application functions as a countdown timer, so I want the raw fractional seconds (i.e. 9.9375) to be converted into (hours:minutes:seconds:000).

    This is somewhat hard to explain, and I apologize, but right now, I have an NSString that looks like 9.xxxx; my goal is for it to look like 00:00:9:xx:xx.

    If this is possible, I would appreciate some guidance.

    Please Register or Log in to view images



    Thanks.
  2. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Can you use printf's format string.
    Code:
    
    int main(int argc, char** argv) {
        char buf[80];
        char formatted[8];
        float f = 9.9375;
    
        sprintf(buf, "%07.4f", f);
        sprintf(formatted, "%c%c:%c%c:%c%c", buf[0], buf[1], buf[3], buf[4], buf[5], buf[6]);
        printf("%s\n", formatted);
        return 0;
    }
    
    
  3. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Thanks, but that gives me this
    as the output. While that does take care of the formatting, the number I had originally was 9.9375 seconds. Also, the output that I just posted shows 93 minutes, 75 seconds, which I need to show up as 1 hour, 34 minutes, 15 seconds, (01:34:15).
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Zomg! It looks like you're going to do some math.
  5. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    What about an NSTimer that fires up every second and then use NSDate's timeIntervalSinceDate: to get the time interval between the starting time and the actual one?... Just because it's easier to get that string.

    Well I think that won't help, did I overlook sth.? Seems so...
    If I'm missing something then I would go and follow Skylar's idea: Calculate it, dividing by some 60's isn't that hard.

    Please Register or Log in to view images

  6. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    It's OK, thank you, but I've got it working, thanks to some help from a certain member at a developer's forum.

    __

    1. float 'seconds' contains the "raw" seconds value

    2. convert float 'seconds' into an integer (effectively rounding down to the nearest whole number)

    3. subtract that integer from the float 'seconds' yields the fractional seconds

    4. multiply the fractional seconds by 100, creating a float value 'hundredths'

    5. convert the float 'hundredths' into an integer like 'seconds' earlier.

    6. build string with the integer seconds and integer hundredths
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Or, just learn how to use the modulus operator and all your problems will be solved.

    Code:
    	long time = /* whatever you do to get seconds */ ;	
    	int seconds = ((time) % 60);
    	int minutes = ((time/60) % 60);
    	int hours = ((time/3600) % 24);
    Of course, that code is dealing entirely with seconds. You can do this many other ways to, but this is how I prefer to look at things. It is what I do in PocketTouch (for the most part, I work with milliseconds in there not seconds, but that is irrelevant).


    EDIT: As I stated earlier, the answer to your question is purely mathematics. Display the result with a simple NSString stringWithFormat: message.

Share This Page