diff options
author | Damiano Galassi <[email protected]> | 2019-03-06 10:14:02 +0100 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2019-03-06 10:14:02 +0100 |
commit | 1ca62a0b52244e988f5505dd3cb9cf509cc3552a (patch) | |
tree | bf039cba47e6863373fd7ceab16b38ced9bea745 /macosx/HBRange+UIAdditions.m | |
parent | 8d78f7d001b06872b1c31620c6387da239ae51eb (diff) |
MacGui: show the start/end seconds range as formatted time strings.
Diffstat (limited to 'macosx/HBRange+UIAdditions.m')
-rw-r--r-- | macosx/HBRange+UIAdditions.m | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/macosx/HBRange+UIAdditions.m b/macosx/HBRange+UIAdditions.m index f574225a3..559d5e26e 100644 --- a/macosx/HBRange+UIAdditions.m +++ b/macosx/HBRange+UIAdditions.m @@ -86,3 +86,52 @@ } @end + +@implementation HBTimeInSecondsTransformer + ++ (Class)transformedValueClass +{ + return [NSString class]; +} + +- (id)transformedValue:(id)value +{ + uint64_t duration = [value integerValue]; + uint64_t hours = duration / 3600; + uint64_t minutes = (duration % 3600) / 60; + uint64_t seconds = duration % 60; + + NSString *result = [NSString stringWithFormat:@"%02llu:%02llu:%02llu", hours, minutes, seconds]; + return result; +} + ++ (BOOL)allowsReverseTransformation +{ + return YES; +} + +- (id)reverseTransformedValue:(id)value +{ + const char *time = [value UTF8String]; + if (time) + { + unsigned hour, minute, second, timeval; + + if (sscanf(time, "%2u:%u:%u", &hour, &minute, &second) < 3) { + if (sscanf(time, "%u:%u:%u", &hour, &minute, &second) < 3) { + return 0; + } + } + + if (second > 60) { + second = 0; + } + + timeval = hour * 60 * 60 + minute * 60 + second; + + return @(timeval); + } + return @"00:00:00"; +} + +@end |