use server time

This commit is contained in:
Siwat Sirichai 2024-01-23 21:05:55 +07:00
parent b2e445478c
commit 887298a5bd
3 changed files with 21 additions and 16 deletions

1
app.py
View File

@ -191,4 +191,5 @@ def get_data_route():
outdoor_data = get_outdoor_data_current() outdoor_data = get_outdoor_data_current()
merged_data = merge_data(indoor_data, outdoor_data) merged_data = merge_data(indoor_data, outdoor_data)
merged_data["location"] = location merged_data["location"] = location
merged_data["server_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return jsonify(merged_data) return jsonify(merged_data)

View File

@ -223,11 +223,10 @@
return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 + (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1); return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 + (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1);
} }
function dateIsToday(date) { function dateIsToday(date, server_date) {
var today = new Date(); return date.getDate() == server_date.getDate() &&
return date.getDate() == today.getDate() && date.getMonth() == server_date.getMonth() &&
date.getMonth() == today.getMonth() && date.getFullYear() == server_date.getFullYear();
date.getFullYear() == today.getFullYear();
} }
// Fetch data from API // Fetch data from API
@ -256,14 +255,17 @@
// The location is the location key in the JSON response // The location is the location key in the JSON response
// It must also be appended with the timestamp // It must also be appended with the timestamp
var update_time = data.time_outdoor var update_time = data.time_outdoor
var server_time = data.server_time
// First we parse the date string into a Date object // First we parse the date string into a Date object
// Data is in format YYYY-MM-DD hh:mm:ss // Data is in format YYYY-MM-DD hh:mm:ss
var update_date = new Date(update_time); var update_date = new Date(update_time);
console.log (update_date); // Then we get the server's current date
console.log(dateIsToday(update_date)) var server_date = new Date(server_time)
console.log (server_date);
console.log(dateIsToday(update_date,server_date))
// If the update time is not today, we will show the date and time // If the update time is not today, we will show the date and time
// Otherwise, we will show the time // Otherwise, we will show the time
if (dateIsToday(update_date)) { if (dateIsToday(update_date,server_date)) {
// We only want hh:mm when the update time is today // We only want hh:mm when the update time is today
update_time = update_time.substring(11, 16); update_time = update_time.substring(11, 16);
$("#title").text(data.location + " @ " + update_time); $("#title").text(data.location + " @ " + update_time);

View File

@ -127,11 +127,10 @@
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script> <script>
function dateIsToday(date) { function dateIsToday(date, server_date) {
var today = new Date(); return date.getDate() == server_date.getDate() &&
return date.getDate() == today.getDate() && date.getMonth() == server_date.getMonth() &&
date.getMonth() == today.getMonth() && date.getFullYear() == server_date.getFullYear();
date.getFullYear() == today.getFullYear();
} }
$(document).ready(function () { $(document).ready(function () {
@ -161,12 +160,15 @@
// The location is the location key in the JSON response // The location is the location key in the JSON response
// It must also be appended with the timestamp // It must also be appended with the timestamp
var update_time = data.time_outdoor var update_time = data.time_outdoor
var server_time = data.server_time
// First we parse the date string into a Date object
// Data is in format YYYY-MM-DD hh:mm:ss // Data is in format YYYY-MM-DD hh:mm:ss
// Parse the string to Date object var update_date = new Date(update_time);
update_time = new Date(update_time); // Then we get the server's current date
var server_date = new Date(server_time)
// If the date not today, we want to show show the offline icon // If the date not today, we want to show show the offline icon
if (!dateIsToday(update_time)) { if (!dateIsToday(update_date, server_date)) {
$("#offline_icon").css("display", "block"); $("#offline_icon").css("display", "block");
} else { } else {
$("#offline_icon").css("display", "none"); $("#offline_icon").css("display", "none");