File: stringtimes.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 (38 lines) | stat: -rw-r--r-- 1,216 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
"""Use strings to handle date and time columns as a last resort."""

from time import strftime, localtime

DateTimeDeltaType = "DateTimeDeltaType"
DateTimeType = "DateTimeType"

def Date(year, month, day):
    """Construct an ISO date string."""
    return "%04d-%02d-%02d" % (year, month, day)

def Time(hour, min, sec):
    """Construct a TIME string."""
    return "%02d:%02d:%02d" % (hour, min, sec)

def Timestamp(year, month, day, hour, min, sec):
    """Construct an ISO timestamp."""
    return "%04d-%02d-%02d %02d:%02d:%02d" % \
           (year, month, day, hour, min, sec)

def DateFromTicks(ticks):
    """Convert UNIX ticks to ISO date format."""
    return strftime("%Y-%m-%d", localtime(ticks))

def TimeFromTicks(ticks):
    """Convert UNIX ticks to time format."""
    return strftime("%H:%M:%S", localtime(ticks))

def TimestampFromTicks(ticks):
    """Convert UNIX ticks to ISO timestamp format."""
    return strftime("%Y-%m-%d %H:%M:%S", localtime(ticks))

def format_DATE(d):
    """Format a date as a date (does nothing, you don't have mx.DateTime)"""
    return d

format_TIME = format_TIMESTAMP = format_DATE
Time_or_None = TimeDelta_or_None = Date_or_None = DateTime_or_None = format_DATE