time.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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( // Note: The result based on `isUTC` are totally different, which can not be just simply
  107. // substituted by the result without `isUTC`. So we make the param `isUTC` mandatory.
  108. time, template, isUTC, lang) {
  109. var date = numberUtil.parseDate(time);
  110. var y = date[fullYearGetterName(isUTC)]();
  111. var M = date[monthGetterName(isUTC)]() + 1;
  112. var q = Math.floor((M - 1) / 3) + 1;
  113. var d = date[dateGetterName(isUTC)]();
  114. var e = date['get' + (isUTC ? 'UTC' : '') + 'Day']();
  115. var H = date[hoursGetterName(isUTC)]();
  116. var h = (H - 1) % 12 + 1;
  117. var m = date[minutesGetterName(isUTC)]();
  118. var s = date[secondsGetterName(isUTC)]();
  119. var S = date[millisecondsGetterName(isUTC)]();
  120. var localeModel = lang instanceof Model ? lang : getLocaleModel(lang || SYSTEM_LANG) || getDefaultLocaleModel();
  121. var timeModel = localeModel.getModel('time');
  122. var month = timeModel.get('month');
  123. var monthAbbr = timeModel.get('monthAbbr');
  124. var dayOfWeek = timeModel.get('dayOfWeek');
  125. var dayOfWeekAbbr = timeModel.get('dayOfWeekAbbr');
  126. return (template || '').replace(/{yyyy}/g, y + '').replace(/{yy}/g, y % 100 + '').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 + '');
  127. }
  128. export function leveledFormat(tick, idx, formatter, lang, isUTC) {
  129. var template = null;
  130. if (zrUtil.isString(formatter)) {
  131. // Single formatter for all units at all levels
  132. template = formatter;
  133. } else if (zrUtil.isFunction(formatter)) {
  134. // Callback formatter
  135. template = formatter(tick.value, idx, {
  136. level: tick.level
  137. });
  138. } else {
  139. var defaults = zrUtil.extend({}, defaultLeveledFormatter);
  140. if (tick.level > 0) {
  141. for (var i = 0; i < primaryTimeUnits.length; ++i) {
  142. defaults[primaryTimeUnits[i]] = "{primary|" + defaults[primaryTimeUnits[i]] + "}";
  143. }
  144. }
  145. var mergedFormatter = formatter ? formatter.inherit === false ? formatter // Use formatter with bigger units
  146. : zrUtil.defaults(formatter, defaults) : defaults;
  147. var unit = getUnitFromValue(tick.value, isUTC);
  148. if (mergedFormatter[unit]) {
  149. template = mergedFormatter[unit];
  150. } else if (mergedFormatter.inherit) {
  151. // Unit formatter is not defined and should inherit from bigger units
  152. var targetId = timeUnits.indexOf(unit);
  153. for (var i = targetId - 1; i >= 0; --i) {
  154. if (mergedFormatter[unit]) {
  155. template = mergedFormatter[unit];
  156. break;
  157. }
  158. }
  159. template = template || defaults.none;
  160. }
  161. if (zrUtil.isArray(template)) {
  162. var levelId = tick.level == null ? 0 : tick.level >= 0 ? tick.level : template.length + tick.level;
  163. levelId = Math.min(levelId, template.length - 1);
  164. template = template[levelId];
  165. }
  166. }
  167. return format(new Date(tick.value), template, isUTC, lang);
  168. }
  169. export function getUnitFromValue(value, isUTC) {
  170. var date = numberUtil.parseDate(value);
  171. var M = date[monthGetterName(isUTC)]() + 1;
  172. var d = date[dateGetterName(isUTC)]();
  173. var h = date[hoursGetterName(isUTC)]();
  174. var m = date[minutesGetterName(isUTC)]();
  175. var s = date[secondsGetterName(isUTC)]();
  176. var S = date[millisecondsGetterName(isUTC)]();
  177. var isSecond = S === 0;
  178. var isMinute = isSecond && s === 0;
  179. var isHour = isMinute && m === 0;
  180. var isDay = isHour && h === 0;
  181. var isMonth = isDay && d === 1;
  182. var isYear = isMonth && M === 1;
  183. if (isYear) {
  184. return 'year';
  185. } else if (isMonth) {
  186. return 'month';
  187. } else if (isDay) {
  188. return 'day';
  189. } else if (isHour) {
  190. return 'hour';
  191. } else if (isMinute) {
  192. return 'minute';
  193. } else if (isSecond) {
  194. return 'second';
  195. } else {
  196. return 'millisecond';
  197. }
  198. }
  199. export function getUnitValue(value, unit, isUTC) {
  200. var date = zrUtil.isNumber(value) ? numberUtil.parseDate(value) : value;
  201. unit = unit || getUnitFromValue(value, isUTC);
  202. switch (unit) {
  203. case 'year':
  204. return date[fullYearGetterName(isUTC)]();
  205. case 'half-year':
  206. return date[monthGetterName(isUTC)]() >= 6 ? 1 : 0;
  207. case 'quarter':
  208. return Math.floor((date[monthGetterName(isUTC)]() + 1) / 4);
  209. case 'month':
  210. return date[monthGetterName(isUTC)]();
  211. case 'day':
  212. return date[dateGetterName(isUTC)]();
  213. case 'half-day':
  214. return date[hoursGetterName(isUTC)]() / 24;
  215. case 'hour':
  216. return date[hoursGetterName(isUTC)]();
  217. case 'minute':
  218. return date[minutesGetterName(isUTC)]();
  219. case 'second':
  220. return date[secondsGetterName(isUTC)]();
  221. case 'millisecond':
  222. return date[millisecondsGetterName(isUTC)]();
  223. }
  224. }
  225. export function fullYearGetterName(isUTC) {
  226. return isUTC ? 'getUTCFullYear' : 'getFullYear';
  227. }
  228. export function monthGetterName(isUTC) {
  229. return isUTC ? 'getUTCMonth' : 'getMonth';
  230. }
  231. export function dateGetterName(isUTC) {
  232. return isUTC ? 'getUTCDate' : 'getDate';
  233. }
  234. export function hoursGetterName(isUTC) {
  235. return isUTC ? 'getUTCHours' : 'getHours';
  236. }
  237. export function minutesGetterName(isUTC) {
  238. return isUTC ? 'getUTCMinutes' : 'getMinutes';
  239. }
  240. export function secondsGetterName(isUTC) {
  241. return isUTC ? 'getUTCSeconds' : 'getSeconds';
  242. }
  243. export function millisecondsGetterName(isUTC) {
  244. return isUTC ? 'getUTCMilliseconds' : 'getMilliseconds';
  245. }
  246. export function fullYearSetterName(isUTC) {
  247. return isUTC ? 'setUTCFullYear' : 'setFullYear';
  248. }
  249. export function monthSetterName(isUTC) {
  250. return isUTC ? 'setUTCMonth' : 'setMonth';
  251. }
  252. export function dateSetterName(isUTC) {
  253. return isUTC ? 'setUTCDate' : 'setDate';
  254. }
  255. export function hoursSetterName(isUTC) {
  256. return isUTC ? 'setUTCHours' : 'setHours';
  257. }
  258. export function minutesSetterName(isUTC) {
  259. return isUTC ? 'setUTCMinutes' : 'setMinutes';
  260. }
  261. export function secondsSetterName(isUTC) {
  262. return isUTC ? 'setUTCSeconds' : 'setSeconds';
  263. }
  264. export function millisecondsSetterName(isUTC) {
  265. return isUTC ? 'setUTCMilliseconds' : 'setMilliseconds';
  266. }