2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002,2003 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: Time.cpp,v 1.8 2004/07/04 12:32:36 randy Exp $
17 void SpTime::regular()
25 }else if(minute >= 60){
38 }else if(second >= 60){
46 SpValue& SpTime::plus(SpValue& e1, SpValue& e2)
48 SpTime* t1 = e1.asTime();
51 SpInt i = e2.getInt();
52 time = new SpTime(t1->hour,
55 }else if(e2.isTime()){
56 SpTime* t2 = e2.asTime();
57 time = new SpTime(t1->hour + t2->hour,
58 t1->minute + t2->minute,
59 t1->second + t2->second);
61 throw SpException("not time operator+");
64 // static SpValue result;
65 // result.setNewObject(time);
67 return SpObjectResult(time);
70 SpValue& SpTime::minus(SpValue& e1, SpValue& e2)
72 SpTime* t1 = e1.asTime();
75 SpInt i = e2.getInt();
76 time = new SpTime(t1->hour,
79 }else if(e2.isTime()){
80 SpTime* t2 = e2.asTime();
81 time = new SpTime(t1->hour - t2->hour,
82 t1->minute - t2->minute,
83 t1->second - t2->second);
85 throw SpException("not time operator-");
88 // static SpValue result;
89 // result.setNewObject(time);
91 return SpObjectResult(time);
94 SpValue& SpTime::eq(SpValue& e1, SpValue& e2)
97 return SpBoolResult(false);
99 SpTime* t1 = e1.asTime();
100 SpTime* t2 = e2.asTime();
101 return SpBoolResult((t1->hour == t2->hour) && (t1->minute == t2->minute) && (t1->second == t2->second));
104 SpValue& SpTime::ne(SpValue& e1, SpValue& e2)
107 return SpBoolResult(false);
109 SpTime* t1 = e1.asTime();
110 SpTime* t2 = e2.asTime();
111 return SpBoolResult((t1->hour != t2->hour) || (t1->minute != t2->minute) || (t1->second != t2->second));
114 SpValue& SpTime::gt(SpValue& e1, SpValue& e2)
117 throw SpException("unmatch type (Time >)");
119 SpTime* t1 = e1.asTime();
120 SpTime* t2 = e2.asTime();
121 if(t1->hour > t2->hour){
122 return SpBoolResult(true);
123 }else if(t1->hour == t2->hour){
124 if(t1->minute > t2->minute){
125 return SpBoolResult(true);
126 }else if(t1->minute == t2->minute){
127 return SpBoolResult(t1->second > t2->second);
130 return SpBoolResult(false);
133 SpValue& SpTime::ge(SpValue& e1, SpValue& e2)
136 throw SpException("unmatch type (>=)");
138 SpTime* t1 = e1.asTime();
139 SpTime* t2 = e2.asTime();
140 if(t1->hour > t2->hour){
141 return SpBoolResult(true);
142 }else if(t1->hour == t2->hour){
143 if(t1->minute > t2->minute){
144 return SpBoolResult(true);
145 }else if(t1->minute == t2->minute){
146 return SpBoolResult(t1->second >= t2->second);
149 return SpBoolResult(false);
152 SpValue& SpTime::lt(SpValue& e1, SpValue& e2)
155 throw SpException("unmatch type (<)");
157 SpTime* t1 = e1.asTime();
158 SpTime* t2 = e2.asTime();
159 if(t1->hour < t2->hour){
160 return SpBoolResult(true);
161 }else if(t1->hour == t2->hour){
162 if(t1->minute < t2->minute){
163 return SpBoolResult(true);
164 }else if(t1->minute == t2->minute){
165 return SpBoolResult(t1->second < t2->second);
168 return SpBoolResult(false);
171 SpValue& SpTime::le(SpValue& e1, SpValue& e2)
174 throw SpException("unmatch type (<=)");
176 SpTime* t1 = e1.asTime();
177 SpTime* t2 = e2.asTime();
178 if(t1->hour < t2->hour){
179 return SpBoolResult(true);
180 }else if(t1->hour == t2->hour){
181 if(t1->minute < t2->minute){
182 return SpBoolResult(true);
183 }else if(t1->minute == t2->minute){
184 return SpBoolResult(t1->second <= t2->second);
187 return SpBoolResult(false);
190 SpValue& SpTime::toString()
193 SpString* str = new SpString;
194 sprintf(buf, "%d", hour);
196 sprintf(buf, ":%d", minute);
198 sprintf(buf, ":%d", second);
200 // static SpValue val;
201 // val.setNewObject(str);
203 return SpObjectResult(str);
206 void SpTime::assign(SpValue& feature, SpValue& value)
210 if(feature == SymHour){
213 throw SpException("not int(assign time)");
216 }else if(feature == SymMinute){
219 throw SpException("not int(assign time)");
222 }else if(feature == SymSecond){
225 throw SpException("not int(assign time)");
229 throw SpException("no such a feature(assign time)");
234 SpValue& SpTime::prim_now()
241 area = localtime(&t);
246 // static SpValue result;
247 // result.setNewObject(new SpTime(h, m, s));
249 return SpObjectResult(new SpTime(h, m, s));
252 SpValue& SpTime::prim_new(SpValue& v1, SpValue& v2, SpValue& v3)
257 throw SpException("not int (Date.new)");
260 throw SpException("not int (Date.new)");
263 throw SpException("not int (Date.new)");
269 // static SpValue result;
270 // result.setNewObject(new SpTime(h, m, s));
272 return SpObjectResult(new SpTime(h, m, s));
276 SpValue& SpTime::prim_succ(SpValue& self)
279 SpTime* time = self.asTime();
284 SpTime* time2 = new SpTime(h, m, s+1);
287 static SpValue result;
288 result.setNewObject(time2);
292 SpValue& SpTime::prim_pred(SpValue& self)
295 SpTime* time = self.asTime();
300 SpTime* time2 = new SpTime(h, m, s-1);
303 static SpValue result;
304 result.setNewObject(time2);
309 SpValue& SpTime::prim_getHour(SpValue& self)
311 SpTime* p = self.asTime();
312 // static SpValue result;
313 // result.setInt(p->hour);
315 return SpIntResult(p->hour);
318 SpValue& SpTime::prim_getMinute(SpValue& self)
320 SpTime* p = self.asTime();
321 // static SpValue result;
322 // result.setInt(p->minute);
324 return SpIntResult(p->minute);
327 SpValue& SpTime::prim_getSecond(SpValue& self)
329 SpTime* p = self.asTime();
330 // static SpValue result;
331 // result.setInt(p->second);
333 return SpIntResult(p->second);
336 SpValue& SpTime::prim_fromString(SpValue& time_str)
340 SpString* str = time_str.asString();
342 SpValue v = str->split(MakeSpChar(CodeJIS, ':'));
343 SpCons* list = (SpCons*)v.asList();
344 if(list->length() != 3){
345 throw SpException("illegal time format");
348 //h = int_fromString(list->value().asString());
349 temp = list->value();
350 h = int_fromString(temp.asString());
351 temp = list->nextList();
352 list = (SpCons*)temp.asList();
353 //m = int_fromString(list->value().asString());
354 temp = list->value();
355 m = int_fromString(temp.asString());
356 temp = list->nextList();
357 list = (SpCons*)temp.asList();
358 //s = int_fromString(list->value().asString());
359 temp = list->value();
360 s = int_fromString(temp.asString());
362 // static SpValue result;
363 // result.setNewObject(new SpTime(h, m, s));
365 return SpObjectResult(new SpTime(h, m, s));
370 SpValue& SpTime::onMessage(SpValue& rec, SpValue& msg)
372 return TimeMsgHandler(rec, msg);
378 SpNameSpace* pTimeNS = new SpNameSpace;
379 SpValue TimeNS(pTimeNS);
380 PMainNameSpace->internConst(SymTime, TimeNS);
383 SpValue SymNow(new SpSymbol("now"));
384 SpValue PrimNow(new SpPrim0(prim_now));
385 pTimeNS->internPrimProperty(SymNow, NilObject, PrimNow, NilObject);
387 SpValue SymNew(new SpSymbol("new"));
388 SpValue PrimNew(new SpPrim3(prim_new));
389 pTimeNS->internConst(SymNew, PrimNew);
391 SpValue SymFromString(new SpSymbol("fromstring"));
392 SpValue PrimFromString(new SpPrim1(prim_fromString));
393 pTimeNS->internConst(SymFromString, PrimFromString);
396 SpValue GetHour(new SpPrim1(prim_getHour));
397 TimeMsgHandler.append(SymHour, GetHour);
399 SpValue GetMinute(new SpPrim1(prim_getMinute));
400 TimeMsgHandler.append(SymMinute, GetMinute);
402 SpValue GetSecond(new SpPrim1(prim_getSecond));
403 TimeMsgHandler.append(SymSecond, GetSecond);
406 SpValue PrimSucc(new SpPrim1(prim_succ));
407 TimeMsgHandler.append(SymSucc, PrimSucc);
409 SpValue PrimPred(new SpPrim1(prim_pred));
410 TimeMsgHandler.append(SymPred, PrimPred);