annotate MySQLdb/times.py @ 10:3f4c6af70e52 MySQLdb

Me and PyLint had a knife fight, but PyLint had a gun.
author adustman
date Mon, 26 Feb 2007 02:40:02 +0000
parents b5a377255eea
children aef6ea6a9737
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
1 """times module
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
2
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
3 This module provides some Date and Time classes for dealing with MySQL data.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5 Use Python datetime module to handle date and time columns."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
7 from time import localtime
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
8 from datetime import date, datetime, time, timedelta
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9 from _mysql import string_literal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
10
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
11 Date = date
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
12 Time = time
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
13 TimeDelta = timedelta
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
14 Timestamp = datetime
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16 DateTimeDeltaType = timedelta
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
17 DateTimeType = datetime
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
18
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
19 def DateFromTicks(ticks):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
20 """Convert UNIX ticks into a date instance."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
21 return date(*localtime(ticks)[:3])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23 def TimeFromTicks(ticks):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24 """Convert UNIX ticks into a time instance."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
25 return time(*localtime(ticks)[3:6])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 def TimestampFromTicks(ticks):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28 """Convert UNIX ticks into a datetime instance."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 return datetime(*localtime(ticks)[:6])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 format_TIME = format_DATE = str
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
33 def format_TIMEDELTA(v):
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
34 seconds = int(v.seconds) % 60
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
35 minutes = int(v.seconds / 60) % 60
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
36 hours = int(v.seconds / 3600) % 24
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
37 return '%d %d:%d:%d' % (v.days, hours, minutes, seconds)
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
38
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39 def format_TIMESTAMP(d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
40 return d.strftime("%Y-%m-%d %H:%M:%S")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
41
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
42
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 def DateTime_or_None(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
44 if ' ' in s:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
45 sep = ' '
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46 elif 'T' in s:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
47 sep = 'T'
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
48 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49 return Date_or_None(s)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 d, t = s.split(sep, 1)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53 return datetime(*[ int(x) for x in d.split('-')+t.split(':') ])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54 except:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 return Date_or_None(s)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
57 def TimeDelta_or_None(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 from math import modf
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 h, m, s = s.split(':')
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61 td = timedelta(hours=int(h), minutes=int(m), seconds=int(float(s)),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62 microseconds=int(modf(float(s))[0]*1000000))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
63 if h < 0:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64 return -td
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 return td
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 except:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68 return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
69
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70 def Time_or_None(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
71 from math import modf
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
72 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
73 h, m, s = s.split(':')
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
74 return time(hour=int(h), minute=int(m), second=int(float(s)),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
75 microsecond=int(modf(float(s))[0]*1000000))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
76 except:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77 return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 def Date_or_None(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80 try: return date(*[ int(x) for x in s.split('-',2)])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 except: return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 def DateTime2literal(d, c):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
84 """Format a DateTime object as an ISO timestamp."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
85 return string_literal(format_TIMESTAMP(d),c)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87 def DateTimeDelta2literal(d, c):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 """Format a DateTimeDelta object as a time."""
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
89 return string_literal(format_TIMEDELTA(d),c)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 def mysql_timestamp_converter(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 """Convert a MySQL TIMESTAMP to a Timestamp object."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93 # MySQL>4.1 returns TIMESTAMP in the same format as DATETIME
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
94 if s[4] == '-': return DateTime_or_None(s)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 s = s + "0"*(14-len(s)) # padding
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 s[8:10],s[10:12],s[12:14])))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 try: return Timestamp(*parts)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 except: return None