time.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import * as numberUtil from './number.js';
  42. import { getDefaultLocaleModel, getLocaleModel, SYSTEM_LANG } from '../core/locale.js';
  43. import Model from '../model/Model.js';
  44. export var ONE_SECOND = 1000;
  45. export var ONE_MINUTE = ONE_SECOND * 60;
  46. export var ONE_HOUR = ONE_MINUTE * 60;
  47. export var ONE_DAY = ONE_HOUR * 24;
  48. export var ONE_YEAR = ONE_DAY * 365;
  49. export var defaultLeveledFormatter = {
  50. year: '{yyyy}',
  51. month: '{MMM}',
  52. day: '{d}',
  53. hour: '{HH}:{mm}',
  54. minute: '{HH}:{mm}',
  55. second: '{HH}:{mm}:{ss}',
  56. millisecond: '{HH}:{mm}:{ss} {SSS}',
  57. none: '{yyyy}-{MM}-{dd} {HH}:{mm}:{ss} {SSS}'
  58. };
  59. var fullDayFormatter = '{yyyy}-{MM}-{dd}';
  60. export var fullLeveledFormatter = {
  61. year: '{yyyy}',
  62. month: '{yyyy}-{MM}',
  63. day: fullDayFormatter,
  64. hour: fullDayFormatter + ' ' + defaultLeveledFormatter.hour,
  65. minute: fullDayFormatter + ' ' + defaultLeveledFormatter.minute,
  66. second: fullDayFormatter + ' ' + defaultLeveledFormatter.second,
  67. millisecond: defaultLeveledFormatter.none
  68. };
  69. export var primaryTimeUnits = ['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond'];
  70. export var timeUnits = ['year', 'half-year', 'quarter', 'month', 'week', 'half-week', 'day', 'half-day', 'quarter-day', 'hour', 'minute', 'second', 'millisecond'];
  71. export function pad(str, len) {
  72. str += '';
  73. return '0000'.substr(0, len - str.length) + str;
  74. }
  75. export function getPrimaryTimeUnit(timeUnit) {
  76. switch (timeUnit) {
  77. case 'half-year':
  78. case 'quarter':
  79. return 'month';
  80. case 'week':
  81. case 'half-week':
  82. return 'day';
  83. case 'half-day':
  84. case 'quarter-day':
  85. return 'hour';
  86. default:
  87. // year, minutes, second, milliseconds
  88. return timeUnit;
  89. }
  90. }
  91. export function isPrimaryTimeUnit(timeUnit) {
  92. return timeUnit === getPrimaryTimeUnit(timeUnit);
  93. }
  94. export function getDefaultFormatPrecisionOfInterval(timeUnit) {
  95. switch (timeUnit) {
  96. case 'year':
  97. case 'month':
  98. return 'day';
  99. case 'millisecond':
  100. return 'millisecond';
  101. default:
  102. // Also for day, hour, minute, second
  103. return 'second';
  104. }
  105. }
  106. export function format(
  107. // Note: The result based on `isUTC` are totally different, which can not be just simply
  108. // substituted by the result without `isUTC`. So we make the param `isUTC` mandatory.
  109. time, template, isUTC, lang) {
  110. var date = numberUtil.parseDate(time);
  111. var y = date[fullYearGetterName(isUTC)]();
  112. var M = date[monthGetterName(isUTC)]() + 1;
  113. var q = Math.floor((M - 1) / 3) + 1;
  114. var d = date[dateGetterName(isUTC)]();
  115. var e = date['get' + (isUTC ? 'UTC' : '') + 'Day']();
  116. var H = date[hoursGetterName(isUTC)]();
  117. var h = (H - 1) % 12 + 1;
  118. var m = date[minutesGetterName(isUTC)]();
  119. var s = date[secondsGetterName(isUTC)]();
  120. var S = date[millisecondsGetterName(isUTC)]();
  121. var a = H >= 12 ? 'pm' : 'am';
  122. var A = a.toUpperCase();
  123. var localeModel = lang instanceof Model ? lang : getLocaleModel(lang || SYSTEM_LANG) || getDefaultLocaleModel();
  124. var timeModel = localeModel.getModel('time');
  125. var month = timeModel.get('month');
  126. var monthAbbr = timeModel.get('monthAbbr');
  127. var dayOfWeek = timeModel.get('dayOfWeek');
  128. var dayOfWeekAbbr = timeModel.get('dayOfWeekAbbr');
  129. return (template || '').replace(/{a}/g, a + '').replace(/{A}/g, A + '').replace(/{yyyy}/g, y + '').replace(/{yy}/g, pad(y % 100 + '', 2)).replace(/{Q}/g, q + '').replace(/{MMMM}/g, month[M - 1]).replace(/{MMM}/g, monthAbbr[M - 1]).replace(/{MM}/g, pad(M, 2)).replace(/{M}/g, M + '').replace(/{dd}/g, pad(d, 2)).replace(/{d}/g, d + '').replace(/{eeee}/g, dayOfWeek[e]).replace(/{ee}/g, dayOfWeekAbbr[e]).replace(/{e}/g, e + '').replace(/{HH}/g, pad(H, 2)).replace(/{H}/g, H + '').replace(/{hh}/g, pad(h + '', 2)).replace(/{h}/g, h + '').replace(/{mm}/g, pad(m, 2)).replace(/{m}/g, m + '').replace(/{ss}/g, pad(s, 2)).replace(/{s}/g, s + '').replace(/{SSS}/g, pad(S, 3)).replace(/{S}/g, S + '');
  130. }
  131. export function leveledFormat(tick, idx, formatter, lang, isUTC) {
  132. var template = null;
  133. if (zrUtil.isString(formatter)) {
  134. // Single formatter for all units at all levels
  135. template = formatter;
  136. } else if (zrUtil.isFunction(formatter)) {
  137. // Callback formatter
  138. template = formatter(tick.value, idx, {
  139. level: tick.level
  140. });
  141. } else {
  142. var defaults = zrUtil.extend({}, defaultLeveledFormatter);
  143. if (tick.level > 0) {
  144. for (var i = 0; i < primaryTimeUnits.length; ++i) {
  145. defaults[primaryTimeUnits[i]] = "{primary|" + defaults[primaryTimeUnits[i]] + "}";
  146. }
  147. }
  148. var mergedFormatter = formatter ? formatter.inherit === false ? formatter // Use formatter with bigger units
  149. : zrUtil.defaults(formatter, defaults) : defaults;
  150. var unit = getUnitFromValue(tick.value, isUTC);
  151. if (mergedFormatter[unit]) {
  152. template = mergedFormatter[unit];
  153. } else if (mergedFormatter.inherit) {
  154. // Unit formatter is not defined and should inherit from bigger units
  155. var targetId = timeUnits.indexOf(unit);
  156. for (var i = targetId - 1; i >= 0; --i) {
  157. if (mergedFormatter[unit]) {
  158. template = mergedFormatter[unit];
  159. break;
  160. }
  161. }
  162. template = template || defaults.none;
  163. }
  164. if (zrUtil.isArray(template)) {
  165. var levelId = tick.level == null ? 0 : tick.level >= 0 ? tick.level : template.length + tick.level;
  166. levelId = Math.min(levelId, template.length - 1);
  167. template = template[levelId];
  168. }
  169. }
  170. return format(new Date(tick.value), template, isUTC, lang);
  171. }
  172. export function getUnitFromValue(value, isUTC) {
  173. var date = numberUtil.parseDate(value);
  174. var M = date[monthGetterName(isUTC)]() + 1;
  175. var d = date[dateGetterName(isUTC)]();
  176. var h = date[hoursGetterName(isUTC)]();
  177. var m = date[minutesGetterName(isUTC)]();
  178. var s = date[secondsGetterName(isUTC)]();
  179. var S = date[millisecondsGetterName(isUTC)]();
  180. var isSecond = S === 0;
  181. var isMinute = isSecond && s === 0;
  182. var isHour = isMinute && m === 0;
  183. var isDay = isHour && h === 0;
  184. var isMonth = isDay && d === 1;
  185. var isYear = isMonth && M === 1;
  186. if (isYear) {
  187. return 'year';
  188. } else if (isMonth) {
  189. return 'month';
  190. } else if (isDay) {
  191. return 'day';
  192. } else if (isHour) {
  193. return 'hour';
  194. } else if (isMinute) {
  195. return 'minute';
  196. } else if (isSecond) {
  197. return 'second';
  198. } else {
  199. return 'millisecond';
  200. }
  201. }
  202. export function getUnitValue(value, unit, isUTC) {
  203. var date = zrUtil.isNumber(value) ? numberUtil.parseDate(value) : value;
  204. unit = unit || getUnitFromValue(value, isUTC);
  205. switch (unit) {
  206. case 'year':
  207. return date[fullYearGetterName(isUTC)]();
  208. case 'half-year':
  209. return date[monthGetterName(isUTC)]() >= 6 ? 1 : 0;
  210. case 'quarter':
  211. return Math.floor((date[monthGetterName(isUTC)]() + 1) / 4);
  212. case 'month':
  213. return date[monthGetterName(isUTC)]();
  214. case 'day':
  215. return date[dateGetterName(isUTC)]();
  216. case 'half-day':
  217. return date[hoursGetterName(isUTC)]() / 24;
  218. case 'hour':
  219. return date[hoursGetterName(isUTC)]();
  220. case 'minute':
  221. return date[minutesGetterName(isUTC)]();
  222. case 'second':
  223. return date[secondsGetterName(isUTC)]();
  224. case 'millisecond':
  225. return date[millisecondsGetterName(isUTC)]();
  226. }
  227. }
  228. export function fullYearGetterName(isUTC) {
  229. return isUTC ? 'getUTCFullYear' : 'getFullYear';
  230. }
  231. export function monthGetterName(isUTC) {
  232. return isUTC ? 'getUTCMonth' : 'getMonth';
  233. }
  234. export function dateGetterName(isUTC) {
  235. return isUTC ? 'getUTCDate' : 'getDate';
  236. }
  237. export function hoursGetterName(isUTC) {
  238. return isUTC ? 'getUTCHours' : 'getHours';
  239. }
  240. export function minutesGetterName(isUTC) {
  241. return isUTC ? 'getUTCMinutes' : 'getMinutes';
  242. }
  243. export function secondsGetterName(isUTC) {
  244. return isUTC ? 'getUTCSeconds' : 'getSeconds';
  245. }
  246. export function millisecondsGetterName(isUTC) {
  247. return isUTC ? 'getUTCMilliseconds' : 'getMilliseconds';
  248. }
  249. export function fullYearSetterName(isUTC) {
  250. return isUTC ? 'setUTCFullYear' : 'setFullYear';
  251. }
  252. export function monthSetterName(isUTC) {
  253. return isUTC ? 'setUTCMonth' : 'setMonth';
  254. }
  255. export function dateSetterName(isUTC) {
  256. return isUTC ? 'setUTCDate' : 'setDate';
  257. }
  258. export function hoursSetterName(isUTC) {
  259. return isUTC ? 'setUTCHours' : 'setHours';
  260. }
  261. export function minutesSetterName(isUTC) {
  262. return isUTC ? 'setUTCMinutes' : 'setMinutes';
  263. }
  264. export function secondsSetterName(isUTC) {
  265. return isUTC ? 'setUTCSeconds' : 'setSeconds';
  266. }
  267. export function millisecondsSetterName(isUTC) {
  268. return isUTC ? 'setUTCMilliseconds' : 'setMilliseconds';
  269. }