VLCTime.m 4.68 KB
Newer Older
1
/*****************************************************************************
2
 * VLCTime.m: VLCKit.framework VLCTime implementation
3 4
 *****************************************************************************
 * Copyright (C) 2007 Pierre d'Herbemont
5
 * Copyright (C) 2007 VLC authors and VideoLAN
6 7 8 9
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

Pierre's avatar
Pierre committed
25
#import "VLCTime.h"
26

27
@implementation VLCTime
28

29
/* Factories */
30 31
+ (VLCTime *)nullTime
{
32
    static VLCTime * nullTime = nil;
33 34
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
35
        nullTime = [VLCTime timeWithNumber:nil];
36
    });
37 38 39 40 41
    return nullTime;
}

+ (VLCTime *)timeWithNumber:(NSNumber *)aNumber
{
42
    return [[VLCTime alloc] initWithNumber:aNumber];
43 44
}

45
+ (VLCTime *)timeWithInt:(int)aInt
46
{
47
    return [[VLCTime alloc] initWithInt:aInt];
48 49
}

50
/* Initializers */
51
- (instancetype)initWithNumber:(NSNumber *)aNumber
52
{
53
    if (self = [super init]) {
54
        _value = aNumber;
55 56 57 58
    }
    return self;
}

59
- (instancetype)initWithInt:(int)aInt
60
{
61
    if (self = [super init]) {
62
        if (aInt)
63
            _value = @(aInt);
64 65 66 67
    }
    return self;
}

68 69 70 71 72 73
/* NSObject Overrides */
- (NSString *)description
{
    return self.stringValue;
}

74
- (NSNumber *)numberValue
75
{
76
    return _value;
77 78
}

79
- (NSString *)stringValue
80
{
81
    if (_value) {
82 83 84 85 86 87
        long long duration = [_value longLongValue];
        if (duration == INT_MAX || duration == INT_MIN) {
            // Return a string that represents an undefined time.
            return @"--:--";
        }
        duration = duration / 1000;
88
        long long positiveDuration = llabs(duration);
89
        if (positiveDuration > 3600)
90
            return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
Pierre's avatar
Pierre committed
91
                        duration < 0 ? "-" : "",
92 93 94
                (long) (positiveDuration / 3600),
                (long)((positiveDuration / 60) % 60),
                (long) (positiveDuration % 60)];
95
        else
96
            return [NSString stringWithFormat:@"%s%02ld:%02ld",
Pierre's avatar
Pierre committed
97
                            duration < 0 ? "-" : "",
98 99
                    (long)((positiveDuration / 60) % 60),
                    (long) (positiveDuration % 60)];
100
    } else {
101
        // Return a string that represents an undefined time.
102
        return @"--:--";
103 104
    }
}
105

106 107
- (NSString *)verboseStringValue
{
108 109 110 111 112 113 114 115 116
    if (!_value)
        return @"";

    long long duration = [_value longLongValue] / 1000;
    long long positiveDuration = llabs(duration);
    long hours = (long)(positiveDuration / 3600);
    long mins = (long)((positiveDuration / 60) % 60);
    long seconds = (long)(positiveDuration % 60);
    BOOL remaining = duration < 0;
117 118 119 120 121 122 123 124 125
   
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setHour:hours];
    [components setMinute:mins];
    [components setSecond:seconds];

    NSString *verboseString = [NSDateComponentsFormatter localizedStringFromDateComponents:components unitsStyle:NSDateComponentsFormatterUnitsStyleFull];
    verboseString = remaining ? [NSString stringWithFormat:@"%@ remaining", verboseString] : verboseString;
    return [verboseString stringByReplacingOccurrencesOfString:@"," withString:@""];
126 127
}

128 129
- (NSString *)minuteStringValue
{
130 131 132
    if (_value) {
        long long positiveDuration = llabs([_value longLongValue]);
        long minutes = (long)(positiveDuration / 60000);
133
        return [NSString stringWithFormat:@"%ld", minutes];
134 135 136 137
    }
    return @"";
}

138 139
- (int)intValue
{
140
    return [_value intValue];
141 142
}

143 144
- (NSComparisonResult)compare:(VLCTime *)aTime
{
145 146 147 148 149 150
    NSInteger a = [_value integerValue];
    NSInteger b = [aTime.value integerValue];

    return (a > b) ? NSOrderedDescending :
        (a < b) ? NSOrderedAscending :
            NSOrderedSame;
151
}
152

153 154 155 156 157 158 159 160 161 162 163 164 165
- (BOOL)isEqual:(id)object
{
    if (![object isKindOfClass:[VLCTime class]])
        return NO;

    return [[self description] isEqual:[object description]];
}

- (NSUInteger)hash
{
    return [[self description] hash];
}

166
@end