Source
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
var CDate = function () {
this.clientDate = (arguments.length > 0)
? new Date(arguments[0])
: new Date();
this.calcTZdiff(this.clientDate.getTime());
this.serverDate = new Date(this.clientDate.getTime() - this.tzDiff * 1000);
};
CDate.prototype = {
server: 0, // getTime uses clients :0, or servers time :1
tzDiff: 0, // server and client TZ diff
clientDate: null, // clients(JS, Browser) date object
serverDate: null, // servers(PHP, Unix) date object
calcTZdiff: function(time) {
if (time === undefined) {
time = new Date().getTime();
}
const timestamp = Object.keys(PHP_TZ_OFFSETS).reverse().find((ts) => ts * 1000 <= time);
this.tzDiff = this.clientDate.getTimezoneOffset() * -60 - PHP_TZ_OFFSETS[timestamp];
},
/**
* Formats date according given format. Uses server timezone.
* Supported formats: 'd M Y H:i', 'j. M Y G:i', 'Y/m/d H:i', 'Y-m-d H:i', 'Y-m-d H:i:s', 'Y-m-d h:i:s A',
* 'Y-m-d','H:i:s', 'H:i', 'M jS, Y h:i A', 'Y M d H:i', 'd.m.Y H:i' and 'd m Y H i'
* Format 'd m Y H i' is also accepted but used internally for date input fields.
*
* @param format PHP style date format limited to supported formats
*
* @return string|bool human readable date or false if unsupported format given
*/
format: function(format) {
const shortMn = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const dt = this.getDate();
const mnth = this.getMonth();
const yr = this.getFullYear();
const hrs = this.getHours();
const mnts = this.getMinutes();
const sec = this.getSeconds();
const ampm = (hrs < 12) ? 'AM' : 'PM';
const ampmhrs = appendZero((hrs + 11) % 12 + 1);
/**
* Append date suffix according to English rules e.g., 3 becomes 3rd.
*
* @param int date
*
* @return string
*/
const appSfx = function(date) {
if (date % 10 == 1 && date != 11) {
return date + 'st';
}
if (date % 10 == 2 && date != 12) {
return date + 'nd';
}
if (date % 10 == 3 && date != 13) {
return date + 'rd';
}
return date + 'th';
};
switch (format) {
case 'd M Y H:i':
return appendZero(dt) + ' ' + shortMn[mnth] + ' ' + yr + ' ' + appendZero(hrs) + ':' + appendZero(mnts);
case 'j. M Y G:i':
return dt + '. ' + shortMn[mnth] + ' ' + yr + ' ' + hrs + ':' + appendZero(mnts);
case 'Y/m/d H:i':