Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:09:43+00:00 2026-05-11T03:09:43+00:00

SQLAlchemy’s DateTime type allows for a timezone=True argument to save a non-naive datetime object

  • 0

SQLAlchemy’s DateTime type allows for a timezone=True argument to save a non-naive datetime object to the database, and to return it as such. Is there any way to modify the timezone of the tzinfo that SQLAlchemy passes in so it could be, for instance, UTC? I realize that I could just use default=datetime.datetime.utcnow; however, this is a naive time that would happily accept someone passing in a naive localtime-based datetime, even if I used timezone=True with it, because it makes local or UTC time non-naive without having a base timezone to normalize it with. I have tried (using pytz) to make the datetime object non-naive, but when I save this to the DB it comes back as naive.

Note how datetime.datetime.utcnow does not work with timezone=True so well:

import sqlalchemy as sa from sqlalchemy.sql import select import datetime  metadata = sa.MetaData('postgres://user:pass@machine/db')  data_table = sa.Table('data', metadata,     sa.Column('id',   sa.types.Integer, primary_key=True),     sa.Column('date', sa.types.DateTime(timezone=True), default=datetime.datetime.utcnow) )  metadata.create_all()  engine = metadata.bind conn = engine.connect() result = conn.execute(data_table.insert().values(id=1))  s = select([data_table]) result = conn.execute(s) row = result.fetchone() 

(1, datetime.datetime(2009, 1, 6, 0, 9, 36, 891887))

row[1].utcoffset() 

datetime.timedelta(-1, 64800) # that’s my localtime offset!!

datetime.datetime.now(tz=pytz.timezone('US/Central')) 

datetime.timedelta(-1, 64800)

datetime.datetime.now(tz=pytz.timezone('UTC')) 

datetime.timedelta(0) #UTC

Even if I change it to explicitly use UTC:

…

data_table = sa.Table('data', metadata,     sa.Column('id',   sa.types.Integer, primary_key=True),     sa.Column('date', sa.types.DateTime(timezone=True), default=datetime.datetime.now(tz=pytz.timezone('UTC'))) )  row[1].utcoffset() 

…

datetime.timedelta(-1, 64800) # it did not use the timezone I explicitly added

Or if I drop the timezone=True:

…

data_table = sa.Table('data', metadata,     sa.Column('id',   sa.types.Integer, primary_key=True),     sa.Column('date', sa.types.DateTime(), default=datetime.datetime.now(tz=pytz.timezone('UTC'))) )  row[1].utcoffset() is None 

…

True # it didn’t even save a timezone to the db this time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:09:44+00:00Added an answer on May 11, 2026 at 3:09 am

    http://www.postgresql.org/docs/8.3/interactive/datatype-datetime.html#DATATYPE-TIMEZONES

    All timezone-aware dates and times are stored internally in UTC. They are converted to local time in the zone specified by the timezone configuration parameter before being displayed to the client.

    The only way to store it with postgresql is to store it separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're not showing the whole code, so the simplest solution… May 13, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer Syntactically, the dollar sign itself means nothing -- to the… May 13, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer Simply loop through your places to find the proper item.… May 13, 2026 at 3:13 pm

Related Questions

SQLAlchemy's DateTime type allows for a timezone=True argument to save a non-naive datetime object
SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = 'as' id = Column(Integer,
In SQLAlchemy, it appears I'm supposed to pass an expression to filter() in certain
I'm using SQLAlchemy 0.5rc, and I'd like to add an automatic filter to a
What is the sqlalchemy equivalent column type for 'money' and 'OID' column types in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.