5

I have 2 strings which represent:

  1. date- dd/mm/yyyy hh:mm and I need to convert it to timestamp without time zone
  2. start_time/end_tiime- hh:mm:ss and I need to convert it time without time zone

I need to do this in order to add them to a sql query. which looks like this:

sql = '''
    INSERT INTO myTable (date, start_time, end_time)
    VALUES (%s ,%s, %s, %s);'''
params = [timestamp, start_time, end_time]
self.cursor.execute(sql, params)
3
  • Do you use ORM (SQLAlchemy, Autumn, etc) or do you use some dbapi module directly, which one (psycopg2, pg8000, etc)? Commented Oct 24, 2011 at 18:33
  • 1
    I didn't try it but I think you could give psycopg2 Python strings, floats, integers, datetime object and it will convert them by itself to compatible SQL type if not you could register an adapter. There might be complications such as different versions of postgresql treat timestamp with/without time zone types differently, psycopg2 might expose whether postgresql compiled with integers or floats for timestamp, time. You could see what would be sent to the database by calling cursor.mogrify(). Commented Oct 25, 2011 at 18:17
  • related: convert timestamp without time zone returned as float by psycopg2 to datetime Commented Oct 25, 2011 at 20:18

1 Answer 1

2
  1. dd/mm/yyyy hh:mm to timestamp without time zone:

    import time
    
    date_string='24/10/2011 12:43'
    ttuple=time.strptime(date_string,'%d/%m/%Y %H:%M')
    timestamp=time.mktime(ttuple)
    # 1319474580.0
    
  2. hh:mm:ss to time without time zone:

    Can you clarify what kind of object is a time?

Sign up to request clarification or add additional context in comments.

4 Comments

ok. thanks. If I want it as ISO 8601, without the date, just the time?
If you are given a string in hh:mm:ss format as input, what do you want as output?
I've tested here, this is returning a timestamp with timezone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.