admin
1 week ago
front
1 week ago
actions.js
2 weeks ago
latecheckbox.js
1 year ago
lateselect.js
1 year ago
notifications.js
1 year ago
shared.js
1 year ago
time.js
1 year ago
time.js
104 lines
| 1 | function latepoint_is_timeframe_in_periods(timeframe_start, timeframe_end, periods_arr) { |
| 2 | var is_inside = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; |
| 3 | |
| 4 | for (var i = 0; i < periods_arr.length; i++) { |
| 5 | |
| 6 | var period_start = 0; |
| 7 | var period_end = 0; |
| 8 | var buffer_before = 0; |
| 9 | var buffer_after = 0; |
| 10 | |
| 11 | var period_info = periods_arr[i].split(':'); |
| 12 | if (period_info.length == 2) { |
| 13 | period_start = period_info[0]; |
| 14 | period_end = period_info[1]; |
| 15 | } else { |
| 16 | buffer_before = period_info[2]; |
| 17 | buffer_after = period_info[3]; |
| 18 | period_start = parseFloat(period_info[0]) - parseFloat(buffer_before); |
| 19 | period_end = parseFloat(period_info[1]) + parseFloat(buffer_after); |
| 20 | } |
| 21 | if (is_inside) { |
| 22 | if (latepoint_is_period_inside_another(timeframe_start, timeframe_end, period_start, period_end)) { |
| 23 | return true; |
| 24 | } |
| 25 | } else { |
| 26 | if (latepoint_is_period_overlapping(timeframe_start, timeframe_end, period_start, period_end)) { |
| 27 | return true; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | function latepoint_is_period_overlapping(period_one_start, period_one_end, period_two_start, period_two_end) { |
| 35 | // https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/ |
| 36 | return period_one_start < period_two_end && period_two_start < period_one_end; |
| 37 | } |
| 38 | |
| 39 | function latepoint_is_period_inside_another(period_one_start, period_one_end, period_two_start, period_two_end) { |
| 40 | return period_one_start >= period_two_start && period_one_end <= period_two_end; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // Converts time in minutes to hours if possible, if minutes also exists - shows minutes too |
| 45 | function latepoint_minutes_to_hours_preferably(time) { |
| 46 | var army_clock = latepoint_is_army_clock(); |
| 47 | |
| 48 | var hours = Math.floor(time / 60); |
| 49 | if (!army_clock && hours > 12) hours = hours - 12; |
| 50 | |
| 51 | var minutes = time % 60; |
| 52 | if (minutes > 0) hours = hours + ':' + minutes; |
| 53 | return hours; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | function latepoint_minutes_to_hours(time) { |
| 58 | var army_clock = latepoint_is_army_clock(); |
| 59 | |
| 60 | var hours = Math.floor(time / 60); |
| 61 | if (!army_clock && hours > 12) hours = hours - 12; |
| 62 | return hours; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | function latepoint_am_or_pm(minutes) { |
| 67 | if (latepoint_is_army_clock()) return ''; |
| 68 | return (minutes < 720 || minutes == 1440) ? 'am' : 'pm'; |
| 69 | } |
| 70 | |
| 71 | function latepoint_hours_and_minutes_to_minutes(hours_and_minutes, ampm) { |
| 72 | var hours_and_minutes_arr = hours_and_minutes.split(':'); |
| 73 | var hours = hours_and_minutes_arr[0]; |
| 74 | var minutes = hours_and_minutes_arr[1]; |
| 75 | if (ampm == "pm" && hours < 12) hours = parseInt(hours) + 12; |
| 76 | if (ampm == "am" && hours == 12) hours = 0; |
| 77 | minutes = parseInt(minutes) + (hours * 60); |
| 78 | return minutes; |
| 79 | } |
| 80 | |
| 81 | function latepoint_get_time_system() { |
| 82 | return latepoint_helper.time_system; |
| 83 | } |
| 84 | |
| 85 | function latepoint_is_army_clock() { |
| 86 | return (latepoint_get_time_system() == '24'); |
| 87 | } |
| 88 | |
| 89 | function latepoint_minutes_to_hours_and_minutes(minutes) { |
| 90 | var army_clock = latepoint_is_army_clock(); |
| 91 | var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '%02d:%02d'; |
| 92 | |
| 93 | var hours = Math.floor(minutes / 60); |
| 94 | if (!army_clock && (hours > 12)) hours = hours - 12; |
| 95 | if (!army_clock && hours == 0) hours = 12; |
| 96 | minutes = minutes % 60; |
| 97 | // Check if sprintf is available (either native or from a library) |
| 98 | if (typeof sprintf === 'function') { |
| 99 | return sprintf(format, hours, minutes); |
| 100 | } else { |
| 101 | return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; |
| 102 | } |
| 103 | } |
| 104 |