Timezones
Re: Timezones
It seems to be a rather simple feature, to be honest. Just a little JavaScript.
Pay more attention to detail.
Re: Timezones
Spent an hour typing this up, don't feel like contributing on this anymore.
Would look something like this:
// Time Strings
// ============
// Ease interactions between users from different timezones.
//
// Problem
// -------
// - Barrier of entry
// - Error prone:
// (+/-), (AM/PM) are easy to miscalculate
// - Timezone changes:
// Unless a GMT time is produced through a timezone calculator (produced
// without a corresponding date), the local time a user has determined based
// on the current GMT offset will be incorrect.
//
// Research
// --------
// - The current convention observed among users is to prefix "gmt" to a time
// string (source: RO-16 scheduling).
//
// Solutions
// ---------
// - Timezone changes:
// [N] Date picker UI:
// Although a date picker would ensure the date of the time string, this
// approach would be too cumbersome for users.
// [Y] Detect whether there will be a timezone change, and provide multiple
// time strings.
//
// Design
// ------
// - Exclusive to GMT time strings:
// - No support for local to GMT; determining the timezone based on the
// context is not reliable.
// - If there is a timezone context (for example, local time), then both
// parties don't require a translation.
// - Implement client-side:
// - Posts should be stored and rendered in their original format, "add-on"
// features should be implemented client-side.
// - Fail-safe detection:
// - Let date.parse() determine whether the time format is valid and usable.
// Example: `Date.parse(${detectedTimestring})`
// - Timezone changes:
// - Provide local times for timezone changes within the next 2 weeks.
// - Distinguish helptext from a user's content
//
// Logic
// -----
// 1. Set local time from preferences (not reliable to determine)
// 2. Get posts
// 3. Search for GMT timestrings within posts
// 4. Filter duplicate timestrings
// 5. Try to parse (remove possible false positives)
// 6. Detect future timezone changes by comparing the timezone offset
// 7. Replace timestrings in posts
//
// Dependencies
// ------------
// - moment-timezone (https://github.com/moment/moment-timezone/)
Needs some refactoring:
Would look something like this:
// Time Strings
// ============
// Ease interactions between users from different timezones.
//
// Problem
// -------
// - Barrier of entry
// - Error prone:
// (+/-), (AM/PM) are easy to miscalculate
// - Timezone changes:
// Unless a GMT time is produced through a timezone calculator (produced
// without a corresponding date), the local time a user has determined based
// on the current GMT offset will be incorrect.
//
// Research
// --------
// - The current convention observed among users is to prefix "gmt" to a time
// string (source: RO-16 scheduling).
//
// Solutions
// ---------
// - Timezone changes:
// [N] Date picker UI:
// Although a date picker would ensure the date of the time string, this
// approach would be too cumbersome for users.
// [Y] Detect whether there will be a timezone change, and provide multiple
// time strings.
//
// Design
// ------
// - Exclusive to GMT time strings:
// - No support for local to GMT; determining the timezone based on the
// context is not reliable.
// - If there is a timezone context (for example, local time), then both
// parties don't require a translation.
// - Implement client-side:
// - Posts should be stored and rendered in their original format, "add-on"
// features should be implemented client-side.
// - Fail-safe detection:
// - Let date.parse() determine whether the time format is valid and usable.
// Example: `Date.parse(${detectedTimestring})`
// - Timezone changes:
// - Provide local times for timezone changes within the next 2 weeks.
// - Distinguish helptext from a user's content
//
// Logic
// -----
// 1. Set local time from preferences (not reliable to determine)
// 2. Get posts
// 3. Search for GMT timestrings within posts
// 4. Filter duplicate timestrings
// 5. Try to parse (remove possible false positives)
// 6. Detect future timezone changes by comparing the timezone offset
// 7. Replace timestrings in posts
//
// Dependencies
// ------------
// - moment-timezone (https://github.com/moment/moment-timezone/)
Needs some refactoring:
Code: Select all
// TODO: import moment from 'moment-timezone';
// (2)
// TODO: Need a selector for only scheduling threads
const posts = document.querySelectorAll('.post .content');
if (posts) {
// (3)
const timestringPosts = [];
let timestringMatches = [];
posts.forEach((post) => {
// TODO: Need a better regexp for gmt in front as well
const matches = post.match(/\b\d?\d(:\d\d)?( ?PM|AM)? ?gmt\b/gi);
if (matches.length) {
timestringPosts.push(post);
timestringMatches.concat(matches);
}
});
if (timestringPosts.length && timestringMatches.length) {
// (4)
const timestringMatchesUnique = [...new Set(timestringMatches)];
const timestrings = {};
// (5)
timestringMatchesUnique.forEach((timestring) => {
const parsed = moment(timestring);
if (parsed) {
timestrings[timestring] = parsed;
}
});
if (timestrings.length) {
// (6)
const clientDate = new moment();
const clientTimezoneOffset = clientDate.utcOffset();
const changedDates = [];
// Start at day = 1 (next day)
for (const day = 1; day < 14, day++) {
const futureDate = clientDate.add(day, 'days');
if (futureDate.utcOffset() !== clientTimezoneOffset) {
changedDates.push(futureDate);
}
}
// TODO: Set timezone and format based on user's preferences
const timeFormat = 'hh:mm PST';
const helpData = {};
timestrings.forEach((timestring) => {
helpData[timestring] = {};
helpData[timestring].current = moment(timestring).format(timeFormat);
if (changedDates.length) {
changedDates.forEach((changedDate) => {
helpData[timestring][changedDate.format(timeFormat)] = changedDate
.format(timestring).format(timeFormat);
});
}
});
// (7)
if (helpData.length) {
// TODO: Refactoring needed, need posts after (5)
timestringPosts.forEach((post) => {
// Replace with helpData
});
}
}
}
}
- Attachments
-
- helptext-timestrings.js
- (4.17 KiB) Downloaded 15 times
-
- Ninja
- Posts: 13004
- Joined: Apr 28, 2020
Re: Timezones
Lol dis guy. Can write entire code that will calculate right time for him, but cant figure out the right time himself :p op haha
Well done, no joke.
Well done, no joke.
Re: Timezones
Final refactor for me:
I haven't actually run this code yet, so there are probably runtime errors, but I fixed other stuff.
I haven't actually run this code yet, so there are probably runtime errors, but I fixed other stuff.
Code: Select all
import moment from 'moment-timezone';
// User-defined preferences
const timezone = 'America/Los_Angeles';
const dateFormat = 'DD MMM YYYY';
const timeFormat = 'h:mmA z';
// TODO: Refactor control flow for objects relying on `timeMatch` (functional)
// (1) Get user posts on scheduling pages
const posts = document.querySelectorAll('.post .content');
if (posts) {
// (2) Find GMT timestring matches within posts
const timeMatchedPosts = [];
posts.forEach((post) => {
const gmt = 'gmt';
const time = '\\d?\\d(:\\d\\d)?( ?am|pm)?';
const gmtTime = `(${gmt} ?${time}|${time} ?${gmt})`;
const matches = post.match(new RegExp(`\b${gmtTime}\b`, 'gi'));
if (matches.length) {
timeMatchedPosts.push({ matches, post });
}
});
if (timeMatchedPosts.length) {
// (3) Filter timestring matches for duplicates and invalid formats
const timeMatchesUnique = new Set(timeMatchedPosts.map(post => post.matches));
const times = {};
timeMatchesUnique.forEach((timeMatch) => {
const time = moment(timeMatch);
if (time) {
times[timeMatch] = time;
}
});
if (times.length) {
// (4) Detect future time changes for the timezone in the next 2 weeks
const currentDate = moment().tz(timezone);
const currentDateOffset = currentDate.utcOffset();
const offsetChangeDates = [];
// Start at next day (day = 1)
for (let day = 1; day <= 15; day += 1) {
const futureDate = currentDate.add(day, 'days');
if (futureDate.utcOffset() !== currentDateOffset) {
offsetChangeDates.push(futureDate);
}
}
// '6:00 pm gmt': {
// '30 Oct 2016': '5:00 PM PST'
// }
const offsetChangeTimes = {};
times.forEach((time, timeMatch) => {
offsetChangeTimes[timeMatch] = {};
// Refactor?
offsetChangeDates.forEach((futureDate) => {
offsetChangeTimes[timeMatch][futureDate.format(dateFormat)] =
futureDate.format(timeFormat);
});
});
// (5) Replace timestrings in posts with valid GMT timestrings
const timeMatches = Object.keys(times);
const timePosts = timeMatchedPosts.filter(timeMatchedPost =>
timeMatchedPost.matches.filter(match => timeMatches.includes(match))
);
timePosts.forEach((timePost) => {
const { matches, post } = timePost;
matches.forEach((match) => {
let localTimestring = times[match].format(timeFormat);
if (offsetChangeTimes(match).length) {
// Append potential time changes
// Format? 3:00 PM PST (or 2:00 PM PST on or after 30 Oct 2016)
// localTimestring = localTimestring + offsetChangeTimes[match].reduce(...)
}
post.replace(
new RegExp(`${match}`, 'gi'),
`$& <span class="help-localtime">${localTimestring}</span>`
);
});
});
}
}
}
- Attachments
-
- help-time.js
- (4.78 KiB) Downloaded 17 times
Re: Timezones
Imo not worth putting dev hours into, GMT is the standard and everyone knows how far they are from it. If they don't, they only need to look it up once then they know... Besides, in-post replacement sounds nice in theory but it relies on people adhering to a specific format.. It might end up confusing more people than it would help.
- macacoalbino
- Howdah
- Posts: 1305
- Joined: Apr 2, 2015
- ESO: MacacoAlbino
- Clan: 3Huss
Re: Timezones
Big problem with timezones is that I have no idea who's in Daylight Savings(DS) and who isn't. All I know is when it starts/end in my own country.
Btw, not all states in Brazil use DS, it's a fucking mess.
Btw, not all states in Brazil use DS, it's a fucking mess.
Re: Timezones
That's exactly what the problem would be without time-zones. Comparing in terms of GMT, such factors become irrelevant.macacoalbino wrote:Big problem with timezones is that I have no idea who's in Daylight Savings(DS) and who isn't. All I know is when it starts/end in my own country.
Btw, not all states in Brazil use DS, it's a fucking mess.
- macacoalbino
- Howdah
- Posts: 1305
- Joined: Apr 2, 2015
- ESO: MacacoAlbino
- Clan: 3Huss
- macacoalbino
- Howdah
- Posts: 1305
- Joined: Apr 2, 2015
- ESO: MacacoAlbino
- Clan: 3Huss
Re: Timezones
macacoalbino wrote:Brazil [...], it's a fucking mess.
After all the shit I've seen on Liveleak, I don't get why anyone with the option to not live in Brazil would live in Brazil. Can you enlighten me?
Pay more attention to detail.
- macacoalbino
- Howdah
- Posts: 1305
- Joined: Apr 2, 2015
- ESO: MacacoAlbino
- Clan: 3Huss
- lemmings121
- Jaeger
- Posts: 2673
- Joined: Mar 15, 2015
- ESO: lemmings121
Re: Timezones
macacoalbino wrote:Big problem with timezones is that I have no idea who's in Daylight Savings(DS) and who isn't. All I know is when it starts/end in my own country.
Btw, not all states in Brazil use DS, it's a fucking mess.
you dont have to know everyones 'ds', just google yours, and convert your time to gmt. and let the other guy do the same...
Re: Timezones
GMT and UTC are the same time-zone, and DST applies to neither one. The only difference is what time of the day the two zones start, IIRC – one is practical for astronomers, and the other for plebs.forgrin wrote:Jerom wrote:Isnt the daylight savingtime switch thing coming in soon? Thats going to confuse the fuck about people again.
Well technically for all the people scheduling for this tourney on GMT, GMT hasn't been in effect until I think yesterday (?) so... glad that worked in the end. We should be just using UTC to schedule stuff.
Who is online
Users browsing this forum: No registered users and 1 guest