Logbook Application adding hours and minutes

Discussion in 'Mac Programming' started by dg1234uk, Jul 19, 2009.

  1. dg1234uk macrumors newbie

    Jun 1, 2009
    Hi I have just started out with programming with cocoa and I am trying to create a simple flying logbook application with which you can record every flight you do and the duration of that flight so then it can total up and tell you how many flying hours you have!

    So far I have decided to use core data for this application (please tell me if there's a better way or if this is the way forward) and one of the attributes in the entity logbook is 'flight duration' I set this a date type but when i came to use it it obviously deals with time in the sense of hours, days, weeks etc. not how I want to use it! For example I will have a flight of 1:30 then next of 0:30 then of 2:15 etc. which I want to total to give me 4:15 and obviously once I add enough flights to give me over 24 hours it would tell me my time in 1 day and x hours etc. I want it to tell me I have 214:15 etc.

    I hope that long winded explanation makes some sense

  2. petron macrumors member

    May 22, 2009
    Malmo, Sweden
    It does not seems that you have got any reply yet, so I can at least write some words. It does need to be true but it is what I have experienced as a newbe...

    Core Data is fine and very fast way to create data base applications but it has some of the limitations. One of those you discovered already. The way to solve the problem you have would be to create application manually with the NSArrayController and use NSMutableArray.
    In this way you could manually and fully control the presentation of flight duration.

    Good luck

  3. lucasgladding macrumors 6502

    Feb 16, 2007
    Waterloo, Ontario

    I would recommend sticking with Core Data.

    Personally I would set up attributes for start and stop times as Date, then create a transient duration attribute as Float. Generate a custom model class for the log Entity and use the timeIntervalSinceDate: method inside the duration attribute to obtain the length of time between the start and stop.

    For calculating the date components from the time interval, you could use something like the following:

    long longValue = (long)value; 
    long seconds = longValue%60;
    long minutes = (longValue/60)%60;
    long hours = (longValue/60/60)%24;
    long days = (longValue/60/60/24)%7;
    long weeks = longValue/60/60/24/7;	
    Best of luck

  4. dg1234uk thread starter macrumors newbie

    Jun 1, 2009
    Excellent thanks all for the replies, very interesting!

    Unfortunately only the duration of the flight is logged in my logbook and not the start stop times! So it looks like I will have to make just use a float to store a decimal number and convert the users hours and minutes time into hour/decimal time then recalculate it back to hours and minutes for the display.

    Therefore how would I format either/both the tableview cell and/or text box so that when they type it in it conforms to the hours and minutes and then convert to decimal time before saving it into the core data because at the moment my tableview and textbox are both bound to the coredata so its automatic!

    Thanks, I will keep working on it!
  5. JoshDC macrumors regular

    Apr 8, 2009
    I think the easiest way will be with an NSTransformer subclass (time to float) and setting the duration's type to Transformable with that subclass. NSFormatter may also for displaying and inputting.
  6. dg1234uk thread starter macrumors newbie

    Jun 1, 2009
  7. lucasgladding macrumors 6502

    Feb 16, 2007
    Waterloo, Ontario
    NSValueTransformer should work, though an NSFormatter subclass might be more appropriate since the conversion is being done for presentation. The difference between the classes isn't very clear IMHO.

    If you use NSValueTransformer, don't forget to register it when launching the application. I think that caught me the first time I used the class. You can register the transformer with:

    + (void)setValueTransformer:(NSValueTransformer *)transformer forName:(NSString *)name
    You may also want to check the following thread and the corresponding source code for more information:
  8. dg1234uk thread starter macrumors newbie

    Jun 1, 2009
    Simple solution

    I have just come back to this problem after some time, for those who are interested this is how I have come to solve it in a simple command line tool (if anyone has any ideas on how to improve on this I would be very happy to learn!)

    #import <Foundation/Foundation.h>
    NSDateComponents *totalTime;
    void timeByAddingComponents(NSDateComponents *firstTime, NSDateComponents *secondTime) {
    	int addMin = [firstTime minute] + [secondTime minute];
    	int addHour = floor(addMin/60);
    	addMin = addMin % 60;
    	addHour = addHour + [firstTime hour] + [secondTime hour];
    	[totalTime setMinute:addMin];
    	[totalTime setHour:addHour];
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	totalTime = [[NSDateComponents alloc] init];
    	[totalTime setHour:0];
    	[totalTime setMinute:0];
    	NSDateComponents *addTime = [[NSDateComponents alloc] init];
    	while (TRUE) {
    		int x, y;
    		printf("Please Enter Number of Hours: ");
    		scanf("%d", &x);
    		printf("Please Enter Number of Minutes: ");
    		scanf("%d", &y);
    		if (x == 0 && y == 0)
    		NSLog(@"Add %i:%i to %i:%i", x,y, [totalTime hour], [totalTime minute]);
    		[addTime setHour:x];
    		[addTime setMinute:y];
    		timeByAddingComponents(totalTime, addTime);
    		NSString *time = [NSString stringWithFormat:@"%.2d:%.2d",[totalTime hour],[totalTime minute]];
    		NSLog(@"Total time now: %@", time);
    	[totalTime release];
    	[addTime release];
        [pool drain];
        return 0;

Share This Page