Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
WITimeIntervalFormatter.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredFoundation/WITimeIntervalFormatter.h>
00030 
00031 @implementation WITimeIntervalFormatter
00032 
00033 - (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
00034         NSString                *string;
00035         NSUInteger              days, hours, minutes, seconds;
00036         BOOL                    past = NO;
00037 
00038         interval = rint(interval);
00039 
00040         if(interval < 0) {
00041                 past = YES;
00042                 interval = -interval;
00043         }
00044 
00045         days = interval / 86400;
00046         interval -= days * 86400;
00047 
00048         hours = interval / 3600;
00049         interval -= hours * 3600;
00050 
00051         minutes = interval / 60;
00052         interval -= minutes * 60;
00053 
00054         seconds = interval;
00055 
00056         if(days > 0) {
00057                 string = [NSSWF:
00058                         WILS(@"%lu:%0.2lu:%0.2lu:%0.2lu days", @"NSString-WIFoundation: time strings (days, hours, minutes, seconds)"),
00059                         days, hours, minutes, seconds];
00060         }
00061         else if(hours > 0) {
00062                 string = [NSSWF:
00063                         WILS(@"%0.2lu:%0.2lu:%0.2lu hours", @"NSString-WIFoundation: time strings (hours, minutes, seconds)"),
00064                         hours, minutes, seconds];
00065         }
00066         else if(minutes > 0) {
00067                 string = [NSSWF:
00068                         WILS(@"%0.2lu:%0.2lu minutes", @"NSString-WIFoundation: time strings (minutes, seconds)"),
00069                         minutes, seconds];
00070         }
00071         else {
00072                 string = [NSSWF:
00073                         WILS(@"00:%0.2lu seconds", @"NSString-WIFoundation: time string (minutes, seconds)"),
00074                         seconds];
00075         }
00076 
00077         if(past)
00078                 string = [NSSWF:WILS(@"%@ ago", @"NSString-WIFoundation: time string"), string];
00079 
00080         return string;
00081 }
00082 
00083 
00084 
00085 - (NSString *)stringFromTimeIntervalSinceDate:(NSDate *)date {
00086         return [self stringFromTimeInterval:[[NSDate date] timeIntervalSinceDate:date]];
00087 }
00088 
00089 
00090 
00091 - (NSString *)stringFromNumber:(NSNumber *)number {
00092         return [self stringFromTimeInterval:[number doubleValue]];
00093 }
00094 
00095 
00096 
00097 - (NSString *)stringForObjectValue:(id)object {
00098         return [self stringFromTimeInterval:[object doubleValue]];
00099 }
00100 
00101 @end
 All Classes