0

I want to make an app which is able to calculate the time at work (and other things like flexitime).

Getting a String of a flexitime (which is a NSTimeInterval) by a specific format is really easy and I have written a method for this with the possibility of adding a specific format:

+ (NSString *)stringForTimeInterval: (NSTimeInterval) interval withFormat: (NSString *)format

This method returns @"25:00" for a flexitime of 90000.0 and @"-25:00" for -90000.0. format for both examples is @"HH:mm".

The format is a string which looks like @"HH:mm". "HH" are hours (could be any positive or negative integer but will normally be between bounds of -300 and 300) and "mm" are minutes (0 - 59, digits 0 - 9 get a leading zero).

Now I want to write a method to get back a NSTimeInterval of a string formatted with a known format.

+ (NSTimeInterval)timeIntervalForString: (NSString *)timeString withFormat: (NSString *)format

I really do not know how to do this. I can not use a normal NSDateFormatter because a flexitime could be more than 23:59 and less than 00:00. There also has to be a TimeFormat because I want to give the users the possibility of easily switching their format. I also want to have the possibility of adding a new timeFormat in a few seconds (actually I just have to add a new NSString to an NSArray to add a new format in the whole app).

I also tried regex but I found no way how to solve it with.

Does anybody know how I could solve this?


Edit:
This is my method for getting a string of hours and minutes with a specific format:

+ (NSString *)stringForTimeInterval: (NSTimeInterval) interval withFormat: (NSString *)format
{    
    // minutes are never negative!
    int minutes = abs((int)interval / 60 % 60);

    int hours = (int)interval / 3600;

    // replacing 'HH' and 'mm'
    NSString *time = [[format stringByReplacingOccurrencesOfString:@"HH" withString:[NSString stringWithFormat:@"%0.2d", hours]] stringByReplacingOccurrencesOfString:@"mm" withString:[NSString stringWithFormat:@"%0.2d", minutes]];

    return time;
}
  • Here something you could do, with timeString something like "-25:37" (according to what you use). `NSArray *array = [timeString componentsSeparatedByString:@":"]; int hours = [[array objectAtIndex:0] intValue]; int minutes = [[array objectAtIndex:1] intValue]; NSTimeInterval timeInterval = hours*3600+minutes*60;` But I suggest you need to determine what's really "format". – Larme Apr 15 '14 at 10:39
  • Thx for your comment. `format` is a string showing the format of flexitime. It usually is `"HH:mm"` to get times like "23:45". But it also could be `"HH.mm"` to create strings like "23.45". The rule is that `HH` is replaced by hours and `mm` by minutes. I could format the time also like "Your flexitime is HH hours and mm minutes." to get "Your flexitime is 23 hours and 45 minutes.". To hold this freedom I need a way how to get back a flexitime when the formatted string and the format are available. Before I decided to use a format I used your method but it does not work with format. – strohkoenig Apr 15 '14 at 11:05
  • You really need to set the limits of your possibles formats. If there always be hours first, then minutes, you could use an NSRegularExpression. – Larme Apr 15 '14 at 12:00
  • Could you show the code of `stringForTimeInterval:withFormat:`? The way you managed "format" could be a clue to how reverse it. – Larme Apr 16 '14 at 10:55
  • I made an edit. It is quite a simple solution but it fits for my app.. – strohkoenig Apr 16 '14 at 11:15
  • You know, that if you use for format: "You worked as a commercial at Wal-Mart HH:mm", you should get a weird response?" So you really should use something else that "HH" or "mm", maybe [HH] and [mm], or things like that. But still, reversing would be quite difficult. And, if you have a sentence like this: "I work in 2009 at Wal-Mart 46 hours and 23mn.", what numbers would you read? So don't be that much flexible. – Larme Apr 16 '14 at 15:10

0 Answers0