File: mxdatetimes.py

package info (click to toggle)
python-mysqldb 1.2.1c2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 336 kB
  • ctags: 694
  • sloc: ansic: 2,507; python: 1,085; sh: 129; makefile: 65
file content (50 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Use mx.DateTime to handle date and time columns."""

from time import strftime, localtime

try:
    # new packaging
    from mx.DateTime import Date, Time, Timestamp, ISO, \
        DateTimeType, DateTimeDeltaType
except ImportError:
    # old packaging, deprecated
    from DateTime import Date, Time, Timestamp, ISO, \
        DateTimeType, DateTimeDeltaType

def DateFromTicks(ticks):
    """Convert UNIX ticks into a mx.DateTime.Date."""
    return Date(*localtime(ticks)[:3])

def TimeFromTicks(ticks):
    """Convert UNIX ticks into a mx.DateTime.Time."""
    return Time(*localtime(ticks)[3:6])

def TimestampFromTicks(ticks):
    """Convert UNIX ticks into a mx.DateTime.Timestamp."""
    return Timestamp(*localtime(ticks)[:6])

def format_DATE(d):
    """Format a DateTime object as an ISO date."""
    return d.strftime("%Y-%m-%d")

def format_TIME(d):
    """Format a DateTime object as a time value."""
    return d.strftime("%d %H:%M:%S")

def format_TIMESTAMP(d):
    """Format a DateTime object as an ISO timestamp."""
    return d.strftime("%Y-%m-%d %H:%M:%S")

def DateTime_or_None(s):
    try: return ISO.ParseDateTime(s)
    except: return None

def TimeDelta_or_None(s):
    try: return ISO.ParseTimeDelta(s)
    except: return None

Time_or_None = TimeDelta_or_None

def Date_or_None(s):
    try: return ISO.ParseDate(s)
    except: return None