Note: To achieve this it is important to work with UTC timestamps in both DB and your view
Here is what my code in django views.py looks like:
def get_entry_info(request): """ Recover information for the specified entry name """ entry_name = request.POST.get("name", None) if request.method == "POST" and entry_name is not None: data = {} # Get entry object entry = MyEntries.objects.get(name=entry_name) data['start_date'] = int(time.mktime(entry.start_date.replace(tzinfo=None).timetuple())) * 1000 return JsonResponse(data) else: return HttpResponse("Invalid entry name")
Here is what my javascript code looks like:
var startDate; // Loads content into the information panel // @param data : data to be displayed in the information panel function reloadInformationPanel(entryname){ // Get job information from server $.ajax({ headers: { "X-CSRFToken": '{{ csrf_token }}' }, url: "{% url 'get_entry_info' %}", method: 'POST', dataType: 'json', data: { 'name': entryname, // outgoing data }, success: function (data) { startDate = new Date(data.start_date); startTime(); }, error: function(xhr,errmsg,err) { console.log(xhr.status + ": " + xhr.responseText); } }); } // Starts timer of job duration function startTime() { var now = convertDateToUTC(new Date()); var delay = new Date(now - startDate); document.getElementById('entry_duration').innerHTML = delay.getUTCHours() + "h" + delay.getUTCMinutes() + "m" + delay.getUTCSeconds() + "s"; var t = setTimeout(startTime, 500); } // Converts a Date object to UTC function convertDateToUTC(date) { return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); }
Now simply map reloadInformationPanel to the entry selection button ;)
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.