VLCTime.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. - (NSNumber *)numberValue
  66. {
  67. return _value;
  68. }
  69. - (NSString *)stringValue
  70. {
  71. if (_value) {
  72. long long duration = [_value longLongValue] / 1000;
  73. long long positiveDuration = llabs(duration);
  74. if (positiveDuration > 3600)
  75. return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
  76. duration < 0 ? "-" : "",
  77. (long) (positiveDuration / 3600),
  78. (long)((positiveDuration / 60) % 60),
  79. (long) (positiveDuration % 60)];
  80. else
  81. return [NSString stringWithFormat:@"%s%02ld:%02ld",
  82. duration < 0 ? "-" : "",
  83. (long)((positiveDuration / 60) % 60),
  84. (long) (positiveDuration % 60)];
  85. } else {
  86. // Return a string that represents an undefined time.
  87. return @"--:--";
  88. }
  89. }
  90. - (NSString *)verboseStringValue
  91. {
  92. if (_value) {
  93. long long duration = [_value longLongValue] / 1000;
  94. long long positiveDuration = llabs(duration);
  95. long hours = (long)(positiveDuration / 3600);
  96. long mins = (long)((positiveDuration / 60) % 60);
  97. long seconds = (long)(positiveDuration % 60);
  98. const char * remaining = duration < 0 ? " remaining" : "";
  99. if (hours > 0)
  100. return [NSString stringWithFormat:@"%ld hours %ld minutes%s", hours, mins, remaining];
  101. if (mins > 5)
  102. return [NSString stringWithFormat:@"%ld minutes%s", mins, remaining];
  103. if (mins > 0)
  104. return [NSString stringWithFormat:@"%ld minutes %ld seconds%s", mins, seconds, remaining];
  105. return [NSString stringWithFormat:@"%ld seconds%s", seconds, remaining];
  106. }
  107. // Return a string that represents an undefined time.
  108. return @"";
  109. }
  110. - (NSString *)minuteStringValue
  111. {
  112. if (_value) {
  113. long long positiveDuration = llabs([_value longLongValue]);
  114. long minutes = (long)(positiveDuration / 60000);
  115. return [NSString stringWithFormat:@"%ld", minutes];
  116. }
  117. return @"";
  118. }
  119. - (int)intValue
  120. {
  121. return [_value intValue];
  122. }
  123. - (NSComparisonResult)compare:(VLCTime *)aTime
  124. {
  125. NSInteger a = [_value integerValue];
  126. NSInteger b = [aTime.value integerValue];
  127. return (a > b) ? NSOrderedDescending :
  128. (a < b) ? NSOrderedAscending :
  129. NSOrderedSame;
  130. }
  131. @end