]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCTime.m
macosx/framework: Support printing negative duration.
[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 /* Initializers */
43 - (id)initWithNumber:(NSNumber *)aNumber
44 {
45     if (self = [super init])
46     {
47         if (aNumber)
48             value = [[aNumber copy] retain];
49         else
50             value = nil;
51     }
52     return self;
53 }
54
55 - (void)dealloc
56 {
57     [value release];
58     [super dealloc];
59 }
60
61 - (id)copyWithZone:(NSZone *)zone
62 {
63     return [[VLCTime alloc] initWithNumber:value];
64 }
65
66 /* NSObject Overrides */
67 - (NSString *)description
68 {
69     return self.stringValue;
70 }
71
72 /* Operations */
73 - (NSNumber *)numberValue
74 {
75     return value ? [[value copy] autorelease] : nil;
76 }
77
78 - (NSString *)stringValue
79 {
80     if (value)
81     {
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)];
90         else
91             return [NSString stringWithFormat:@"%s%02d:%02d",
92                             duration < 0 ? "-" : ""
93                     (long)((positiveDuration / 60) % 60),
94                     (long) (positiveDuration % 60)];
95     }
96     else
97     {
98         // Return a string that represents an undefined time.
99         return @"--:--";
100     }
101 }
102
103 - (NSComparisonResult)compare:(VLCTime *)aTime
104 {
105     if (!aTime && !value)
106         return NSOrderedSame;
107     else if (!aTime)
108         return NSOrderedDescending;
109     else
110         return [value compare:aTime.numberValue];
111 }
112 @end