PageRenderTime 32ms CodeModel.GetById 1ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 16#if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_PHOTOS_SERVICE \
 17  || GDATA_INCLUDE_YOUTUBE_SERVICE
 18
 19#import "GDataNormalPlayTime.h"
 20
 21static NSString* const kNowString = @"now";
 22
 23@implementation GDataNormalPlayTime
 24
 25+ (GDataNormalPlayTime *)normalPlayTimeWithString:(NSString *)str {
 26  
 27  GDataNormalPlayTime *npt = [[[GDataNormalPlayTime alloc] init] autorelease];
 28  
 29  [npt setFromString:str];
 30  return npt;
 31}
 32
 33- (long long)timeOffsetInMilliseconds { // -1 if "now"
 34  if (isNow_) return -1;
 35  return ms_;
 36}
 37
 38- (void)setTimeOffsetInMilliseconds:(long long)ms {
 39  ms_ = ms;
 40  isNow_ = (ms == -1);
 41}
 42
 43- (BOOL)isNow {
 44  return isNow_; 
 45}
 46
 47- (void)setIsNow:(BOOL)isNow {
 48  isNow_ = isNow; 
 49}
 50
 51- (NSString *)HHMMSSString { // hh:mm:ss.fraction or "now"
 52  if (isNow_) {
 53    return kNowString;
 54  }
 55  
 56  long fractional = (long) (ms_ % 1000LL);
 57  long totalSeconds = (long) (ms_ / 1000LL);
 58  long seconds = totalSeconds % 60L;
 59  long totalMinutes = totalSeconds / 60L;
 60  long minutes = totalMinutes % 60L;
 61  long hours = totalMinutes / 60L;
 62  
 63  if (fractional > 0) {
 64    return [NSString stringWithFormat:@"%ld:%02ld:%02ld.%03ld",
 65      hours, minutes, seconds, fractional];
 66  } 
 67  return [NSString stringWithFormat:@"%ld:%02ld:%02ld",
 68    hours, minutes, seconds];
 69}
 70
 71- (NSString *)secondsString { // seconds.fraction or "now"
 72  if (isNow_) {
 73    return kNowString;
 74  }
 75  int seconds = (int) (ms_ / 1000LL);
 76  int fractional = (int) (ms_ % 1000LL);
 77  
 78  if (fractional == 0) {
 79    return [NSString stringWithFormat:@"%d", seconds];
 80  }
 81  return [NSString stringWithFormat:@"%d.%03d", seconds, fractional];
 82}
 83
 84- (void)setFromString:(NSString *)str {
 85  NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
 86  NSString *trimmedStr = [str stringByTrimmingCharactersInSet:whitespace];
 87  
 88  // handle "now"
 89  if ([trimmedStr caseInsensitiveCompare:@"now"] == NSOrderedSame) {
 90    isNow_ = YES;
 91    ms_ = -1;
 92    return;
 93  }
 94  
 95  // parse hh:mm:ss.fff or ss.fff into milliseconds
 96  long seconds = 0;
 97  long thousandths = 0;
 98  
 99  NSScanner *scanner = [NSScanner scannerWithString:str];
100  NSCharacterSet *period = [NSCharacterSet characterSetWithCharactersInString:@"."];
101  NSCharacterSet *colon = [NSCharacterSet characterSetWithCharactersInString:@":"];
102  
103  int scannedInt;
104  if ([scanner scanInt:&scannedInt]) {
105    seconds = scannedInt;
106    
107    if ([scanner scanCharactersFromSet:colon intoString:NULL]
108        && [scanner scanInt:&scannedInt]) {
109      // push seconds to minutes
110      seconds = seconds * 60 + scannedInt;
111    }
112  
113    if ([scanner scanCharactersFromSet:colon intoString:NULL]
114        && [scanner scanInt:&scannedInt]) {
115      // push minutes to hours, seconds to minutes
116      seconds = seconds * 60 + scannedInt;
117    }
118    
119    if ([scanner scanCharactersFromSet:period intoString:NULL]
120        && [scanner scanInt:&scannedInt]) {
121      
122      // append 000 and take the first 3 digits to create thousands
123      NSString *paddedFraction = [NSString stringWithFormat:@"%d000", scannedInt];
124      NSString *thousandthsStr = [paddedFraction substringToIndex:3];
125      thousandths = [thousandthsStr intValue];
126    }
127  }    
128  ms_ = seconds * 1000 + thousandths;
129  isNow_ = NO;
130}
131
132- (BOOL)isEqual:(GDataNormalPlayTime *)other {
133  if ([self isNow]) {
134    return ([self isNow] == [other isNow]);
135  }
136  return ([self timeOffsetInMilliseconds] == [other timeOffsetInMilliseconds]);
137}
138
139- (id)copyWithZone:(NSZone *)zone {
140  GDataNormalPlayTime* newObj = [[[self class] allocWithZone:zone] init];
141  [newObj setTimeOffsetInMilliseconds:[self timeOffsetInMilliseconds]];
142  [newObj setIsNow:[self isNow]];
143  return newObj;
144}
145
146- (NSString *)description {
147  return [NSString stringWithFormat:@"%@ %p: {%@}",
148    [self class], self, [self HHMMSSString]];
149
150}
151
152@end
153
154#endif // #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_*_SERVICE