VLCTime.m 4.5 KB

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