/core/externals/update-engine/externals/gdata-objectivec-client/Source/Media/GDataNormalPlayTime.m

http://macfuse.googlecode.com/ · Objective C · 154 lines · 104 code · 31 blank · 19 comment · 21 complexity · 11063254d5ceaf4d19bc4ad8d2cffb32 MD5 · raw file

  1. /* Copyright (c) 2007 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_PHOTOS_SERVICE \
  16. || GDATA_INCLUDE_YOUTUBE_SERVICE
  17. #import "GDataNormalPlayTime.h"
  18. static NSString* const kNowString = @"now";
  19. @implementation GDataNormalPlayTime
  20. + (GDataNormalPlayTime *)normalPlayTimeWithString:(NSString *)str {
  21. GDataNormalPlayTime *npt = [[[GDataNormalPlayTime alloc] init] autorelease];
  22. [npt setFromString:str];
  23. return npt;
  24. }
  25. - (long long)timeOffsetInMilliseconds { // -1 if "now"
  26. if (isNow_) return -1;
  27. return ms_;
  28. }
  29. - (void)setTimeOffsetInMilliseconds:(long long)ms {
  30. ms_ = ms;
  31. isNow_ = (ms == -1);
  32. }
  33. - (BOOL)isNow {
  34. return isNow_;
  35. }
  36. - (void)setIsNow:(BOOL)isNow {
  37. isNow_ = isNow;
  38. }
  39. - (NSString *)HHMMSSString { // hh:mm:ss.fraction or "now"
  40. if (isNow_) {
  41. return kNowString;
  42. }
  43. long fractional = (long) (ms_ % 1000LL);
  44. long totalSeconds = (long) (ms_ / 1000LL);
  45. long seconds = totalSeconds % 60L;
  46. long totalMinutes = totalSeconds / 60L;
  47. long minutes = totalMinutes % 60L;
  48. long hours = totalMinutes / 60L;
  49. if (fractional > 0) {
  50. return [NSString stringWithFormat:@"%ld:%02ld:%02ld.%03ld",
  51. hours, minutes, seconds, fractional];
  52. }
  53. return [NSString stringWithFormat:@"%ld:%02ld:%02ld",
  54. hours, minutes, seconds];
  55. }
  56. - (NSString *)secondsString { // seconds.fraction or "now"
  57. if (isNow_) {
  58. return kNowString;
  59. }
  60. int seconds = (int) (ms_ / 1000LL);
  61. int fractional = (int) (ms_ % 1000LL);
  62. if (fractional == 0) {
  63. return [NSString stringWithFormat:@"%d", seconds];
  64. }
  65. return [NSString stringWithFormat:@"%d.%03d", seconds, fractional];
  66. }
  67. - (void)setFromString:(NSString *)str {
  68. NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  69. NSString *trimmedStr = [str stringByTrimmingCharactersInSet:whitespace];
  70. // handle "now"
  71. if ([trimmedStr caseInsensitiveCompare:@"now"] == NSOrderedSame) {
  72. isNow_ = YES;
  73. ms_ = -1;
  74. return;
  75. }
  76. // parse hh:mm:ss.fff or ss.fff into milliseconds
  77. long seconds = 0;
  78. long thousandths = 0;
  79. NSScanner *scanner = [NSScanner scannerWithString:str];
  80. NSCharacterSet *period = [NSCharacterSet characterSetWithCharactersInString:@"."];
  81. NSCharacterSet *colon = [NSCharacterSet characterSetWithCharactersInString:@":"];
  82. int scannedInt;
  83. if ([scanner scanInt:&scannedInt]) {
  84. seconds = scannedInt;
  85. if ([scanner scanCharactersFromSet:colon intoString:NULL]
  86. && [scanner scanInt:&scannedInt]) {
  87. // push seconds to minutes
  88. seconds = seconds * 60 + scannedInt;
  89. }
  90. if ([scanner scanCharactersFromSet:colon intoString:NULL]
  91. && [scanner scanInt:&scannedInt]) {
  92. // push minutes to hours, seconds to minutes
  93. seconds = seconds * 60 + scannedInt;
  94. }
  95. if ([scanner scanCharactersFromSet:period intoString:NULL]
  96. && [scanner scanInt:&scannedInt]) {
  97. // append 000 and take the first 3 digits to create thousands
  98. NSString *paddedFraction = [NSString stringWithFormat:@"%d000", scannedInt];
  99. NSString *thousandthsStr = [paddedFraction substringToIndex:3];
  100. thousandths = [thousandthsStr intValue];
  101. }
  102. }
  103. ms_ = seconds * 1000 + thousandths;
  104. isNow_ = NO;
  105. }
  106. - (BOOL)isEqual:(GDataNormalPlayTime *)other {
  107. if ([self isNow]) {
  108. return ([self isNow] == [other isNow]);
  109. }
  110. return ([self timeOffsetInMilliseconds] == [other timeOffsetInMilliseconds]);
  111. }
  112. - (id)copyWithZone:(NSZone *)zone {
  113. GDataNormalPlayTime* newObj = [[[self class] allocWithZone:zone] init];
  114. [newObj setTimeOffsetInMilliseconds:[self timeOffsetInMilliseconds]];
  115. [newObj setIsNow:[self isNow]];
  116. return newObj;
  117. }
  118. - (NSString *)description {
  119. return [NSString stringWithFormat:@"%@ %p: {%@}",
  120. [self class], self, [self HHMMSSString]];
  121. }
  122. @end
  123. #endif // #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_*_SERVICE