我正在处理的应用程序已作为目标用户USA。而且(你可能知道)在美国有不止一个时区。所以我有些疑惑:
DateTime
数据以显示正确的时间
每个用户?user.event.created > datetime.now()
)? .now()
将使用哪个时区?settings.py
文件中设置什么TIME_ZONE。这是一个API,通过JS获取用户的时区不是一个选项。我通过TokenAuthentication获得当前用户。
答案 0 :(得分:1)
对UTC
使用settings.py
并从Javascript中获取他们的时区偏移量:
var utcOffset = -((new Date()).getTimezoneOffset()) / 60;
e.g。现在洛杉矶utcOffset == -7
不幸的是,这不会考虑以后的夏令时更改(偏移更改为-8
),因此您可能必须在检索时将其弄清楚才能获得Pacific/Los_Angeles
。否则,您可以随时以注册形式询问用户是否对您的业务非常重要。
编辑:由于您使用的是API,因此您还可以尝试使用IPInfoDB API根据客户端IP地址进行地理定位。它并不总是完全准确,但几乎总是足以获得正确的时区。
答案 1 :(得分:0)
不要使用datetime.now(),而是使用timezone模块中的now()函数。
from django.utils import timezone
now = timezone.now()
Django将确定您所在的时区并相应地进行比较。
user.event.created > timezone.now()
https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/
答案 2 :(得分:0)
解决方案是始终将unix时间戳存储在数据库中。您可以使用time.time()
生成。这意味着您的模型应该有一个Floatfield(甚至BigIntegerField,具体取决于所需的准确性)。
您的模板应按原样显示数值。然后你需要一点点javascript来将unix时间戳转换为日期时间。
new Date(unix_timestamp);
答案 3 :(得分:0)
您的Web应用程序的用户可能处于不同的时区,因此有必要转换为适当的时区。您可以创建一个中间件并使用activate函数来设置适当的时区。您可以通过在目标网页中对 Free IP Geolocation API 进行ajax api调用来获取客户端时区,时区值可以保存在cookie变量中,以后可以在中间件中进行访问
landingpage.html
create-link-with one-of potentialHomes [ set color red
middleware.py
undirected-link-breed [own-links own-link]
undirected-link-breed [rent-links rent-link]
breed [city-centers city-center]
breed [households household]
households-own
[
age
money
income
monthly-income
consumption
monthly-consumption
hh-size race
preference
net-income
net-monthly-income
myHouse
]
breed [houses house]
houses-own
[
cost
down-payment
mortgage-payment
rent
rent-premium
rooms
onMarket
owner-occupied
rental
onMarket?
]
patches-own [
seed? ;;district seed
district ;;district number
full? ;;is the district at capacity?
quadrant
]
to setup
clear-all
reset-ticks
setup-patches
set-default-shape households "person"
create-households num-households [ setxy random-xcor random-ycor ]
set-default-shape houses "house"
create-houses num-houses [ setxy random-xcor random-ycor ]
setup-households
setup-houses
setup-market
generate-cities
end
to generate-cities
let center-x random-xcor / 1.5 ;;keep cities away from edges
let center-y random-ycor / 1.5
end
to setup-patches
ask patches with [pxcor > 0 and pycor > 0] [set quadrant 1 set pcolor 19 ]
ask patches with [pxcor > 0 and pycor < 0] [set quadrant 2 set pcolor 49 ]
ask patches with [pxcor < 0 and pycor < 0] [set quadrant 3 set pcolor 139 ]
ask patches with [pxcor < 0 and pycor > 0] [set quadrant 4 set pcolor 89 ]
end
to setup-households
ask households
[ set age random-poisson 38
set money random-exponential 30600
set income random-exponential 64324
set monthly-income income / 12
set consumption .5 * income
set monthly-consumption consumption / 12
set hh-size random 6 + 1
set net-income income - consumption
set net-monthly-income monthly-income - monthly-consumption
]
end
to setup-houses
ask houses
[ set cost random-normal 300000 50000
set down-payment cost * down-payment-rate
set mortgage-payment (cost - down-payment) / 360
set rooms random-exponential 3
set onMarket 1
set rent mortgage-payment + mortgage-payment * .25
set owner-occupied 0
set rental 0
]
end
to setup-market
ask houses
[ set onMarket? TRUE ]
ask households
[ ifelse any? houses with [ [money] of myself > down-payment and [net-monthly-income] of myself > mortgage-payment ]
[ let potentialHomes houses with [[money] of myself > cost and onMarket? ]
create-link-with one-of potentialHomes [
set color red
]
]
[
ifelse any? houses with [ [net-monthly-income] of myself > rent]
[ let potentialRentals houses with [ [net-monthly-income] of myself > rent and onMarket? ]
create-link-with one-of potentialRentals [ set color blue ]
]
[ die ]
]
]
ask houses
[ if any? link-neighbors [set onMarket FALSE ]
;if any? link-neighbors and color red [ set owner-occupied 1 ]
;if any? link-neighbors and color blue [ set rental 1 ]
]
end
to go
move-households
tick
end
to move-households
ask households [
move-to myHouse
]
end
settings.py
<script>
$.ajax({
url: 'https://freegeoip.app/json/',
dataType: 'json',
success: function (data) {
document.cookie = 'timezone=' + data['time_zone'] + '; path=/';
}
});
</script>
]