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];
43 - (id)initWithNumber:(NSNumber *)aNumber
45 if (self = [super init])
48 value = [[aNumber copy] retain];
61 - (id)copyWithZone:(NSZone *)zone
63 return [[VLCTime alloc] initWithNumber:value];
66 /* NSObject Overrides */
67 - (NSString *)description
69 return self.stringValue;
73 - (NSNumber *)numberValue
75 return value ? [[value copy] autorelease] : nil;
78 - (NSString *)stringValue
82 long long duration = [value longLongValue] / 1000000;
83 long long positiveDuration = llabs(duration);
84 if( positiveDuration > 3600 )
85 return [NSString stringWithFormat:@"%s%01d:%02d:%02d",
86 duration < 0 ? "-" : ""
87 (long) (positiveDuration / 3600),
88 (long)((positiveDuration / 60) % 60),
89 (long) (positiveDuration % 60)];
91 return [NSString stringWithFormat:@"%s%02d:%02d",
92 duration < 0 ? "-" : ""
93 (long)((positiveDuration / 60) % 60),
94 (long) (positiveDuration % 60)];
98 // Return a string that represents an undefined time.
103 - (NSComparisonResult)compare:(VLCTime *)aTime
105 if (!aTime && !value)
106 return NSOrderedSame;
108 return NSOrderedDescending;
110 return [value compare:aTime.numberValue];