VLCKit: Import MobileVLCKit.
[vlc/asuraparaju-public.git] / projects / macosx / framework / Sources / VLCTime.m
blob25c65934a56ddaeade541c3f3b23f15f541e8adb
1 /*****************************************************************************
2  * VLCTime.m: VLCKit.framework VLCTime implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
25 #import "VLCTime.h"
27 @implementation VLCTime
28 /* Factories */
29 + (VLCTime *)nullTime
31     static VLCTime * nullTime = nil;
32     if (!nullTime)
33         nullTime = [[VLCTime timeWithNumber:nil] retain];
34     return nullTime;
37 + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
39     return [[[VLCTime alloc] initWithNumber:aNumber] autorelease];
42 + (VLCTime *)timeWithInt:(NSInteger)aInt
44     return [[[VLCTime alloc] initWithInt:aInt] autorelease];
47 /* Initializers */
48 - (id)initWithNumber:(NSNumber *)aNumber
50     if (self = [super init])
51     {
52         if (aNumber)
53             value = [aNumber copy];
54         else
55             value = nil;
56     }
57     return self;
60 - (id)initWithInt:(int)aInt
62     if (self = [super init])
63     {
64         if (aInt)
65             value = [[NSNumber numberWithInt: aInt] retain];
66         else
67             value = nil;
68     }
69     return self;
72 - (void)dealloc
74     [value release];
75     [super dealloc];
78 - (id)copyWithZone:(NSZone *)zone
80     return [[VLCTime alloc] initWithNumber:value];
83 /* NSObject Overrides */
84 - (NSString *)description
86     return self.stringValue;
89 /* Operations */
90 - (NSNumber *)numberValue
92     return value ? [[value copy] autorelease] : nil;
95 - (NSString *)stringValue
97     if (value)
98     {
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)];
107         else
108             return [NSString stringWithFormat:@"%s%02d:%02d",
109                             duration < 0 ? "-" : "",
110                     (long)((positiveDuration / 60) % 60),
111                     (long) (positiveDuration % 60)];
112     }
113     else
114     {
115         // Return a string that represents an undefined time.
116         return @"--:--";
117     }
120 - (NSString *)verboseStringValue
122     if (value)
123     {
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" : "";
130         if (hours > 0)
131             return [NSString stringWithFormat:@"%d hours %d minutes%s", hours, mins, remaining];
132         else if (mins > 5)
133             return [NSString stringWithFormat:@"%d minutes%s", mins, remaining];
134         else if (mins > 0)
135             return [NSString stringWithFormat:@"%d minutes %d seconds%s", mins, seconds, remaining];
136         else
137             return [NSString stringWithFormat:@"%d seconds%s", seconds, remaining];
138     }
139     else
140     {
141         // Return a string that represents an undefined time.
142         return @"";
143     }
146 - (int)intValue
148     if( value )
149         return [value intValue];
150     return 0;
153 - (NSComparisonResult)compare:(VLCTime *)aTime
155     if (!aTime && !value)
156         return NSOrderedSame;
157     else if (!aTime)
158         return NSOrderedDescending;
159     else
160         return [value compare:aTime.numberValue];
162 @end