我想将UTC时间转换为用户的浏览器时间。 我尝试了一个厌恶,但它只显示我的系统时间。 使用django应用程序。任何研究可以帮助我。
import pytz
from tzlocal import get_localzone
def utc_to_local(utc_dt):#utc_dt that is UTC time
local_tz = get_localzone()
print local_tz #that is display system timezone inplace of USER's timezone
我尝试使用以下代码。
import time
from datetime import datetime
from dateutil import tz
utc = datetime.strptime(str(utc_dt)[:19], '%Y-%m-%d %H:%M:%S')
return time.mktime(utc_dt.timetuple()) * 1000
将utc时间转换为秒,然后使用javascript需要使其成为本地时间。但转换价值不准确,并显示错误的日期。
Javascript代码。
//1449206930000 #Seconds when above code run
//1449226703.79 #that is correct >> time.time() in python = 1449226703.79
#below code give perfact output
var date = new Date(parseInt(1449226703.79, 10) * 1000);
console.log(date);
#below code not working
var date = new Date(parseInt(1449206930000, 10) * 1000);
console.log(date);
此致
答案 0 :(得分:0)
您需要先找到用户时区,才能在当地时区转换时间。
有两种方法可以做到。
1)简单地传递UNIX时间戳然后使用Javascript在浏览器momentjs上以人类可读时间转换它可以帮助您。
function human_time(unixtime, tz) {
var t=moment.unix(unixtime);
return t.tz(tz).format('DD/MM/YY HH:mm:ss');
}
2)Use IP address or HTTP headers to detect the user location然后使用pytz转换您在用户系统时区中的时间,而不是您自己的django / server时区。
def get_user_timezone(request):
return request.visitor.location.timezone
def human_time(dt, tz="Asia/Kolkata"):
try:
tz = pytz.timezone(tz)
except:
tz = pytz.timezone("Asia/Kolkata")
dt = dt.astimezone(tz)
return dt.strftime(r'%d/%m/%Y %H:%M:%S %Z')
def view_get_local_time(request):
tz = get_user_timezone(request) #detect user timezone, base on IP
text = human_time(timezone.now(), tz) #convert server-time to detected timezone
return HttpResponse(text)
注意:第一种方法是最优选的方法。
答案 1 :(得分:0)
Element subtree:
→Application 0x14e7e300: {{0.0, 0.0}, {320.0, 568.0}}, label: 'MyApp'
Window 0x14dbfef0: Main Window, {{0.0, 0.0}, {320.0, 568.0}}
Other 0x14dc0a40: traits: 8589934592, {{0.0, 0.0}, {320.0, 568.0}}
Other 0x14dc0ff0: traits: 8589934592
Other 0x14eb4f50: traits: 8589934592, {{0.0, 0.0}, {320.0, 568.0}}
Other 0x14eb53d0: traits: 8589934592
Button 0x14eb5940: traits: 8589934593, {{12.0, 26.0}, {30.0, 30.0}}, label: 'home hamburger'
Image 0x14eb5f00: traits: 8589934596, {{16.0, 30.0}, {22.0, 22.0}}, identifier: 'home_hamburger'
Other 0x14eb64c0: traits: 8589934592, {{0.0, 109.0}, {320.0, 459.0}}
Other 0x14eb6a20: traits: 8589934592, {{16.0, 167.0}, {288.0, 84.0}}
Image 0x14eb6f80: traits: 8589934596, {{59.0, 184.0}, {201.0, 50.0}}, identifier: 'main_logo'
Other 0x14eb4850: traits: 8589934592, {{16.0, 254.0}, {288.0, 60.0}}
StaticText 0x14eb79e0: traits: 8589934656, {{57.0, 264.0}, {160.0, 40.0}}, label: 'Enter name...'
Image 0x14eb7f90: traits: 8589934596, {{31.0, 275.0}, {18.0, 18.0}}, identifier: 'SearchIcon'
Other 0x14eb8530: traits: 8589934592, {{57.0, 274.0}, {2.0, 20.0}}
Other 0x14eb8ac0: traits: 8589934592, {{16.0, 262.0}, {288.0, 44.0}}
Image 0x14eb9060: traits: 8589934596, {{16.0, 262.0}, {288.0, 44.0}}
SearchField 0x14eb9610: traits: 146029151232, {{24.0, 270.0}, {272.0, 28.0}}
Other 0x14eb9bc0: traits: 8589934592
Other 0x14eba170: traits: 8589934592
Window 0x14eba730: {{0.0, 0.0}, {320.0, 568.0}}
StatusBar 0x14ebaca0: {{0.0, 0.0}, {320.0, 20.0}}
Other 0x14ebb220: {{0.0, 0.0}, {320.0, 20.0}}
Other 0x14ebb7b0: {{0.0, 0.0}, {320.0, 20.0}}
Other 0x14ebbd50: traits: 8388608, {{6.0, 0.0}, {35.0, 20.0}}, label: '3 of 5 bars, signal strength'
Other 0x14ebc330: traits: 8388608, {{44.0, 0.0}, {73.0, 20.0}}, label: 'vodafone UK network'
Other 0x14ebc8c0: traits: 8388608, {{122.0, 0.0}, {13.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
Other 0x14ebce50: traits: 8389120, {{146.0, 0.0}, {32.0, 20.0}}, label: '11:34'
Other 0x14ebd3d0: traits: 8388608, {{282.0, 0.0}, {33.0, 20.0}}, label: '100% battery power, Charging'
Path to element:
→Application 0x14e7e300: {{0.0, 0.0}, {320.0, 568.0}}, label: 'MyApp'
Query chain:
→Find: Target Application 0x14ea2c00
Output: {
Application 0x14e7e300: {{0.0, 0.0}, {320.0, 568.0}}, label: 'MyApp'
}
感谢您的帮助。