VLCTime.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. nullTime = [VLCTime timeWithNumber:nil];
  33. });
  34. return nullTime;
  35. }
  36. + (VLCTime *)timeWithNumber:(NSNumber *)aNumber
  37. {
  38. return [[VLCTime alloc] initWithNumber:aNumber];
  39. }
  40. + (VLCTime *)timeWithInt:(int)aInt
  41. {
  42. return [[VLCTime alloc] initWithInt:aInt];
  43. }
  44. /* Initializers */
  45. - (instancetype)initWithNumber:(NSNumber *)aNumber
  46. {
  47. if (self = [super init]) {
  48. _value = aNumber;
  49. }
  50. return self;
  51. }
  52. - (instancetype)initWithInt:(int)aInt
  53. {
  54. if (self = [super init]) {
  55. if (aInt)
  56. _value = @(aInt);
  57. }
  58. return self;
  59. }
  60. /* NSObject Overrides */
  61. - (NSString *)description
  62. {
  63. return self.stringValue;
  64. }
  65. - (NSString *)stringValue
  66. {
  67. if (_value) {
  68. long long duration = [_value longLongValue];
  69. if (duration == INT_MAX || duration == INT_MIN) {
  70. // Return a string that represents an undefined time.
  71. return @"--:--";
  72. }
  73. duration = duration / 1000;
  74. long long positiveDuration = llabs(duration);
  75. if (positiveDuration > 3600)
  76. return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
  77. duration < 0 ? "-" : "",
  78. (long) (positiveDuration / 3600),
  79. (long)((positiveDuration / 60) % 60),
  80. (long) (positiveDuration % 60)];
  81. else
  82. return [NSString stringWithFormat:@"%s%02ld:%02ld",
  83. duration < 0 ? "-" : "",
  84. (long)((positiveDuration / 60) % 60),
  85. (long) (positiveDuration % 60)];
  86. } else {
  87. // Return a string that represents an undefined time.
  88. return @"--:--";
  89. }
  90. }
  91. - (NSString *)verboseStringValue
  92. {
  93. if (!_value)
  94. return @"";
  95. long long duration = [_value longLongValue] / 1000;
  96. long long positiveDuration = llabs(duration);
  97. long hours = (long)(positiveDuration / 3600);
  98. long mins = (long)((positiveDuration / 60) % 60);
  99. long seconds = (long)(positiveDuration % 60);
  100. BOOL remaining = duration < 0;
  101. NSDateComponents *components = [[NSDateComponents alloc] init];
  102. [components setHour:hours];
  103. [components setMinute:mins];
  104. [components setSecond:seconds];
  105. NSString *verboseString = [NSDateComponentsFormatter localizedStringFromDateComponents:components unitsStyle:NSDateComponentsFormatterUnitsStyleFull];
  106. verboseString = remaining ? [NSString stringWithFormat:@"%@ remaining", verboseString] : verboseString;
  107. return [verboseString stringByReplacingOccurrencesOfString:@"," withString:@""];
  108. }
  109. - (NSString *)minuteStringValue
  110. {
  111. if (_value) {
  112. long long positiveDuration = llabs([_value longLongValue]);
  113. long minutes = (long)(positiveDuration / 60000);
  114. return [NSString stringWithFormat:@"%ld", minutes];
  115. }
  116. return @"";
  117. }
  118. - (int)intValue
  119. {
  120. return [_value intValue];
  121. }
  122. - (NSComparisonResult)compare:(VLCTime *)aTime
  123. {
  124. NSInteger a = [_value integerValue];
  125. NSInteger b = [aTime.value integerValue];
  126. return (a > b) ? NSOrderedDescending :
  127. (a < b) ? NSOrderedAscending :
  128. NSOrderedSame;
  129. }
  130. - (BOOL)isEqual:(id)object
  131. {
  132. if (![object isKindOfClass:[VLCTime class]])
  133. return NO;
  134. return [[self description] isEqual:[object description]];
  135. }
  136. - (NSUInteger)hash
  137. {
  138. return [[self description] hash];
  139. }
  140. @end