Index: trac/ticket/report.py
===================================================================
--- trac/ticket/report.py	(revision 8466)
+++ trac/ticket/report.py	(working copy)
@@ -354,7 +354,8 @@
                 'col': col,
                 'title': col.strip('_').capitalize(),
                 'hidden': False,
-                'asc': False
+                'asc': False,
+		'type': col
             }
 
             if col == sort_col:
@@ -404,6 +405,7 @@
         # Structure the rows and cells:
         #  - group rows according to __group__ value, if defined
         #  - group cells the same way headers are grouped
+        type_warnings = []
         row_groups = []
         prev_group_value = None
         for row_idx, result in enumerate(results):
@@ -438,6 +440,17 @@
                         email_cells.append(cell)
                     elif col == 'realm':
                         realm = value
+
+                    # Fix bug #5806, if results come from a db query, we must check
+                    # the types before sending to the template engine, for now 
+                    # we just prefix the type with 'sql.'
+                    if (col == 'time' 
+                        or col in ['date', 'created', 'modified'] 
+                        or col == 'datetime'):
+                        if not re.match(r"[0-9]*$", value):
+                            header['type'] = "sql." + header['type']
+                            type_warnings.append(col)
+
                     cell_group.append(cell)
                 cell_groups.append(cell_group)
             resource = Resource(realm, row.get('id'))
@@ -457,6 +470,11 @@
                 row_groups = [(None, row_group)]
             row_group.append(row)
 
+        for type_warning in set(type_warnings):
+            add_warning(req, 
+                        _("Using a reserved column name (%s) with "
+                          "an incompatible type" % type_warning))
+
         # Get the email addresses of all known users
         email_map = {}
         if Chrome(self.env).show_email_addresses:
Index: trac/ticket/templates/report_view.html
===================================================================
--- trac/ticket/templates/report_view.html	(revision 8466)
+++ trac/ticket/templates/report_view.html	(working copy)
@@ -119,18 +119,18 @@
 
                 <py:for each="cell in cell_group">
                   <py:if test="not cell.header.hidden">
-                    <py:with vars="col = cell.header.col.strip('_')">
+                    <py:with vars="col = cell.header.col.strip('_'); type = cell.header.type">
                       <py:choose>
 
                         <!--! for the report listing -->
-                        <py:when test="col == 'report'">
+                        <py:when test="type == 'report'">
                           <td class="$col" py:attrs="td_attrs">
                             <a title="View report" href="${href.report(cell.value)}">{$cell.value}</a>
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
 
-                        <py:when test="col == 'title' and report.id == -1">
+                        <py:when test="type == 'title' and report.id == -1">
                           <td class="$col" py:attrs="td_attrs">
                             <a title="View report" href="${href.report(row.id)}">$cell.value</a>
                             <hr py:if="fullrow"/>
@@ -138,14 +138,14 @@
                         </py:when>
 
                         <!--! for the ticket listing -->
-                        <py:when test="col in ('ticket', 'id')">
+                        <py:when test="type in ('ticket', 'id')">
                           <td class="ticket" py:attrs="td_attrs">
                             <a title="View ${row.resource.realm}" href="${url_of(row.resource)}">#$cell.value</a>
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
 
-                        <py:when test="col == 'summary' and row.id">
+                        <py:when test="type == 'summary' and row.id">
                           <td class="$col" py:attrs="td_attrs">
                             <a title="View ${row.resource.realm}" href="${url_of(row.resource)}">$cell.value</a>
                             <hr py:if="fullrow"/>
@@ -153,32 +153,32 @@
                         </py:when>
 
                         <!--! generic fields -->
-                        <py:when test="col == 'time'">
+                        <py:when test="type == 'time'">
                           <td class="date" py:attrs="td_attrs">${cell.value != '' and format_time(int(cell.value)) or '--'}
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
-
-                        <py:when test="col in ('date', 'created', 'modified')">
+                        
+			<py:when test="type in ('date', 'created', 'modified')">
                           <td class="date" py:attrs="td_attrs">${cell.value != '' and format_date(int(cell.value)) or '--'}
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
 
-                        <py:when test="col == 'datetime'">
+                        <py:when test="type == 'datetime'">
                           <td class="date" py:attrs="td_attrs">${cell.value != '' and format_datetime(int(cell.value)) or '--'}
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
 
-                        <py:when test="col == 'description'">
+                        <py:when test="type == 'description'">
                           <td class="$col" py:attrs="td_attrs" xml:space="preserve">
                             ${wiki_to_html(context(row.resource), cell.value)}
                             <hr py:if="fullrow"/>
                           </td>
                         </py:when>
 
-                        <py:when test="col == 'milestone'">
+                        <py:when test="type == 'milestone'">
                           <td class="$col" py:attrs="td_attrs">
                             <a title="View milestone" href="${href.milestone(cell.value)}">$cell.value</a>
                             <hr py:if="fullrow"/>

