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 11KB

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