VLCTime.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /* NSObject Overrides */
  56. - (NSString *)description
  57. {
  58. return self.stringValue;
  59. }
  60. /* Operations */
  61. - (NSNumber *)numberValue
  62. {
  63. return value ? [[value copy] autorelease] : nil;
  64. }
  65. - (NSString *)stringValue
  66. {
  67. if (value)
  68. {
  69. long long duration = [value longLongValue] / 1000000;
  70. if( duration > 3600 )
  71. return [NSString stringWithFormat:@"%01d:%02d:%02d",
  72. (long) (duration / 3600),
  73. (long)((duration / 60) % 60),
  74. (long) (duration % 60)];
  75. else
  76. return [NSString stringWithFormat:@"%02d:%02d",
  77. (long)((duration / 60) % 60),
  78. (long) (duration % 60)];
  79. }
  80. else
  81. {
  82. // Return a string that represents an undefined time.
  83. return @"--:--";
  84. }
  85. }
  86. - (NSComparisonResult)compare:(VLCTime *)aTime
  87. {
  88. if (!aTime && !value)
  89. return NSOrderedSame;
  90. else if (!aTime)
  91. return NSOrderedDescending;
  92. else
  93. return [value compare:aTime.numberValue];
  94. }
  95. @end