From 15707f89c5a6f7cb492693371fa1e71f98f2714a Mon Sep 17 00:00:00 2001 From: LtWorf Date: Fri, 8 Aug 2008 14:28:47 +0000 Subject: [PATCH] Added support for dates git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@35 014f5005-505e-4b48-8d0a-63407b615a7c --- CHANGELOG | 3 +- relation.py | 6 ++-- rtypes.py | 75 ++++++++++++++++++++++++++++++++++++++++++++-- samples/dates.tlb | 6 ++++ samples/people.tlb | 2 +- 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 samples/dates.tlb diff --git a/CHANGELOG b/CHANGELOG index 5e3515b..f856cbb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -26,4 +26,5 @@ - Created GUI 0.5 -- Added support for float numbers \ No newline at end of file +- Added support for float numbers +- Added support for dates \ No newline at end of file diff --git a/relation.py b/relation.py index ddf1420..510090b 100644 --- a/relation.py +++ b/relation.py @@ -16,7 +16,7 @@ # # author Salvo "LtWorf" Tomaselli -import rtypes +from rtypes import * class relation (object): '''This objects defines a relation (as a group of consistent tuples) and operations @@ -78,8 +78,10 @@ class relation (object): for j in range(len(self.header.attributes)): if i[j].isdigit(): attributes[self.header.attributes[j]]=int(i[j]) - elif rtypes.rstring(i[j]).isFloat(): + elif rstring(i[j]).isFloat(): attributes[self.header.attributes[j]]=float(i[j]) + elif isDate(i[j]): + attributes[self.header.attributes[j]]=rdate(i[j]) else: attributes[self.header.attributes[j]]=i[j] diff --git a/rtypes.py b/rtypes.py index 9ea84fa..ec26b45 100644 --- a/rtypes.py +++ b/rtypes.py @@ -17,12 +17,83 @@ # author Salvo "LtWorf" Tomaselli '''Custom types for relational algebra''' + +import datetime + class rstring (str): '''String subclass with some custom methods''' def isFloat(self): '''True if the string is a float number, false otherwise''' - lst=['0','1','2','3','4','5','6','7','8','9','.'] + lst=('0','1','2','3','4','5','6','7','8','9','.') for i in self: if i not in lst: return False; - return True; \ No newline at end of file + return True; + +class rdate (object): + '''Represents a date''' + def __init__(self,date): + sep=('-','/','\\') + splitter=None + for i in sep: + if i in date: + splitter=i + break; + elems=date.split(splitter) + + year=int(elems[0]) + month=int(elems[1]) + day=int(elems[2]) + + self.intdate=datetime.date(year,month,day) + self.day= self.intdate.day + self.month=self.intdate.month + self.weekday=self.intdate.weekday() + self.year=self.intdate.year + + def __str__(self): + return self.intdate.__str__() + def __add__(self,days): + res=self.intdate+datetime.timedelta(days) + return rdate(res.__str__()) + + def __eq__(self,other): + return self.intdate==other.intdate + def __ge__(self,other): + return self.intdate>=other.intdate + def __gt__ (self,other): + return self.intdate>other.intdate + def __le__ (self,other): + return self.intdate<=other.intdate + def __lt__ (self,other): + return self.intdatedatetime.MAXYEAR: + return False + if month<1 or month>12: + return False + if day<1 or day >31: + return False + return True \ No newline at end of file diff --git a/samples/dates.tlb b/samples/dates.tlb new file mode 100644 index 0000000..663e142 --- /dev/null +++ b/samples/dates.tlb @@ -0,0 +1,6 @@ +date +2008-12-12 +2007-08-12 +1985-05-09 +1988-4-21 +1992-7-27 \ No newline at end of file diff --git a/samples/people.tlb b/samples/people.tlb index 4fc8277..aea76e8 100644 --- a/samples/people.tlb +++ b/samples/people.tlb @@ -1,5 +1,5 @@ id name chief age -0 jack 0 22.1 +0 jack 0 22 1 carl 0 20 2 john 1 30 3 dean 1 33