1 /*****************************************************************************
2 * VLCTime.m: VLCKit.framework VLCTime implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 @implementation VLCTime
31 static VLCTime * nullTime = nil;
33 nullTime = [[VLCTime timeWithNumber:nil] retain];
37 + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
39 return [[[VLCTime alloc] initWithNumber:aNumber] autorelease];
42 + (VLCTime *)timeWithInt:(NSInteger)aInt
44 return [[[VLCTime alloc] initWithInt:aInt] autorelease];
48 - (id)initWithNumber:(NSNumber *)aNumber
50 if (self = [super init])
53 value = [aNumber copy];
60 - (id)initWithInt:(int)aInt
62 if (self = [super init])
65 value = [[NSNumber numberWithInt: aInt] retain];
78 - (id)copyWithZone:(NSZone *)zone
80 return [[VLCTime alloc] initWithNumber:value];
83 /* NSObject Overrides */
84 - (NSString *)description
86 return self.stringValue;
90 - (NSNumber *)numberValue
92 return value ? [[value copy] autorelease] : nil;
95 - (NSString *)stringValue
99 long long duration = [value longLongValue] / 1000;
100 long long positiveDuration = llabs(duration);
101 if( positiveDuration > 3600 )
102 return [NSString stringWithFormat:@"%s%01d:%02d:%02d",
103 duration < 0 ? "-" : "",
104 (long) (positiveDuration / 3600),
105 (long)((positiveDuration / 60) % 60),
106 (long) (positiveDuration % 60)];
108 return [NSString stringWithFormat:@"%s%02d:%02d",
109 duration < 0 ? "-" : "",
110 (long)((positiveDuration / 60) % 60),
111 (long) (positiveDuration % 60)];
115 // Return a string that represents an undefined time.
120 - (NSString *)verboseStringValue
124 long long duration = [value longLongValue] / 1000;
125 long long positiveDuration = llabs(duration);
126 long hours = positiveDuration / 3600;
127 long mins = (positiveDuration / 60) % 60;
128 long seconds = positiveDuration % 60;
129 const char * remaining = duration < 0 ? " remaining" : "";
131 return [NSString stringWithFormat:@"%d hours %d minutes%s", hours, mins, remaining];
133 return [NSString stringWithFormat:@"%d minutes%s", mins, remaining];
135 return [NSString stringWithFormat:@"%d minutes %d seconds%s", mins, seconds, remaining];
137 return [NSString stringWithFormat:@"%d seconds%s", seconds, remaining];
141 // Return a string that represents an undefined time.
149 return [value intValue];
153 - (NSComparisonResult)compare:(VLCTime *)aTime
155 if (!aTime && !value)
156 return NSOrderedSame;
158 return NSOrderedDescending;
160 return [value compare:aTime.numberValue];