VLCTime.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*****************************************************************************
  2. * VLCTime.m: VLCKit.framework VLCTime implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 VLC authors and VideoLAN
  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 it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCTime.h"
  25. @implementation VLCTime
  26. /* Factories */
  27. + (VLCTime *)nullTime
  28. {
  29. static VLCTime * nullTime = nil;
  30. if (!nullTime)
  31. nullTime = [[VLCTime timeWithNumber:nil] retain];
  32. return nullTime;
  33. }
  34. + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
  35. {
  36. return [[[VLCTime alloc] initWithNumber:aNumber] autorelease];
  37. }
  38. + (VLCTime *)timeWithInt:(NSInteger)aInt
  39. {
  40. return [[[VLCTime alloc] initWithInt:aInt] autorelease];
  41. }
  42. /* Initializers */
  43. - (id)initWithNumber:(NSNumber *)aNumber
  44. {
  45. if (self = [super init])
  46. {
  47. if (aNumber)
  48. value = [aNumber copy];
  49. else
  50. value = nil;
  51. }
  52. return self;
  53. }
  54. - (id)initWithInt:(int)aInt
  55. {
  56. if (self = [super init])
  57. {
  58. if (aInt)
  59. value = [[NSNumber numberWithInt: aInt] retain];
  60. else
  61. value = nil;
  62. }
  63. return self;
  64. }
  65. - (void)dealloc
  66. {
  67. [value release];
  68. [super dealloc];
  69. }
  70. - (id)copyWithZone:(NSZone *)zone
  71. {
  72. return [[VLCTime alloc] initWithNumber:value];
  73. }
  74. /* NSObject Overrides */
  75. - (NSString *)description
  76. {
  77. return self.stringValue;
  78. }
  79. /* Operations */
  80. - (NSNumber *)numberValue
  81. {
  82. return value ? [[value copy] autorelease] : nil;
  83. }
  84. - (NSString *)stringValue
  85. {
  86. if (value)
  87. {
  88. long long duration = [value longLongValue] / 1000;
  89. long long positiveDuration = llabs(duration);
  90. if( positiveDuration > 3600 )
  91. return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
  92. duration < 0 ? "-" : "",
  93. (long) (positiveDuration / 3600),
  94. (long)((positiveDuration / 60) % 60),
  95. (long) (positiveDuration % 60)];
  96. else
  97. return [NSString stringWithFormat:@"%s%02ld:%02ld",
  98. duration < 0 ? "-" : "",
  99. (long)((positiveDuration / 60) % 60),
  100. (long) (positiveDuration % 60)];
  101. }
  102. else
  103. {
  104. // Return a string that represents an undefined time.
  105. return @"--:--";
  106. }
  107. }
  108. - (NSString *)verboseStringValue
  109. {
  110. if (value)
  111. {
  112. long long duration = [value longLongValue] / 1000;
  113. long long positiveDuration = llabs(duration);
  114. long hours = positiveDuration / 3600;
  115. long mins = (positiveDuration / 60) % 60;
  116. long seconds = positiveDuration % 60;
  117. const char * remaining = duration < 0 ? " remaining" : "";
  118. if (hours > 0)
  119. return [NSString stringWithFormat:@"%ld hours %ld minutes%s", hours, mins, remaining];
  120. else if (mins > 5)
  121. return [NSString stringWithFormat:@"%ld minutes%s", mins, remaining];
  122. else if (mins > 0)
  123. return [NSString stringWithFormat:@"%ld minutes %ld seconds%s", mins, seconds, remaining];
  124. else
  125. return [NSString stringWithFormat:@"%ld seconds%s", seconds, remaining];
  126. }
  127. else
  128. {
  129. // Return a string that represents an undefined time.
  130. return @"";
  131. }
  132. }
  133. - (int)intValue
  134. {
  135. if( value )
  136. return [value intValue];
  137. return 0;
  138. }
  139. - (NSComparisonResult)compare:(VLCTime *)aTime
  140. {
  141. if (!aTime && !value)
  142. return NSOrderedSame;
  143. else if (!aTime)
  144. return NSOrderedDescending;
  145. else
  146. return [value compare:aTime.numberValue];
  147. }
  148. @end