Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
WIDateFormatter.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2007-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/WIDateFormatter.h>
00030 
00031 @interface WIDateFormatter(Private)
00032 
00033 - (BOOL)_isRFC3339;
00034 - (void)_setRFC3339:(BOOL)value;
00035 
00036 @end
00037 
00038 
00039 @implementation WIDateFormatter(Private)
00040 
00041 - (BOOL)_isRFC3339 {
00042         return _rfc3339;
00043 }
00044 
00045 
00046 
00047 - (void)_setRFC3339:(BOOL)value {
00048         _rfc3339 = value;
00049 }
00050 
00051 @end
00052 
00053 
00054 
00055 @implementation WIDateFormatter
00056 
00057 + (WIDateFormatter *)dateFormatterForRFC3339 {
00058         static WIDateFormatter          *dateFormatter;
00059         
00060         if(!dateFormatter) {
00061                 dateFormatter = [[self alloc] init];
00062                 [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
00063                 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZ"];
00064                 [dateFormatter _setRFC3339:YES];
00065         }
00066         
00067         return dateFormatter;
00068 }
00069 
00070 
00071 
00072 #pragma mark -
00073 
00074 - (void)setNaturalLanguageStyle:(WIDateFormatterNaturalLanguageStyle)style {
00075         _naturalLanguageStyle = style;
00076 }
00077 
00078 
00079 
00080 - (WIDateFormatterNaturalLanguageStyle)naturalLanguageStyle {
00081         return _naturalLanguageStyle;
00082 }
00083 
00084 
00085 
00086 #pragma mark -
00087 
00088 - (NSString *)stringFromDate:(NSDate *)date {
00089         NSMutableString                 *string;
00090         NSString                                *timeString, *dateString;
00091         NSDateFormatterStyle    style;
00092         NSUInteger                              day, today;
00093         
00094         if(_naturalLanguageStyle != WIDateFormatterNoNaturalLanguageStyle) {
00095                 day = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:date];
00096                 today = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
00097                 
00098                 if(day == today)
00099                         dateString = WILS(@"today", @"WIDateFormatter: this day");
00100                 else if(day == today - 1)
00101                         dateString = WILS(@"yesterday", @"WIDateFormatter: prior day");
00102                 else if(day == today + 1)
00103                         dateString = WILS(@"tomorrow", @"WIDateFormatter: next day");
00104                 else
00105                         return [super stringFromDate:date];
00106                 
00107                 if(_naturalLanguageStyle == WIDateFormatterCapitalizedNaturalLanguageStyle)
00108                         dateString = [dateString capitalizedString];
00109                 else
00110                         dateString = [dateString lowercaseString];
00111                 
00112                 style = [self dateStyle];
00113                 [self setDateStyle:NSDateFormatterNoStyle];
00114                 timeString = [super stringFromDate:date];
00115                 [self setDateStyle:style];
00116                 
00117                 return [NSSWF:@"%@ %@", dateString, timeString];
00118         }
00119         
00120         if([self _isRFC3339]) {
00121                 string = [[super stringFromDate:date] mutableCopy];
00122                 
00123                 [string insertString:@":" atIndex:22];
00124                 
00125                 return [string autorelease];
00126         }
00127         
00128         return [super stringFromDate:date];
00129 }
00130 
00131 @end
 All Classes