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

  • SEARCH
  • Home
  • 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 697683
In Process

The Archive Base Latest Questions

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

I am working with SQLAlchemy, and I’m not yet sure which database I’ll use

  • 0

I am working with SQLAlchemy, and I’m not yet sure which database I’ll use under it, so I want to remain as DB-agnostic as possible. How can I store a timezone-aware datetime object in the DB without tying myself to a specific database? Right now, I’m making sure that times are UTC before I store them in the DB, and converting to localized at display-time, but that feels inelegant and brittle. Is there a DB-agnostic way to get a timezone-aware datetime out of SQLAlchemy instead of getting naive datatime objects out of the DB?

  • 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. Editorial Team
    Editorial Team
    2026-05-14T03:11:36+00:00Added an answer on May 14, 2026 at 3:11 am

    There is a timezone parameter to DateTime column time, so there is no problem with storing timezone-aware datetime objects. However I found convenient to convert stored datetime to UTC automatically with simple type decorator:

    from sqlalchemy import types
    from datetime import datetime, timezone
    
    class UTCDateTime(types.TypeDecorator):
    
        impl = types.DateTime
    
        def process_bind_param(self, value, engine):
            if value is None:
                return
            if value.utcoffset() is None:
                raise ValueError(
                    'Got naive datetime while timezone-aware is expected'
                )
            return value.astimezone(timezone.utc)
    
        def process_result_value(self, value, engine):
            if value is not None:
                return value.replace(tzinfo=timezone.utc)
    

    Note, that is behaves nicely when you use naive datetime by accident (meaning it will raise a ValueError).

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

Sidebar

Related Questions

Working with stl:list and stl::vector types under interrupt handlers I want to avoid malloc()
I am working with an SQLAlchemy database. I have a simple schema with columns
I'm working on writing a small WSGI application using Bottle and SqlAlchemy and am
Working with python interactively, it's sometimes necessary to display a result which is some
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
I am trying to use pyramid beaker in Pyramid framework and its just not
I am working on my first pylons + SQLAlchemy app (I'm new to both).
I am currently working on a pyramid system that uses sqlalchemy. This system will
I am working on a web app based on Flask and SQLAlchemy. Most of
Working on dom html . I want to convert node value to string: $html

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.