VLCTime.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. return [NSString stringWithFormat:@"%02d:%02d:%02d",
  71. (long) (duration / 3600),
  72. (long)((duration / 60) % 60),
  73. (long) (duration % 60)];
  74. }
  75. else
  76. {
  77. // Return a string that represents an undefined time.
  78. return @"--:--:--";
  79. }
  80. }
  81. - (NSComparisonResult)compare:(VLCTime *)aTime
  82. {
  83. if (!aTime && !value)
  84. return NSOrderedSame;
  85. else if (!aTime)
  86. return NSOrderedDescending;
  87. else
  88. return [value compare:aTime.numberValue];
  89. }
  90. @end