VLCTime.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*****************************************************************************
  2. * VLCTime.m: VLCKit.framework VLCTime implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  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
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, 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. /* Initializers */
  39. - (id)initWithNumber:(NSNumber *)aNumber
  40. {
  41. if (self = [super init])
  42. {
  43. if (aNumber)
  44. value = [[aNumber copy] retain];
  45. else
  46. value = nil;
  47. }
  48. return self;
  49. }
  50. - (void)dealloc
  51. {
  52. [value release];
  53. [super dealloc];
  54. }
  55. - (id)copyWithZone:(NSZone *)zone
  56. {
  57. return [[VLCTime alloc] initWithNumber:value];
  58. }
  59. /* NSObject Overrides */
  60. - (NSString *)description
  61. {
  62. return self.stringValue;
  63. }
  64. /* Operations */
  65. - (NSNumber *)numberValue
  66. {
  67. return value ? [[value copy] autorelease] : nil;
  68. }
  69. - (NSString *)stringValue
  70. {
  71. if (value)
  72. {
  73. long long duration = [value longLongValue] / 1000000;
  74. long long positiveDuration = llabs(duration);
  75. if( positiveDuration > 3600 )
  76. return [NSString stringWithFormat:@"%s%01d:%02d:%02d",
  77. duration < 0 ? "-" : ""
  78. (long) (positiveDuration / 3600),
  79. (long)((positiveDuration / 60) % 60),
  80. (long) (positiveDuration % 60)];
  81. else
  82. return [NSString stringWithFormat:@"%s%02d:%02d",
  83. duration < 0 ? "-" : ""
  84. (long)((positiveDuration / 60) % 60),
  85. (long) (positiveDuration % 60)];
  86. }
  87. else
  88. {
  89. // Return a string that represents an undefined time.
  90. return @"--:--";
  91. }
  92. }
  93. - (NSComparisonResult)compare:(VLCTime *)aTime
  94. {
  95. if (!aTime && !value)
  96. return NSOrderedSame;
  97. else if (!aTime)
  98. return NSOrderedDescending;
  99. else
  100. return [value compare:aTime.numberValue];
  101. }
  102. @end