fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

391 lines
12 KiB

/** Constants used for time computations */
import { getDate, getTime, parseInt } from "../2.base";
import { i18nText } from "../utils";
import { StartOfWeek } from "../constant";
import { isKhtml } from "../platform/web";
import { _global } from "../0.foundation";
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
const _FD = 1;
const _SMN = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11
];
const _MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const _OFFSET = [0, -1, -2, -3, -4, -5, -6]; // 实际上无论周几作为一周的第一天,周初周末都是在-6-0间做偏移,用一个数组就可以
export const Date = {
SECOND,
MINUTE,
HOUR,
DAY,
WEEK,
_FD,
_SMN,
_MD,
_OFFSET,
};
/**
* 获取时区
* @returns {String}
*/
export function getTimezone(date) {
return date.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, "");
}
/**
* Returns the number of days in the current month
*/
export function getMonthDays(date, month = date.getMonth()) {
const year = date.getFullYear();
if (((0 === (year % 4)) && ((0 !== (year % 100)) || (0 === (year % 400)))) && month === 1) {
return 29;
}
return _MD[month];
}
/**
* 获取每月的最后一天
* @returns {Date}
*/
export function getLastDateOfMont(date) {
return getDate(date.getFullYear(), date.getMonth(), getMonthDays(date));
}
/**
* 获取每月的最后一天
* @returns {Date}
*/
export function getLastDateOfMonth(date) {
return getDate(date.getFullYear(), date.getMonth(), getMonthDays(date));
}
/**
* Returns the number of day in the year.
* @param date
* @returns {number}
*/
export function getDayOfYear(date) {
const now = getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
const then = getDate(date.getFullYear(), 0, 0, 0, 0, 0);
const time = now - then;
return Math.floor(time / DAY);
}
/**
* Returns the number of the week in year, as defined in ISO 8601.
* @param date
* @returns {number}
*/
export function getWeekNumber(date) {
const d = getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
const week = d.getDay();
const startOfWeek = StartOfWeek % 7;
let middleDay = (startOfWeek + 3) % 7;
middleDay = middleDay || 7;
// 偏移到周周首之前需要多少天
const offsetWeekStartCount = week < startOfWeek ? (7 + week - startOfWeek) : (week - startOfWeek);
const offsetWeekMiddleCount = middleDay < startOfWeek ? (7 + middleDay - startOfWeek) : (middleDay - startOfWeek);
d.setDate(d.getDate() - offsetWeekStartCount + offsetWeekMiddleCount);
const ms = d.valueOf();
d.setMonth(0);
d.setDate(1);
return Math.floor((ms - d.valueOf()) / (7 * 864e5)) + 1;
}
export function getQuarter(date) {
return Math.floor(date.getMonth() / 3) + 1;
}
/**
* 离当前时间多少天的时间
* @param date
* @param offset
* @returns {Date}
*/
export function getOffsetDate(date, offset) {
return getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()) + offset * 864e5);
}
export function getOffsetQuarter(date, n) {
const dt = getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
let day = dt.getDate();
const monthDay = getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10) * 3, 1));
if (day > monthDay) {
day = monthDay;
}
dt.setDate(day);
dt.setMonth(dt.getMonth() + parseInt(n, 10) * 3);
return dt;
}
/**
* 得到本季度的起始月份
* @param date
* @returns {number}
*/
export function getQuarterStartMonth(date) {
let quarterStartMonth = 0;
const nowMonth = date.getMonth();
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
}
/**
* 获得本季度的起始日期
* @param date
* @returns {Date}
*/
export function getQuarterStartDate(date) {
return getDate(date.getFullYear(), getQuarterStartMonth(date), 1);
}
/**
* 得到本季度的结束日期
* @param date
* @returns {Date}
*/
export function getQuarterEndDate(date) {
const quarterEndMonth = getQuarterStartMonth(date) + 2;
return getDate(date.getFullYear(), quarterEndMonth, getMonthDays(date, quarterEndMonth));
}
/**
* 指定日期n个月之前或之后的日期
* @param date
* @param n
* @returns {Date}
*/
export function getOffsetMonth(date, n) {
const dt = getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
let day = dt.getDate();
const monthDay = getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10), 1));
if (day > monthDay) {
day = monthDay;
}
dt.setDate(day);
dt.setMonth(dt.getMonth() + parseInt(n, 10));
return dt;
}
/**
* 获得本周的起始日期
* @param date
* @returns {Date}
*/
export function getWeekStartDate(date) {
const w = date.getDay();
const startOfWeek = StartOfWeek % 7;
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)]);
}
/**
* 得到本周的结束日期
* @param date
* @returns {Date}
*/
export function getWeekEndDate(date) {
const w = date.getDay();
const startOfWeek = StartOfWeek % 7;
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)] + 6);
}
export function getFullDayName(index) {
return [
i18nText("BI-Basic_Sunday"),
i18nText("BI-Basic_Monday"),
i18nText("BI-Basic_Tuesday"),
i18nText("BI-Basic_Wednesday"),
i18nText("BI-Basic_Thursday"),
i18nText("BI-Basic_Friday"),
i18nText("BI-Basic_Saturday"),
i18nText("BI-Basic_Sunday")
][index];
}
export function getShortDayName(index) {
return [
i18nText("BI-Basic_Simple_Sunday"),
i18nText("BI-Basic_Simple_Monday"),
i18nText("BI-Basic_Simple_Tuesday"),
i18nText("BI-Basic_Simple_Wednesday"),
i18nText("BI-Basic_Simple_Thursday"),
i18nText("BI-Basic_Simple_Friday"),
i18nText("BI-Basic_Simple_Saturday"),
i18nText("BI-Basic_Simple_Sunday")
][index];
}
export function getMonthName(index) {
return [
i18nText("BI-Basic_January"),
i18nText("BI-Basic_February"),
i18nText("BI-Basic_March"),
i18nText("BI-Basic_April"),
i18nText("BI-Basic_May"),
i18nText("BI-Basic_June"),
i18nText("BI-Basic_July"),
i18nText("BI-Basic_August"),
i18nText("BI-Basic_September"),
i18nText("BI-Basic_October"),
i18nText("BI-Basic_November"),
i18nText("BI-Basic_December")
][index];
}
export function getQuarterName(index) {
return [
"",
i18nText("BI-Quarter_1"),
i18nText("BI-Quarter_2"),
i18nText("BI-Quarter_3"),
i18nText("BI-Quarter_4")
][index];
}
/**
* 格式化打印日期
* @param date
* @param str
* @returns {*}
*/
export function print(date, str) {
const m = date.getMonth();
const d = date.getDate();
const y = date.getFullYear();
let yWith4number = `${y}`;
while (yWith4number.length < 4) {
yWith4number = `0${yWith4number}`;
}
const wn = getWeekNumber(date);
const qr = getQuarter(date);
const w = date.getDay();
const s = {};
const hr = date.getHours();
const pm = (hr >= 12);
let ir = (pm) ? (hr - 12) : hr;
const dy = getDayOfYear(date);
if (ir === 0) {
ir = 12;
}
const min = date.getMinutes();
const sec = date.getSeconds();
s["%a"] = getShortDayName(w); // abbreviated weekday name [FIXME: I18N]
s["%A"] = getFullDayName(w); // full weekday name
s["%b"] = _SMN[m]; // abbreviated month name [FIXME: I18N]
s["%B"] = getMonthName(m); // full month name
// FIXME: %c : preferred date and time representation for the current locale
s["%C"] = 1 + Math.floor(y / 100); // the century number
s["%d"] = (d < 10) ? (`0${d}`) : d; // the day of the month (range 01 to 31)
s["%e"] = d; // the day of the month (range 1 to 31)
// FIXME: %D : american date style: %m/%d/%y
// FIXME: %E, %F, %G, %g, %h (man strftime)
s["%H"] = (hr < 10) ? (`0${hr}`) : hr; // hour, range 00 to 23 (24h format)
s["%I"] = (ir < 10) ? (`0${ir}`) : ir; // hour, range 01 to 12 (12h format)
s["%j"] = (dy < 100) ? ((dy < 10) ? (`00${dy}`) : (`0${dy}`)) : dy; // day of the year (range 001 to 366)
s["%k"] = `${hr}`; // hour, range 0 to 23 (24h format)
s["%l"] = `${ir}`; // hour, range 1 to 12 (12h format)
s["%X"] = (m < 9) ? (`0${1 + m}`) : (1 + m); // month, range 01 to 12
s["%x"] = m + 1; // month, range 1 to 12
s["%M"] = (min < 10) ? (`0${min}`) : min; // minute, range 00 to 59
s["%n"] = "\n"; // a newline character
s["%p"] = pm ? "PM" : "AM";
s["%P"] = pm ? "pm" : "am";
// FIXME: %r : the time in am/pm notation %I:%M:%S %p
// FIXME: %R : the time in 24-hour notation %H:%M
s["%s"] = Math.floor(date.getTime() / 1000);
s["%S"] = (sec < 10) ? (`0${sec}`) : sec; // seconds, range 00 to 59
s["%t"] = "\t"; // a tab character
// FIXME: %T : the time in 24-hour notation (%H:%M:%S)
s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? (`0${wn}`) : wn;
s["%u"] = w + 1; // the day of the week (range 1 to 7, 1 = MON)
s["%w"] = w; // the day of the week (range 0 to 6, 0 = SUN)
// FIXME: %x : preferred date representation for the current locale without the time
// FIXME: %X : preferred time representation for the current locale without the date
s["%y"] = yWith4number.substr(2, 2); // year without the century (range 00 to 99)
s["%Y"] = yWith4number; // year with the century
s["%%"] = "%"; // a literal '%' character
s["%q"] = `0${qr}`;
s["%Q"] = qr;
let re = /%./g;
const isNewKhtml = isKhtml || function () {
if (!_global.navigator) {
return false;
}
return /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
};
// 包含年周的格式化,ISO8601标准周的计数会影响年
if ((str.indexOf("%Y") !== -1 || str.indexOf("%y") !== -1) && (str.indexOf("%W") !== -1 || str.indexOf("%U") !== -1 || str.indexOf("%V") !== -1)) {
switch (wn) {
// 如果周数是1,但是当前却在12月,表示此周数为下一年的
case 1:
if (m === 11) {
s["%y"] = parseInt(s["%y"]) + 1;
s["%Y"] = parseInt(s["%Y"]) + 1;
}
break;
// 如果周数是53,但是当前却在1月,表示此周数为上一年的
case 53:
if (m === 0) {
s["%y"] = parseInt(s["%y"]) - 1;
s["%Y"] = parseInt(s["%Y"]) - 1;
}
break;
default:
break;
}
}
if (!isNewKhtml()) {
return str.replace(re, par => s[par] || par);
}
const a = str.match(re);
for (let i = 0; i < a.length; i++) {
const tmp = s[a[i]];
if (tmp) {
re = new RegExp(a[i], "g");
str = str.replace(re, tmp);
}
}
return str;
}