mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
Matlab
61 lines
1.7 KiB
Matlab
8 years ago
|
//
|
||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "OWSFormat.h"
|
||
|
|
||
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
|
@implementation OWSFormat
|
||
|
|
||
|
+ (NSString *)formatInt:(int)value
|
||
|
{
|
||
|
static NSNumberFormatter *formatter = nil;
|
||
|
static dispatch_once_t onceToken;
|
||
|
dispatch_once(&onceToken, ^{
|
||
|
formatter = [NSNumberFormatter new];
|
||
|
formatter.numberStyle = NSNumberFormatterNoStyle;
|
||
|
});
|
||
|
return [formatter stringFromNumber:@(value)];
|
||
|
}
|
||
|
|
||
|
+ (NSString *)formatFileSize:(unsigned long)fileSize
|
||
|
{
|
||
7 years ago
|
static NSNumberFormatter *formatter = nil;
|
||
|
static dispatch_once_t onceToken;
|
||
|
dispatch_once(&onceToken, ^{
|
||
|
formatter = [NSNumberFormatter new];
|
||
|
formatter.numberStyle = NSNumberFormatterDecimalStyle;
|
||
|
});
|
||
|
|
||
8 years ago
|
const unsigned long kOneKilobyte = 1024;
|
||
|
const unsigned long kOneMegabyte = kOneKilobyte * kOneKilobyte;
|
||
|
|
||
|
if (fileSize > kOneMegabyte * 10) {
|
||
7 years ago
|
return [[formatter stringFromNumber:@((int)round(fileSize / (CGFloat)kOneMegabyte))]
|
||
8 years ago
|
stringByAppendingString:@" MB"];
|
||
|
} else if (fileSize > kOneKilobyte * 10) {
|
||
7 years ago
|
return [[formatter stringFromNumber:@((int)round(fileSize / (CGFloat)kOneKilobyte))]
|
||
8 years ago
|
stringByAppendingString:@" KB"];
|
||
|
} else {
|
||
|
return [NSString stringWithFormat:@"%lu Bytes", fileSize];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (NSString *)formatDurationSeconds:(long)timeSeconds
|
||
|
{
|
||
|
long seconds = timeSeconds % 60;
|
||
|
long minutes = (timeSeconds / 60) % 60;
|
||
|
long hours = timeSeconds / 3600;
|
||
|
|
||
|
if (hours > 0) {
|
||
|
return [NSString stringWithFormat:@"%ld:%02ld:%02ld", hours, minutes, seconds];
|
||
|
} else {
|
||
|
return [NSString stringWithFormat:@"%ld:%02ld", minutes, seconds];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@end
|
||
|
|
||
|
NS_ASSUME_NONNULL_END
|