]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCTime.m
8e3dcbf52360d098a887b26354b37d49ca3cbcaf
[vlc] / projects / macosx / framework / Sources / VLCTime.m
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  *****************************************************************************/
24
25 #import <VLCTime.h>
26
27 @implementation VLCTime
28 /* Factories */
29 + (VLCTime *)nullTime
30 {
31     static VLCTime * nullTime = nil;
32     if (!nullTime)
33         nullTime = [[VLCTime timeWithNumber:nil] retain];
34     return nullTime;
35 }
36
37 + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
38 {
39     return [[[VLCTime alloc] initWithNumber:aNumber] autorelease];
40 }
41
42 + (VLCTime *)timeWithInt:(int)aInt
43 {
44     return [[[VLCTime alloc] initWithInt:aInt] autorelease];
45 }
46
47 /* Initializers */
48 - (id)initWithNumber:(NSNumber *)aNumber
49 {
50     if (self = [super init])
51     {
52         if (aNumber)
53             value = [aNumber copy];
54         else
55             value = nil;
56     }
57     return self;
58 }
59
60 - (id)initWithInt:(int)aInt
61 {
62     if (self = [super init])
63     {
64         if (aInt)
65             value = [[NSNumber numberWithInt: aInt] retain];
66         else
67             value = nil;
68     }
69     return self;
70 }
71
72 - (void)dealloc
73 {
74     [value release];
75     [super dealloc];
76 }
77
78 - (id)copyWithZone:(NSZone *)zone
79 {
80     return [[VLCTime alloc] initWithNumber:value];
81 }
82
83 /* NSObject Overrides */
84 - (NSString *)description
85 {
86     return self.stringValue;
87 }
88
89 /* Operations */
90 - (NSNumber *)numberValue
91 {
92     return value ? [[value copy] autorelease] : nil;
93 }
94
95 - (NSString *)stringValue
96 {
97     if (value)
98     {
99         long long duration = [value longLongValue] / 1000000;
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     }
118 }
119
120 - (NSString *)verboseStringValue
121 {
122     if (value)
123     {
124         long long duration = [value longLongValue] / 1000000;
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     }
144 }
145
146 - (int)intValue
147 {
148     if( value )
149         return [value intValue];
150     return 0;
151 }
152
153 - (NSComparisonResult)compare:(VLCTime *)aTime
154 {
155     if (!aTime && !value)
156         return NSOrderedSame;
157     else if (!aTime)
158         return NSOrderedDescending;
159     else
160         return [value compare:aTime.numberValue];
161 }
162 @end