#
#  Copyright (c) 1998, 1999, Digital Creations, Fredericksburg, VA, USA.  
#  All rights reserved.
#  
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#  
#    o Redistributions of source code must retain the above copyright
#      notice, this list of conditions, and the disclaimer that follows.
#  
#    o Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions, and the following disclaimer in
#      the documentation and/or other materials provided with the
#      distribution.
#  
#    o Neither the name of Digital Creations nor the names of its
#      contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.
#  
#  
#  THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
#  IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL
#  CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
#  DAMAGE.
#
#  $Id: dbi.py,v 1.4 1999/01/14 17:38:00 jim Exp $
#
#  If you have questions regarding this software,
#  contact:
# 
#    Digital Creations L.C.  
#    info@digicool.com
# 
#    (540) 371-6909
#
##############################################################################
"""dbi module of helper objects"""

from time import localtime, time, mktime
import string


class dbiDate:

    def __init__(self, y=None, mo=None, d=None, h=None, m=None, s=None,
                 StringType=type(''),
                 TupleType=type(()),
                 _ts=localtime(time())[6:],
                 ):
        if d is not None:
            self.year=y
            self.month=mo
            self.day=d
            if h is not None:
                self.hour=h
                if s is not None:
                    self.second=s
                    self.minute=m
                elif m is not None:
                    self.second=0
                    self.minute=m
            else:
                self.hour=0
                self.second=0
                self.minute=0
        elif y is None:
            (self.year, self.month, self.day,
             self.hour, self.minute, self.second) = localtime(time())[:6]
        elif type(y) is StringType:
            atoi=string.atoi
            [d, t] = string.split(string.strip(y)+' 0:0:0')[:2]
            self.year, self.month, self.day = tuple(
                map(lambda s: string.atoi(s), string.split(d,'-')))
            self.hour, self.minute, self.second = tuple(
                map(lambda s: string.atoi(s), string.split(t,':')))
                 
        else:
            (self.year, self.month, self.day,
             self.hour, self.minute, self.second) = localtime(y)[:6]

    def __str__(self):
        return ("%.4d-%.2d-%.2d %.2d:%.2d:%.2d" %
                (self.year, self.month, self.day,
                 self.hour, self.minute, self.second))

    def __repr__(self): return "dbiDate('%s')" % self

    # modified by WKP to get around year 2038 problem
    def __float__(self):
	if self.year < 2035:
	        return float(mktime(self.year, self.month, self.day,
        	                    self.hour, self.minute, self.second,
                	            0,0,0))
	else: # approximate - too bad!
	        return float(mktime(2035, self.month, self.day,
        	                    self.hour, self.minute, self.second,
                	            0,0,0)) \
		+ (self.year - 2035) * 365.2425 * 24 * 3600

    # added by RDW, modified by WKP
    def __cmp__(self, other):
	try: # both dates
	    return cmp(float(self), float(other))
        except: # other is not a dbiDate
            return cmp(float(self), other)

    # added by WKP
    def __hash__(self):
	return hash(float(self))

class dbiRaw: 

    def __init__(self, v):
        self._v=v
    
# Column types
STRING = "STRING"
RAW    = "RAW"
NUMBER = "NUMBER"
DATE   = "DATE"
ROWID  = "ROWID"
