"Fossies" - the Fresh Open Source Software Archive

Member "viewvc-1.2.1/lib/dbi.py" (26 Mar 2020, 2188 Bytes) of package /linux/misc/viewvc-1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "dbi.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.28_vs_1.2.1.

    1 # -*-python-*-
    2 #
    3 # Copyright (C) 1999-2020 The ViewCVS Group. All Rights Reserved.
    4 #
    5 # By using this file, you agree to the terms and conditions set forth in
    6 # the LICENSE.html file which can be found at the top level of the ViewVC
    7 # distribution or at http://viewvc.org/license-1.html.
    8 #
    9 # For more information, visit http://viewvc.org/
   10 #
   11 # -----------------------------------------------------------------------
   12 
   13 import sys
   14 import time
   15 import types
   16 import re
   17 import calendar
   18 import MySQLdb
   19 
   20 # set to 1 to store commit times in UTC, or 0 to use the ViewVC machine's
   21 # local timezone. Using UTC is recommended because it ensures that the 
   22 # database will remain valid even if it is moved to another machine or the host
   23 # computer's time zone is changed. UTC also avoids the ambiguity associated
   24 # with daylight saving time (for example if a computer in New York recorded the
   25 # local time 2002/10/27 1:30 am, there would be no way to tell whether the
   26 # actual time was recorded before or after clocks were rolled back). Use local
   27 # times for compatibility with databases used by ViewCVS 0.92 and earlier 
   28 # versions.
   29 utc_time = 1
   30 
   31 def DateTimeFromTicks(ticks):
   32   """Return a MySQL DATETIME value from a unix timestamp"""
   33 
   34   if utc_time:
   35     t = time.gmtime(ticks)
   36   else:
   37     t = time.localtime(ticks)
   38   return "%04d-%02d-%02d %02d:%02d:%02d" % t[:6]
   39 
   40 _re_datetime = re.compile('([0-9]{4})-([0-9][0-9])-([0-9][0-9]) '
   41                           '([0-9][0-9]):([0-9][0-9]):([0-9][0-9])')
   42 
   43 def TicksFromDateTime(datetime):
   44   """Return a unix timestamp from a MySQL DATETIME value"""
   45 
   46   if type(datetime) == types.StringType:
   47     # datetime is a MySQL DATETIME string
   48     matches = _re_datetime.match(datetime).groups()
   49     t = tuple(map(int, matches)) + (0, 0, 0)
   50   elif hasattr(datetime, "timetuple"):
   51     # datetime is a Python >=2.3 datetime.DateTime object
   52     t = datetime.timetuple()
   53   else:
   54     # datetime is an eGenix mx.DateTime object
   55     t = datetime.tuple()
   56 
   57   if utc_time:
   58     return calendar.timegm(t)
   59   else:
   60     return time.mktime(t[:8] + (-1,))
   61     
   62 def connect(host, port, user, passwd, db):
   63     return MySQLdb.connect(host=host, port=port, user=user, passwd=passwd, db=db)