Ohm-Management - Projektarbeit B-ME
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.

timestamp.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6. exports.getStartOfWeek = getStartOfWeek;
  7. exports.getEndOfWeek = getEndOfWeek;
  8. exports.getStartOfMonth = getStartOfMonth;
  9. exports.getEndOfMonth = getEndOfMonth;
  10. exports.parseTime = parseTime;
  11. exports.validateTimestamp = validateTimestamp;
  12. exports.parseTimestamp = parseTimestamp;
  13. exports.parseDate = parseDate;
  14. exports.getDayIdentifier = getDayIdentifier;
  15. exports.getTimeIdentifier = getTimeIdentifier;
  16. exports.updateRelative = updateRelative;
  17. exports.updateMinutes = updateMinutes;
  18. exports.updateWeekday = updateWeekday;
  19. exports.updateFormatted = updateFormatted;
  20. exports.getWeekday = getWeekday;
  21. exports.isLeapYear = isLeapYear;
  22. exports.daysInMonth = daysInMonth;
  23. exports.copyTimestamp = copyTimestamp;
  24. exports.padNumber = padNumber;
  25. exports.getDate = getDate;
  26. exports.getTime = getTime;
  27. exports.nextMinutes = nextMinutes;
  28. exports.nextDay = nextDay;
  29. exports.prevDay = prevDay;
  30. exports.relativeDays = relativeDays;
  31. exports.findWeekday = findWeekday;
  32. exports.getWeekdaySkips = getWeekdaySkips;
  33. exports.createDayList = createDayList;
  34. exports.createIntervalList = createIntervalList;
  35. exports.createNativeLocaleFormatter = createNativeLocaleFormatter;
  36. var PARSE_REGEX = exports.PARSE_REGEX = /^(\d{4})-(\d{1,2})(-(\d{1,2}))?([^\d]+(\d{1,2}))?(:(\d{1,2}))?(:(\d{1,2}))?$/;
  37. var PARSE_TIME = exports.PARSE_TIME = /(\d\d?)(:(\d\d?)|)(:(\d\d?)|)/;
  38. var DAYS_IN_MONTH = exports.DAYS_IN_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  39. var DAYS_IN_MONTH_LEAP = exports.DAYS_IN_MONTH_LEAP = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  40. var DAYS_IN_MONTH_MIN = exports.DAYS_IN_MONTH_MIN = 28;
  41. var DAYS_IN_MONTH_MAX = exports.DAYS_IN_MONTH_MAX = 31;
  42. var MONTH_MAX = exports.MONTH_MAX = 12;
  43. var MONTH_MIN = exports.MONTH_MIN = 1;
  44. var DAY_MIN = exports.DAY_MIN = 1;
  45. var DAYS_IN_WEEK = exports.DAYS_IN_WEEK = 7;
  46. var MINUTES_IN_HOUR = exports.MINUTES_IN_HOUR = 60;
  47. var HOURS_IN_DAY = exports.HOURS_IN_DAY = 24;
  48. var FIRST_HOUR = exports.FIRST_HOUR = 0;
  49. function getStartOfWeek(timestamp, weekdays, today) {
  50. var start = copyTimestamp(timestamp);
  51. findWeekday(start, weekdays[0], prevDay);
  52. updateFormatted(start);
  53. if (today) {
  54. updateRelative(start, today, start.hasTime);
  55. }
  56. return start;
  57. }
  58. function getEndOfWeek(timestamp, weekdays, today) {
  59. var end = copyTimestamp(timestamp);
  60. findWeekday(end, weekdays[weekdays.length - 1]);
  61. updateFormatted(end);
  62. if (today) {
  63. updateRelative(end, today, end.hasTime);
  64. }
  65. return end;
  66. }
  67. function getStartOfMonth(timestamp) {
  68. var start = copyTimestamp(timestamp);
  69. start.day = DAY_MIN;
  70. updateWeekday(start);
  71. updateFormatted(start);
  72. return start;
  73. }
  74. function getEndOfMonth(timestamp) {
  75. var end = copyTimestamp(timestamp);
  76. end.day = daysInMonth(end.year, end.month);
  77. updateWeekday(end);
  78. updateFormatted(end);
  79. return end;
  80. }
  81. function parseTime(input) {
  82. if (typeof input === 'number') {
  83. // when a number is given, it's minutes since 12:00am
  84. return input;
  85. } else if (typeof input === 'string') {
  86. // when a string is given, it's a hh:mm:ss format where seconds are optional
  87. var parts = PARSE_TIME.exec(input);
  88. if (!parts) {
  89. return false;
  90. }
  91. return parseInt(parts[1]) * 60 + parseInt(parts[3] || 0);
  92. } else if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object') {
  93. // when an object is given, it must have hour and minute
  94. if (typeof input.hour !== 'number' || typeof input.minute !== 'number') {
  95. return false;
  96. }
  97. return input.hour * 60 + input.minute;
  98. } else {
  99. // unsupported type
  100. return false;
  101. }
  102. }
  103. function validateTimestamp(input) {
  104. return !!PARSE_REGEX.exec(input);
  105. }
  106. function parseTimestamp(input, now) {
  107. // YYYY-MM-DD hh:mm:ss
  108. var parts = PARSE_REGEX.exec(input);
  109. if (!parts) return null;
  110. var timestamp = {
  111. date: input,
  112. time: '',
  113. year: parseInt(parts[1]),
  114. month: parseInt(parts[2]),
  115. day: parseInt(parts[4]) || 1,
  116. hour: parseInt(parts[6]) || 0,
  117. minute: parseInt(parts[8]) || 0,
  118. weekday: 0,
  119. hasDay: !!parts[4],
  120. hasTime: !!(parts[6] && parts[8]),
  121. past: false,
  122. present: false,
  123. future: false
  124. };
  125. updateWeekday(timestamp);
  126. updateFormatted(timestamp);
  127. if (now) {
  128. updateRelative(timestamp, now, timestamp.hasTime);
  129. }
  130. return timestamp;
  131. }
  132. function parseDate(date) {
  133. return updateFormatted({
  134. date: '',
  135. time: '',
  136. year: date.getFullYear(),
  137. month: date.getMonth() + 1,
  138. day: date.getDate(),
  139. weekday: date.getDay(),
  140. hour: date.getHours(),
  141. minute: date.getMinutes(),
  142. hasDay: true,
  143. hasTime: true,
  144. past: false,
  145. present: true,
  146. future: false
  147. });
  148. }
  149. function getDayIdentifier(timestamp) {
  150. return timestamp.year * 10000 + timestamp.month * 100 + timestamp.day;
  151. }
  152. function getTimeIdentifier(timestamp) {
  153. return timestamp.hour * 100 + timestamp.minute;
  154. }
  155. function updateRelative(timestamp, now) {
  156. var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  157. var a = getDayIdentifier(now);
  158. var b = getDayIdentifier(timestamp);
  159. var present = a === b;
  160. if (timestamp.hasTime && time && present) {
  161. a = getTimeIdentifier(now);
  162. b = getTimeIdentifier(timestamp);
  163. present = a === b;
  164. }
  165. timestamp.past = b < a;
  166. timestamp.present = present;
  167. timestamp.future = b > a;
  168. return timestamp;
  169. }
  170. function updateMinutes(timestamp, minutes, now) {
  171. timestamp.hasTime = true;
  172. timestamp.hour = Math.floor(minutes / MINUTES_IN_HOUR);
  173. timestamp.minute = minutes % MINUTES_IN_HOUR;
  174. timestamp.time = getTime(timestamp);
  175. if (now) {
  176. updateRelative(timestamp, now, true);
  177. }
  178. return timestamp;
  179. }
  180. function updateWeekday(timestamp) {
  181. timestamp.weekday = getWeekday(timestamp);
  182. return timestamp;
  183. }
  184. function updateFormatted(timestamp) {
  185. timestamp.time = getTime(timestamp);
  186. timestamp.date = getDate(timestamp);
  187. return timestamp;
  188. }
  189. function getWeekday(timestamp) {
  190. if (timestamp.hasDay) {
  191. var _ = Math.floor;
  192. var k = timestamp.day;
  193. var m = (timestamp.month + 9) % MONTH_MAX + 1;
  194. var C = _(timestamp.year / 100);
  195. var Y = timestamp.year % 100 - (timestamp.month <= 2 ? 1 : 0);
  196. return ((k + _(2.6 * m - 0.2) - 2 * C + Y + _(Y / 4) + _(C / 4)) % 7 + 7) % 7;
  197. }
  198. return timestamp.weekday;
  199. }
  200. function isLeapYear(year) {
  201. return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
  202. }
  203. function daysInMonth(year, month) {
  204. return isLeapYear(year) ? DAYS_IN_MONTH_LEAP[month] : DAYS_IN_MONTH[month];
  205. }
  206. function copyTimestamp(timestamp) {
  207. var date = timestamp.date,
  208. time = timestamp.time,
  209. year = timestamp.year,
  210. month = timestamp.month,
  211. day = timestamp.day,
  212. weekday = timestamp.weekday,
  213. hour = timestamp.hour,
  214. minute = timestamp.minute,
  215. hasDay = timestamp.hasDay,
  216. hasTime = timestamp.hasTime,
  217. past = timestamp.past,
  218. present = timestamp.present,
  219. future = timestamp.future;
  220. return { date: date, time: time, year: year, month: month, day: day, weekday: weekday, hour: hour, minute: minute, hasDay: hasDay, hasTime: hasTime, past: past, present: present, future: future };
  221. }
  222. function padNumber(x, length) {
  223. var padded = String(x);
  224. while (padded.length < length) {
  225. padded = '0' + padded;
  226. }
  227. return padded;
  228. }
  229. function getDate(timestamp) {
  230. var str = padNumber(timestamp.year, 4) + '-' + padNumber(timestamp.month, 2);
  231. if (timestamp.hasDay) str += '-' + padNumber(timestamp.day, 2);
  232. return str;
  233. }
  234. function getTime(timestamp) {
  235. if (!timestamp.hasTime) {
  236. return '';
  237. }
  238. return padNumber(timestamp.hour, 2) + ':' + padNumber(timestamp.minute, 2);
  239. }
  240. function nextMinutes(timestamp, minutes) {
  241. timestamp.minute += minutes;
  242. while (timestamp.minute > MINUTES_IN_HOUR) {
  243. timestamp.minute -= MINUTES_IN_HOUR;
  244. timestamp.hour++;
  245. if (timestamp.hour >= HOURS_IN_DAY) {
  246. nextDay(timestamp);
  247. timestamp.hour = FIRST_HOUR;
  248. }
  249. }
  250. return timestamp;
  251. }
  252. function nextDay(timestamp) {
  253. timestamp.day++;
  254. timestamp.weekday = (timestamp.weekday + 1) % DAYS_IN_WEEK;
  255. if (timestamp.day > DAYS_IN_MONTH_MIN && timestamp.day > daysInMonth(timestamp.year, timestamp.month)) {
  256. timestamp.day = DAY_MIN;
  257. timestamp.month++;
  258. if (timestamp.month > MONTH_MAX) {
  259. timestamp.month = MONTH_MIN;
  260. timestamp.year++;
  261. }
  262. }
  263. return timestamp;
  264. }
  265. function prevDay(timestamp) {
  266. timestamp.day--;
  267. timestamp.weekday = (timestamp.weekday + 6) % DAYS_IN_WEEK;
  268. if (timestamp.day < DAY_MIN) {
  269. timestamp.month--;
  270. if (timestamp.month < MONTH_MIN) {
  271. timestamp.year--;
  272. timestamp.month = MONTH_MAX;
  273. }
  274. timestamp.day = daysInMonth(timestamp.year, timestamp.month);
  275. }
  276. return timestamp;
  277. }
  278. function relativeDays(timestamp) {
  279. var mover = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : nextDay;
  280. var days = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  281. while (--days >= 0) {
  282. mover(timestamp);
  283. }return timestamp;
  284. }
  285. function findWeekday(timestamp, weekday) {
  286. var mover = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : nextDay;
  287. var maxDays = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 6;
  288. while (timestamp.weekday !== weekday && --maxDays >= 0) {
  289. mover(timestamp);
  290. }return timestamp;
  291. }
  292. function getWeekdaySkips(weekdays) {
  293. var skips = [1, 1, 1, 1, 1, 1, 1];
  294. var filled = [0, 0, 0, 0, 0, 0, 0];
  295. for (var i = 0; i < weekdays.length; i++) {
  296. filled[weekdays[i]] = 1;
  297. }
  298. for (var k = 0; k < DAYS_IN_WEEK; k++) {
  299. var skip = 1;
  300. for (var j = 1; j < DAYS_IN_WEEK; j++) {
  301. var next = (k + j) % DAYS_IN_WEEK;
  302. if (filled[next]) {
  303. break;
  304. }
  305. skip++;
  306. }
  307. skips[k] = filled[k] * skip;
  308. }
  309. return skips;
  310. }
  311. function createDayList(start, end, now, weekdaySkips) {
  312. var max = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 42;
  313. var min = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
  314. var stop = getDayIdentifier(end);
  315. var days = [];
  316. var current = copyTimestamp(start);
  317. var currentIdentifier = 0;
  318. var stopped = currentIdentifier === stop;
  319. if (stop < getDayIdentifier(start)) {
  320. return days;
  321. }
  322. while ((!stopped || days.length < min) && days.length < max) {
  323. currentIdentifier = getDayIdentifier(current);
  324. stopped = stopped || currentIdentifier === stop;
  325. if (weekdaySkips[current.weekday] === 0) {
  326. current = nextDay(current);
  327. continue;
  328. }
  329. var day = copyTimestamp(current);
  330. updateFormatted(day);
  331. updateRelative(day, now);
  332. days.push(day);
  333. current = relativeDays(current, nextDay, weekdaySkips[current.weekday]);
  334. }
  335. return days;
  336. }
  337. function createIntervalList(timestamp, first, minutes, count, now) {
  338. var intervals = [];
  339. for (var i = 0; i < count; i++) {
  340. var mins = (first + i) * minutes;
  341. var int = copyTimestamp(timestamp);
  342. intervals.push(updateMinutes(int, mins, now));
  343. }
  344. return intervals;
  345. }
  346. function createNativeLocaleFormatter(locale, getOptions) {
  347. var emptyFormatter = function emptyFormatter(_t, _s) {
  348. return '';
  349. };
  350. if (typeof Intl === 'undefined' || typeof Intl.DateTimeFormat === 'undefined') {
  351. return emptyFormatter;
  352. }
  353. return function (timestamp, short) {
  354. try {
  355. var intlFormatter = new Intl.DateTimeFormat(locale || undefined, getOptions(timestamp, short));
  356. var time = padNumber(timestamp.hour, 2) + ':' + padNumber(timestamp.minute, 2);
  357. var date = timestamp.date;
  358. return intlFormatter.format(new Date(date + 'T' + time + ':00+00:00'));
  359. } catch (e) {
  360. return '';
  361. }
  362. };
  363. }
  364. //# sourceMappingURL=timestamp.js.map