我在获取本地时区的文件修改日期时遇到问题。它总是显示服务器默认时区中的日期时间。我得把它改成印度时间。(亚洲/加尔各答(。我安装了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/亚洲/加尔各答