]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCTime.m
566d94b1a964909c5e6b20d48082c04886149952
[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 + (VLCTime *)nullTime
29 {
30     static VLCTime * nullTime = nil;
31     if (!nullTime)
32         nullTime = [[VLCTime timeWithNumber:nil] retain];
33     return nullTime;
34 }
35
36 + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
37 {
38     return [[[VLCTime alloc] initWithNumber:aNumber] autorelease];
39 }
40
41 // TODO: Implement [VLCTime timeWithString]
42 //+ (VLCTime *)timeWithString:(NSString *)aString
43 //{
44 //  return [[[VLCTime alloc] initWithString:aString] autorelease];
45 //}
46
47 - (id)initWithNumber:(NSNumber *)aNumber
48 {
49     if (self = [super init])
50     {
51         if (aNumber)
52             value = [[aNumber copy] retain];
53         else
54             value = nil;
55     }
56     return self;
57 }
58
59 // TODO: Implement [VLCTime initWithString]
60 //- (id)initWithString:(NSString *)aString
61 //{
62 //  // Sounds like a good idea but I really don't think there is any value added
63 //  if (self = [super init])
64 //  {
65 //      // convert value
66 //  }
67 //  return self;
68 //}
69
70 - (void)dealloc
71 {
72     [value release];
73     [super dealloc];
74 }
75
76 - (NSNumber *)numberValue
77 {
78     return value ? [[value copy] autorelease] : nil;
79 }
80
81 - (NSString *)stringValue
82 {
83     if (value)
84     {
85         long long duration = [value longLongValue] / 1000000;
86         return [NSString stringWithFormat:@"%02d:%02d:%02d",
87             (long) (duration / 3600),
88             (long)((duration / 60) % 60),
89             (long) (duration % 60)];
90     }
91     else
92     {
93         // Return a string that represents an undefined time.
94         return @"--:--:--";
95     }
96 }
97
98 - (NSComparisonResult)compare:(VLCTime *)aTime
99 {
100     if (!aTime && !value)
101         return NSOrderedSame;
102     else if (!aTime)
103         return NSOrderedDescending;
104     else
105         return [value compare:aTime.numberValue];
106 }
107
108 - (NSString *)description
109 {
110     return self.stringValue;
111 }
112 @end