VLCTime.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (aNumber)
  47. value = [aNumber copy];
  48. else
  49. value = nil;
  50. }
  51. return self;
  52. }
  53. - (id)initWithInt:(int)aInt
  54. {
  55. if (self = [super init]) {
  56. if (aInt)
  57. value = [[NSNumber numberWithInt: aInt] retain];
  58. else
  59. value = nil;
  60. }
  61. return self;
  62. }
  63. - (void)dealloc
  64. {
  65. [value release];
  66. [super dealloc];
  67. }
  68. - (id)copyWithZone:(NSZone *)zone
  69. {
  70. return [[VLCTime alloc] initWithNumber:value];
  71. }
  72. /* NSObject Overrides */
  73. - (NSString *)description
  74. {
  75. return self.stringValue;
  76. }
  77. /* Operations */
  78. - (NSNumber *)numberValue
  79. {
  80. return value ? [[value copy] autorelease] : nil;
  81. }
  82. - (NSString *)stringValue
  83. {
  84. if (value) {
  85. long long duration = [value longLongValue] / 1000;
  86. long long positiveDuration = llabs(duration);
  87. if (positiveDuration > 3600)
  88. return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
  89. duration < 0 ? "-" : "",
  90. (long) (positiveDuration / 3600),
  91. (long)((positiveDuration / 60) % 60),
  92. (long) (positiveDuration % 60)];
  93. else
  94. return [NSString stringWithFormat:@"%s%02ld:%02ld",
  95. duration < 0 ? "-" : "",
  96. (long)((positiveDuration / 60) % 60),
  97. (long) (positiveDuration % 60)];
  98. } else {
  99. // Return a string that represents an undefined time.
  100. return @"--:--";
  101. }
  102. }
  103. - (NSString *)verboseStringValue
  104. {
  105. if (value) {
  106. long long duration = [value longLongValue] / 1000;
  107. long long positiveDuration = llabs(duration);
  108. long hours = positiveDuration / 3600;
  109. long mins = (positiveDuration / 60) % 60;
  110. long seconds = positiveDuration % 60;
  111. const char * remaining = duration < 0 ? " remaining" : "";
  112. if (hours > 0)
  113. return [NSString stringWithFormat:@"%ld hours %ld minutes%s", hours, mins, remaining];
  114. else if (mins > 5)
  115. return [NSString stringWithFormat:@"%ld minutes%s", mins, remaining];
  116. else if (mins > 0)
  117. return [NSString stringWithFormat:@"%ld minutes %ld seconds%s", mins, seconds, remaining];
  118. else
  119. return [NSString stringWithFormat:@"%ld seconds%s", seconds, remaining];
  120. } else {
  121. // Return a string that represents an undefined time.
  122. return @"";
  123. }
  124. }
  125. - (int)intValue
  126. {
  127. if (value)
  128. return [value intValue];
  129. return 0;
  130. }
  131. - (NSComparisonResult)compare:(VLCTime *)aTime
  132. {
  133. if (!aTime && !value)
  134. return NSOrderedSame;
  135. else if (!aTime)
  136. return NSOrderedDescending;
  137. else
  138. return [value compare:aTime.numberValue];
  139. }
  140. @end