Is there a proper method to easily converts UTC now time to GMT+7 now time in Python’s datetime?
I need to apply this to my db models in Flask.
Here is my models.py
from flask import Flask from flask_sqlalchemy import SQLAlchemy from datetime import datetime app = Flask(__name__) db = SQLAlchemy(app) class Item(db.Model): itemID = db.Column(db.Integer, primary_key=True) itemName = db.Column(db.String(64), index=True, unique=True) itemPrice = db.Column(db.Integer, index=True) itemTimestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) def __repr__(self): return '<Item {}>'.format(self.itemName)
Any advices would be appreciated. Thank in advance.
Advertisement
Answer
Finally, I can address this issue by creating a new method called gmt7now()
with the help of pytz
module, and below is the code:
import datetime, pytz def gmt7now(dt_utc): dt_utc = datetime.datetime.utcnow() # utcnow class method dt_rep = dt_utc.replace(tzinfo=pytz.UTC) # replace method dt_gmt7 = dt_rep.astimezone(pytz.timezone("Asia/Jakarta")) # astimezone method [gmt++7] return dt_gmt7