MDL-63044 javascript: add getUserMidnightForTimestamp to user_date

This commit is contained in:
Ryan Wyllie 2018-09-27 10:13:55 +08:00
parent 6e403eb62f
commit 119c0124c4
2 changed files with 39 additions and 2 deletions

View File

@ -1 +1 @@
define(["jquery","core/ajax","core/sessionstorage","core/config"],function(a,b,c,d){var e={},f=function(b){var c=a("html").attr("lang").replace(/-/g,"_");return"core_user_date/"+c+"/"+d.usertimezone+"/"+b.timestamp+"/"+b.format},g=function(a){return c.get(a)},h=function(a,b){c.set(a,b)},i=function(a){return"undefined"!=typeof e[a]},j=function(a){return e[a]},k=function(a,b){e[a]=b},l=function(a){var c=a.map(function(a){return{timestamp:a.timestamp,format:a.format}}),e={methodname:"core_get_user_dates",args:{contextid:d.contextid,timestamps:c}};return b.call([e],!0,!0)[0].then(function(b){b.dates.forEach(function(b,c){var d=a[c],e=f(d);h(e,b),d.deferred.resolve(b)})})["catch"](function(b){a.forEach(function(a){a.deferred.reject(b)})})},m=function(b){var c=[],d=[];return b.forEach(function(b){var e=f(b);if(i(e))d.push(j(e));else{var h=a.Deferred(),l=g(e);l?h.resolve(l):(b.deferred=h,c.push(b)),k(e,h.promise()),d.push(h.promise())}}),c.length&&l(c),a.when.apply(a,d).then(function(){return 1===arguments.length?[arguments[0]]:Array.apply(null,arguments)})};return{get:m}}); define(["jquery","core/ajax","core/sessionstorage","core/config"],function(a,b,c,d){var e=86400,f={},g=function(b){var c=a("html").attr("lang").replace(/-/g,"_");return"core_user_date/"+c+"/"+d.usertimezone+"/"+b.timestamp+"/"+b.format},h=function(a){return c.get(a)},i=function(a,b){c.set(a,b)},j=function(a){return"undefined"!=typeof f[a]},k=function(a){return f[a]},l=function(a,b){f[a]=b},m=function(a){var c=a.map(function(a){return{timestamp:a.timestamp,format:a.format}}),e={methodname:"core_get_user_dates",args:{contextid:d.contextid,timestamps:c}};return b.call([e],!0,!0)[0].then(function(b){b.dates.forEach(function(b,c){var d=a[c],e=g(d);i(e,b),d.deferred.resolve(b)})})["catch"](function(b){a.forEach(function(a){a.deferred.reject(b)})})},n=function(b){var c=[],d=[];return b.forEach(function(b){var e=g(b);if(j(e))d.push(k(e));else{var f=a.Deferred(),i=h(e);i?f.resolve(i):(b.deferred=f,c.push(b)),l(e,f.promise()),d.push(f.promise())}}),c.length&&m(c),a.when.apply(a,d).then(function(){return 1===arguments.length?[arguments[0]]:Array.apply(null,arguments)})},o=function(a,b){var c=a>b,d=Math.abs(a-b),f=c?Math.floor(d/e):Math.ceil(d/e),g=f*e,h=c?b+g:b-g;return h};return{get:n,getUserMidnightForTimestamp:o}});

View File

@ -24,6 +24,8 @@
define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'], define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'],
function($, Ajax, Storage, Config) { function($, Ajax, Storage, Config) {
var SECONDS_IN_DAY = 86400;
/** @var {object} promisesCache Store all promises we've seen so far. */ /** @var {object} promisesCache Store all promises we've seen so far. */
var promisesCache = {}; var promisesCache = {};
@ -228,7 +230,42 @@ define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'],
}); });
}; };
/**
* For a given timestamp get the midnight value in the user's timezone.
*
* The calculation is performed relative to the user's midnight timestamp
* for today to ensure that timezones are preserved.
*
* E.g.
* Input:
* timestamp: 1514836800 (01/01/2018 8pm GMT)(02/01/2018 4am GMT+8)
* midnight: 1514851200 (02/01/2018 midnight GMT)
* Output:
* 1514764800 (01/01/2018 midnight GMT)
*
* Input:
* timestamp: 1514836800 (01/01/2018 8pm GMT)(02/01/2018 4am GMT+8)
* midnight: 1514822400 (02/01/2018 midnight GMT+8)
* Output:
* 1514822400 (02/01/2018 midnight GMT+8)
*
* @param {Number} timestamp The timestamp to calculate from
* @param {Number} todayMidnight The user's midnight timestamp
* @return {Number} The midnight value of the user's timestamp
*/
var getUserMidnightForTimestamp = function(timestamp, todayMidnight) {
var future = timestamp > todayMidnight;
var diffSeconds = Math.abs(timestamp - todayMidnight);
var diffDays = future ? Math.floor(diffSeconds / SECONDS_IN_DAY) : Math.ceil(diffSeconds / SECONDS_IN_DAY);
var diffDaysInSeconds = diffDays * SECONDS_IN_DAY;
// Is the timestamp in the future or past?
var dayTimestamp = future ? todayMidnight + diffDaysInSeconds : todayMidnight - diffDaysInSeconds;
return dayTimestamp;
};
return { return {
get: get get: get,
getUserMidnightForTimestamp: getUserMidnightForTimestamp
}; };
}); });