Replace custom time formatting code in TimeField with SecondsFormatter.
This commit is contained in:
parent
aa02f8c27c
commit
ce88971789
1 changed files with 31 additions and 49 deletions
|
@ -8,61 +8,43 @@
|
|||
|
||||
#import "TimeField.h"
|
||||
|
||||
#import "SecondsFormatter.h"
|
||||
|
||||
|
||||
static NSString *kTimerModeKey = @"timerShowTimeRemaining";
|
||||
|
||||
NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) {
|
||||
const int64_t signed_total_seconds = (int64_t)timeInterval;
|
||||
const bool need_minus_sign = signbit(timeInterval);
|
||||
const int64_t total_seconds = (need_minus_sign ? -1 : 1) * signed_total_seconds;
|
||||
const int64_t seconds = total_seconds % 60;
|
||||
const int64_t total_minutes = (total_seconds - seconds) / 60;
|
||||
const int64_t minutes = total_minutes % 60;
|
||||
const int64_t total_hours = (total_minutes - minutes) / 60;
|
||||
const int64_t hours = total_hours % 24;
|
||||
const int64_t days = (total_hours - hours) / 24;
|
||||
|
||||
NSString *timeString = nil;
|
||||
|
||||
if (days > 0) {
|
||||
timeString =
|
||||
[NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64,
|
||||
need_minus_sign ? "-" : "",
|
||||
days,
|
||||
hours,
|
||||
minutes,
|
||||
seconds];
|
||||
}
|
||||
else if (hours > 0) {
|
||||
timeString =
|
||||
[NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64,
|
||||
need_minus_sign ? "-" : "",
|
||||
hours,
|
||||
minutes,
|
||||
seconds];
|
||||
}
|
||||
else if (minutes > 0) {
|
||||
timeString =
|
||||
[NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64,
|
||||
need_minus_sign ? "-" : "",
|
||||
minutes,
|
||||
seconds];
|
||||
}
|
||||
else {
|
||||
timeString =
|
||||
[NSString localizedStringWithFormat:@"%s" "0" ":" "%02" PRIi64,
|
||||
need_minus_sign ? "-" : "",
|
||||
seconds];
|
||||
}
|
||||
|
||||
return timeString;
|
||||
}
|
||||
|
||||
|
||||
@implementation TimeField {
|
||||
BOOL showTimeRemaining;
|
||||
NSDictionary *fontAttributes;
|
||||
SecondsFormatter *secondsFormatter;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)aDecoder
|
||||
{
|
||||
if (self = [super initWithCoder:aDecoder])
|
||||
{
|
||||
[self commonInit];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
if (self = [super initWithFrame:frame])
|
||||
{
|
||||
[self commonInit];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)commonInit
|
||||
{
|
||||
secondsFormatter = [[SecondsFormatter alloc] init];
|
||||
}
|
||||
|
||||
- (void)awakeFromNib
|
||||
{
|
||||
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
|
||||
|
@ -74,7 +56,7 @@ NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) {
|
|||
if (showTimeRemaining == NO)
|
||||
{
|
||||
NSTimeInterval sec = self.currentTime;
|
||||
text = timeStringForTimeInterval(sec);
|
||||
text = [secondsFormatter stringForTimeInterval:sec];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -82,7 +64,7 @@ NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) {
|
|||
// NOTE: The floating point standard has support for negative zero.
|
||||
// We use that to enforce the sign prefix.
|
||||
if (sec == 0.0) { sec = -0.0; }
|
||||
text = timeStringForTimeInterval(sec);
|
||||
text = [secondsFormatter stringForTimeInterval:sec];
|
||||
}
|
||||
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
|
||||
attributes:fontAttributes];
|
||||
|
|
Loading…
Reference in a new issue