Code

Some cleanup.
[roundup.git] / roundup / date.py
index e7002aec0bec76fa258f4ef83c2263ee54b46996..ef65d87f86fb7c1c099777fc8910f11f13094d26 100644 (file)
@@ -1,4 +1,21 @@
-# $Id: date.py,v 1.2 2001-07-22 12:09:32 richard Exp $
+#
+# Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
+# This module is free software, and you may redistribute it and/or modify
+# under the same terms as Python, so long as this copyright message and
+# disclaimer are retained in their original form.
+#
+# IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
+# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
+# OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
+# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
+# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+# 
+# $Id: date.py,v 1.13 2001-09-18 22:58:37 richard Exp $
 
 import time, re, calendar
 
@@ -54,21 +71,20 @@ class Date:
         >>> Date("14:25", -5)
         <Date 2000-06-25.19:25:00>
     '''
-    isDate = 1
-
-    def __init__(self, spec='.', offset=0, set=None):
+    def __init__(self, spec='.', offset=0):
         """Construct a date given a specification and a time zone offset.
 
           'spec' is a full date or a partial form, with an optional
-                 added or subtracted interval.
+                 added or subtracted interval. Or a date 9-tuple.
         'offset' is the local time zone offset from GMT in hours.
         """
-        if set is None:
+        if type(spec) == type(''):
             self.set(spec, offset=offset)
         else:
+            y,m,d,H,M,S,x,x,x = spec
+            ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0))
             self.year, self.month, self.day, self.hour, self.minute, \
-                self.second, x, x, x = set
-        self.offset = offset
+                self.second, x, x, x = time.gmtime(ts)
 
     def applyInterval(self, interval):
         ''' Apply the interval to this date
@@ -90,7 +106,7 @@ class Date:
             self.hour + other.sign * other.hour,
             self.minute + other.sign * other.minute,
             self.second + other.sign * other.second, 0, 0, 0)
-        return Date(set = time.gmtime(calendar.timegm(t)))
+        return Date(time.gmtime(calendar.timegm(t)))
 
     # XXX deviates from spec to allow subtraction of dates as well
     def __sub__(self, other):
@@ -98,7 +114,7 @@ class Date:
              1. an interval from this date to produce another date.
              2. a date from this date to produce an interval.
         """
-        if other.isDate:
+        if isinstance(other, Date):
             # TODO this code will fall over laughing if the dates cross
             # leap years, phases of the moon, ....
             a = calendar.timegm((self.year, self.month, self.day, self.hour,
@@ -128,7 +144,7 @@ class Date:
              self.hour - other.sign * other.hour,
              self.minute - other.sign * other.minute,
              self.second - other.sign * other.second, 0, 0, 0)
-        return Date(set = time.gmtime(calendar.timegm(t)))
+        return Date(time.gmtime(calendar.timegm(t)))
 
     def __cmp__(self, other):
         """Compare this date to another date."""
@@ -139,8 +155,8 @@ class Date:
 
     def __str__(self):
         """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format."""
-        return time.strftime('%Y-%m-%d.%T', (self.year, self.month,
-            self.day, self.hour, self.minute, self.second, 0, 0, 0))
+        return '%4d-%02d-%02d.%02d:%02d:%02d'%(self.year, self.month, self.day,
+            self.hour, self.minute, self.second)
 
     def pretty(self):
         ''' print up the date date using a pretty format...
@@ -163,22 +179,25 @@ class Date:
 
         # get the current date/time using the offset
         y,m,d,H,M,S,x,x,x = time.gmtime(time.time())
-        ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0))
-        self.year, self.month, self.day, self.hour, self.minute, \
-            self.second, x, x, x = time.gmtime(ts)
 
+        # override year, month, day parts
         if info['m'] is not None and info['d'] is not None:
-            self.month = int(info['m'])
-            self.day = int(info['d'])
-            if info['y'] is not None:
-                self.year = int(info['y'])
-            self.hour = self.minute = self.second = 0
+            m = int(info['m'])
+            d = int(info['d'])
+            if info['y'] is not None: y = int(info['y'])
+            H = M = S = 0
 
+        # override hour, minute, second parts
         if info['H'] is not None and info['M'] is not None:
-            self.hour = int(info['H'])
-            self.minute = int(info['M'])
-            if info['S'] is not None:
-                self.second = int(info['S'])
+            H = int(info['H']) - offset
+            M = int(info['M'])
+            S = 0
+            if info['S'] is not None: S = int(info['S'])
+
+        # now handle the adjustment of hour
+        ts = calendar.timegm((y,m,d,H,M,S,0,0,0))
+        self.year, self.month, self.day, self.hour, self.minute, \
+            self.second, x, x, x = time.gmtime(ts)
 
         if info['o']:
             self.applyInterval(Interval(info['o']))
@@ -193,6 +212,9 @@ class Date:
         self.year, self.month, self.day, self.hour, self.minute, \
             self.second, x, x, x = time.gmtime(calendar.timegm(t))
 
+    def get_tuple(self):
+        return (self.year, self.month, self.day, self.hour, self.minute,
+            self.second, 0, 0, 0)
 
 class Interval:
     '''
@@ -215,8 +237,6 @@ class Interval:
         >>> Date(". + 2d") - Interval("3w")
         <Date 2000-06-07.00:34:02>
     '''
-    isInterval = 1
-
     def __init__(self, spec, sign=1):
         """Construct an interval given a specification."""
         if type(spec) == type(''):
@@ -295,8 +315,19 @@ class Interval:
             < 1 day
             otherwise, return None (so a full date may be displayed)
         '''
-        if self.year or self.month or self.day > 5:
+        if self.year or self.month > 2:
             return None
+        if self.month or self.day > 13:
+            days = (self.month * 30) + self.day
+            if days > 28:
+                if int(days/30) > 1:
+                    return '%s months'%int(days/30)
+                else:
+                    return '1 month'
+            else:
+                return '%s weeks'%int(days/7)
+        if self.day > 7:
+            return '1 week'
         if self.day > 1:
             return '%s days'%self.day
         if self.day == 1 or self.hour > 12:
@@ -316,11 +347,15 @@ class Interval:
             return '1 minute'
         if self.minute < 15:
             return '%s minutes'%self.minute
-        quart = self.minute/15
+        quart = int(self.minute/15)
         if quart == 2:
             return '1/2 an hour'
         return '%s/4 hour'%quart
 
+    def get_tuple(self):
+        return (self.year, self.month, self.day, self.hour, self.minute,
+            self.second)
+
 
 def test():
     intervals = ("  3w  1  d  2:00", " + 2d", "3w")
@@ -344,6 +379,45 @@ if __name__ == '__main__':
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.12  2001/08/17 03:08:11  richard
+# fixed prettification of intervals of 1 week
+#
+# Revision 1.11  2001/08/15 23:43:18  richard
+# Fixed some isFooTypes that I missed.
+# Refactored some code in the CGI code.
+#
+# Revision 1.10  2001/08/07 00:24:42  richard
+# stupid typo
+#
+# Revision 1.9  2001/08/07 00:15:51  richard
+# Added the copyright/license notice to (nearly) all files at request of
+# Bizar Software.
+#
+# Revision 1.8  2001/08/05 07:46:12  richard
+# Changed date.Date to use regular string formatting instead of strftime -
+# win32 seems to have problems with %T and no hour... or something...
+#
+# Revision 1.7  2001/08/02 00:27:04  richard
+# Extended the range of intervals that are pretty-printed before actual dates
+# are displayed.
+#
+# Revision 1.6  2001/07/31 09:54:18  richard
+# Fixed the 2.1-specific gmtime() (no arg) call in roundup.date. (Paul Wright)
+#
+# Revision 1.5  2001/07/29 07:01:39  richard
+# Added vim command to all source so that we don't get no steenkin' tabs :)
+#
+# Revision 1.4  2001/07/25 04:09:34  richard
+# Fixed offset handling (shoulda read the spec a little better)
+#
+# Revision 1.3  2001/07/23 07:56:05  richard
+# Storing only marshallable data in the db - no nasty pickled class references.
+#
+# Revision 1.2  2001/07/22 12:09:32  richard
+# Final commit of Grande Splite
+#
 # Revision 1.1  2001/07/22 11:58:35  richard
 # More Grande Splite
 #
+#
+# vim: set filetype=python ts=4 sw=4 et si