API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CPDateFormatter.j
Go to the documentation of this file.
1 /*
2  * CPDateFormatter.j
3  * Foundation
4  *
5  * Created by Alexander Ljungberg.
6  * Copyright 2012, SlevenBits Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #import "Ref.h"
24 
25 
31 
42 @implementation CPDateFormatter : CPFormatter
43 {
44  CPDateFormatterStyle _dateStyle;
45 }
46 
47 - (id)init
48 {
49  if (self = [super init])
50  {
51  _dateStyle = CPDateFormatterShortStyle;
52  }
53 
54  return self;
55 }
56 
57 - (CPString)stringFromDate:(CPDate)aDate
58 {
59  // TODO Add locale support.
60  switch (_dateStyle)
61  {
63  var format = "d/m/Y";
64  return aDate.dateFormat(format);
65  default:
66  return [aDate description];
67  }
68 }
69 
70 - (CPDate)dateFromString:(CPString)aString
71 {
72  if (!aString)
73  return nil;
74  switch (_dateStyle)
75  {
77  var format = "d/m/Y";
78  return Date.parseDate(string, format);
79  default:
80  return Date.parseDate(string);
81  }
82 }
83 
84 - (CPString)stringForObjectValue:(id)anObject
85 {
86  if ([anObject isKindOfClass:[CPDate class]])
87  return [self stringFromDate:anObject];
88  else
89  return [anObject description];
90 }
91 
92 - (CPString)editingStringForObjectValue:(id)anObject
93 {
94  return [self stringForObjectValue:anObject];
95 }
96 
97 - (BOOL)getObjectValue:(id)anObject forString:(CPString)aString errorDescription:(CPString)anError
98 {
99  // TODO Error handling.
100  var value = [self dateFromString:aString];
101  AT_DEREF(anObject, value);
102 
103  return YES;
104 }
105 
106 @end
107 
108 var CPDateFormatterStyleKey = "CPDateFormatterStyle";
109 
111 
112 - (id)initWithCoder:(CPCoder)aCoder
113 {
114  self = [super initWithCoder:aCoder];
115 
116  if (self)
117  {
118  _dateStyle = [aCoder decodeIntForKey:CPDateFormatterStyleKey];
119  }
120 
121  return self;
122 }
123 
124 - (void)encodeWithCoder:(CPCoder)aCoder
125 {
126  [super encodeWithCoder:aCoder];
127 
128  [aCoder encodeInt:_dateStyle forKey:CPDateFormatterStyleKey];
129 }
130 
131 @end
132 
134 
138 - (CPDateFormatterStyle)dateStyle
139 {
140  return _dateStyle;
141 }
142 
146 - (void)setDateStyle:(CPDateFormatterStyle)aValue
147 {
148  _dateStyle = aValue;
149 }
150 
151 @end