Thread: [(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]

hi all,

i want to create an timestamp with to_timestamp() and i get always an output like this

[(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]

here is the code


-------------------------------------------%<----------------------------------------------------------------------------------------------------------->%-------------------------------------------------------

D:\>C:\Python27_64bit\Scripts\ipython.exe
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 0.13 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.


In [2]: import psycopg20
In [3]: conn = psycopg2.connect("port=5432 dbname='xxxDB' user='xxxUser' host='localhost' password='xxx'")
In [4]: cur = conn.cursor()
In [5]: sqlString=cur.mogrify("SELECT to_timestamp (%s, %s)",
   ...: ['130729.0902',
   ...:  'YYMMDD.HH24MI'
   ...: ])
In [6]: print(sqlString)
SELECT to_timestamp ('130729.0902', 'YYMMDD.HH24MI')
In [7]: cur.execute(sqlString)
In [8]: rows = cur.fetchall()
In [9]: print(rows)
[(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]
In [10]: print(psycopg2.__version__)
2.5.1 (dt dec pq3 ext)


-------------------------------------------%<----------------------------------------------------------------------------------------------------------->%-------------------------------------------------------

wihin psql the statement works well


-------------------------------------------%<----------------------------------------------------------------------------------------------------------->%-------------------------------------------------------

xxxDB=> SELECT to_timestamp ('130729.0902', 'YYMMDD.HH24MI');
      to_timestamp
------------------------
 2013-07-29 09:02:00+00
(1 Zeile)


-------------------------------------------%<----------------------------------------------------------------------------------------------------------->%-------------------------------------------------------


I get the same output at windows, mac os and ubuntu, at different python versions (2.6, 2.7), different postgresql
versions(8.4, 9.1, 9.2) and different psycopg versions (2.0.13, 2.5, 2.5.1)  


is this a problem of my code or a bug?

thanks for your help
christian


On Thu, Aug 01, 2013 at 09:24:11AM +0000, Christian.Strobl@dlr.de wrote:

> i want to create an timestamp with to_timestamp() and i get always an output like this
>
> [(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]

...

> is this a problem of my code or a bug?

Neither. It is normal behaviour.

I suppose you are expecting something else but you'd need to
explain that.

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


On Thu, Aug 08/01/13, 2013 at 09:24:11AM +0000, Christian.Strobl@dlr.de wrote:
>
> hi all,
>
> i want to create an timestamp with to_timestamp() and i get always an output like this
>
> [(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]
>

Are you looking to get back a string? Cast to TEXT first:

SELECT CAST(to_timestamp ('130729.0902', 'YYMMDD.HH24MI') AS TEXT);

-Ryan


On Thu, Aug 01, 2013 at 01:25:53PM +0000, Christian.Strobl@dlr.de wrote:

> thanks a lot, i see now. i get a python datetime object, not a result string. i was to fixed to my database way of
thinking
>
> with
>
> In [6]: rows[0][0].isoformat()
> Out[6]: '2013-07-29T09:02:00+00:00'
>
> i get what i want.
>
> an additional question: for more complex queries it is  a
> little bit time consuming to change all queries with
> mogrify. do you know if it is possible to send raw sql to
> the database

No need for mogrify ??

    query = "SELECT ...;"
    conn = psycopg2.connection(...)
    curs = conn.cursor()
    curs.execute(query)
    results = curs.fetch*()

Maybe you want to read the documentation ?

> and to get only the output from the database
> back without any kind of python object creation.

You could cast every SELECTed column to TEXT as
has been suggested already but why ? You could also
just <'%s' % object> it in Python, or some such measure.

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


hi karsten,

thanks a lot, i see now. i get a python datetime object, not a result string. i was to fixed to my database way of
thinking

with

In [6]: rows[0][0].isoformat()
Out[6]: '2013-07-29T09:02:00+00:00'

i get what i want.

an additional question: for more complex queries it is  a little bit time consuming to change all queries with mogrify.
doyou know if it is possible to send raw sql to the database and to get only the output from the database back without
anykind of python object creation. 

kind regards
christian




-----Ursprüngliche Nachricht-----
Von: psycopg-owner@postgresql.org [mailto:psycopg-owner@postgresql.org] Im Auftrag von Karsten Hilbert
Gesendet: Donnerstag, 1. August 2013 13:11
An: psycopg@postgresql.org
Betreff: Re: [psycopg] [(datetime.datetime(2013, 7, 29, 9, 2, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0,
name=None)),)]

On Thu, Aug 01, 2013 at 09:24:11AM +0000, Christian.Strobl@dlr.de wrote:

> i want to create an timestamp with to_timestamp() and i get always an
> output like this
>
> [(datetime.datetime(2013, 7, 29, 9, 2,
> tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)),)]

...

> is this a problem of my code or a bug?

Neither. It is normal behaviour.

I suppose you are expecting something else but you'd need to explain that.

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


--
Sent via psycopg mailing list (psycopg@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/psycopg