问题描述
我在使用本地时区获取文件修改日期时遇到问题。它始终显示服务器默认时区中的日期时间。我必须将其更改为印度时间。(亚洲/加尔各答)。 我已经安装了 pytz,但我不知道如何使用它来更改服务器时区并相应地获取文件修改时间。请建议我。这是我的代码:
from datetime import datetime
import pytz
import os.path,time
from flask import Flask,render_template
app = Flask(__name__)
@app.route("/")
def home():
f=os.path.abspath("templates/index.html")
fts=time.gmtime(os.path.getmtime(f))
dt= time.strftime('%d %b %Y %l:%M %p',fts)
mdt=str(dt)
return render_template('index.html',mdate=mdt)
if __name__ == "__main__":
app.run(debug=True)
解决方法
这对你有用吗:
from datetime import datetime
from pytz import timezone
import os.path,time
from flask import Flask,render_template
app = Flask(__name__)
@app.route("/<tz1>/<tz2>")
def home():
f=os.path.abspath("templates/index.html")
tz = timezone(tz1+'/'+tz2)
dt= datetime.now(tz)
mdt=str(dt)
return render_template('index.html',mdate=mdt)
if __name__ == "__main__":
app.run(debug=True)
现在您可以在这里访问它:
127.0.0.1:5000/亚洲/加尔各答