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;
}