-
Notifications
You must be signed in to change notification settings - Fork 0
Add wdtime.js to format timestamps in the client-side browser #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| wdtime.js | ||
|
|
||
| Copyright (C) 2025 Bedir Ekim | ||
|
|
||
| This file is part of WatchDuck. | ||
|
|
||
| WatchDuck is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License | ||
| as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
|
|
||
| WatchDuck is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
| warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License along with WatchDuck. | ||
| If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function() { | ||
| formatAllTimestamps(); | ||
| }); | ||
|
|
||
| /** | ||
| * Format all timestamp elements on the page. | ||
| */ | ||
| function formatAllTimestamps() { | ||
| document.querySelectorAll('[data-timestamp]').forEach(formatTimestampElement); | ||
|
|
||
| // Make sure the time zone notice is correct for users who don't have JS enabled | ||
| document.querySelectorAll('.tz-notice-nojs').forEach(e => e.style.display = 'none'); | ||
| document.querySelectorAll('.tz-notice-js').forEach(e => e.style.display = 'block'); | ||
| } | ||
|
|
||
| function formatTimestampElement(element) { | ||
| const timestamp = parseInt(element.getAttribute('data-timestamp'), 10) * 1000; | ||
| const date = new Date(timestamp); | ||
|
|
||
| const formatType = element.getAttribute('data-format-type') || 'datetime'; | ||
| const isRange = element.hasAttribute('data-end-timestamp'); | ||
|
|
||
| if (isRange) { | ||
| const endTimestamp = parseInt(element.getAttribute('data-end-timestamp'), 10) * 1000; | ||
| const endDate = new Date(endTimestamp); | ||
| element.textContent = formatDateRange(date, endDate); | ||
| } else if (formatType === 'datetime') { | ||
| element.textContent = formatDateTime(date, true); | ||
| } else if (formatType === 'lastupdated') { | ||
| element.textContent = 'Last updated: ' + formatDateTime(date, true); | ||
| } else if (formatType === 'ongoing') { | ||
| element.textContent = 'Down since ' + formatDateTime(date); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format a date in a user-friendly format. | ||
| * @param {Date} date - Date to format. | ||
| * @param {Boolean} includeTimezone - Whether to include timezone information. | ||
| * @param {Boolean} isOutageTime - Whether this is for an outage timestamp. | ||
| * @returns {string} Formatted date string. | ||
| */ | ||
| function formatDateTime(date, includeTimezone = false, isOutageTime = false) { | ||
| if (isOutageTime) { | ||
| const dateStr = date.toLocaleDateString(); | ||
| const timeStr = date.toLocaleTimeString(undefined, { | ||
| hour: '2-digit', | ||
| minute: '2-digit' | ||
| }); | ||
| return `${dateStr}, ${timeStr}`; | ||
| } | ||
|
|
||
| const options = { | ||
| year: 'numeric', | ||
| month: 'short', | ||
| day: 'numeric', | ||
| hour: '2-digit', | ||
| minute: '2-digit', | ||
| second: '2-digit' | ||
| }; | ||
|
|
||
| const formattedDateTime = date.toLocaleString(undefined, options); | ||
|
|
||
| if (includeTimezone) { | ||
| const tzString = getTimezoneString(date); | ||
| return `${formattedDateTime} ${tzString}`; | ||
| } | ||
|
|
||
| return formattedDateTime; | ||
| } | ||
|
|
||
| /** | ||
| * Get the timezone string (like GMT+2) for a date. | ||
| * @param {Date} date - The date to get timezone for. | ||
| * @returns {string} Timezone string. | ||
| */ | ||
| function getTimezoneString(date) { | ||
| const timezoneOffset = date.getTimezoneOffset(); | ||
| const offsetHours = Math.abs(Math.floor(timezoneOffset / 60)); | ||
| const offsetMinutes = Math.abs(timezoneOffset % 60); | ||
| const offsetSign = timezoneOffset <= 0 ? '+' : '-'; | ||
|
|
||
| if (offsetMinutes === 0) { | ||
| return `GMT${offsetSign}${offsetHours}`; | ||
| } else { | ||
| return `GMT${offsetSign}${offsetHours}:${offsetMinutes.toString().padStart(2, '0')}`; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format a date range. | ||
| * @param {Date} startDate - Start date. | ||
| * @param {Date} endDate - End date. | ||
| * @returns {string} Formatted date range. | ||
| */ | ||
| function formatDateRange(startDate, endDate) { | ||
| if (startDate.getFullYear() === endDate.getFullYear() && | ||
| startDate.getMonth() === endDate.getMonth() && | ||
| startDate.getDate() === endDate.getDate() && | ||
| startDate.getHours() === endDate.getHours() && | ||
| startDate.getMinutes() === endDate.getMinutes()) { | ||
|
|
||
| const formattedStart = formatDateTime(startDate, false, true); | ||
| return `${formattedStart} - Resolved under a minute :)`; | ||
| } | ||
|
|
||
| const formattedStart = formatDateTime(startDate, false, true); | ||
| const formattedEnd = formatDateTime(endDate, false, true); | ||
| return `${formattedStart} - ${formattedEnd}`; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.