Timezones

User avatar
Sweden Gendarme
Gendarme
Donator 03
Posts: 5132
Joined: Sep 11, 2016
ESO: Gendarme

Re: Timezones

Post by Gendarme »

It seems to be a rather simple feature, to be honest. Just a little JavaScript.
Pay more attention to detail.
United States of America urmom69
Crossbow
Posts: 38
Joined: Sep 1, 2016

Re: Timezones

Post by urmom69 »

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:

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
No Flag deleted_user0
Ninja
Posts: 13004
Joined: Apr 28, 2020

Re: Timezones

Post by deleted_user0 »

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.
United States of America urmom69
Crossbow
Posts: 38
Joined: Sep 1, 2016

Re: Timezones

Post by urmom69 »

Final refactor for me:

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
User avatar
Netherlands Goodspeed
Retired Contributor
Posts: 13002
Joined: Feb 27, 2015

Re: Timezones

Post by Goodspeed »

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.
User avatar
Brazil macacoalbino
Howdah
Posts: 1305
Joined: Apr 2, 2015
ESO: MacacoAlbino
Clan: 3Huss

Re: Timezones

Post by macacoalbino »

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.
Image

Image
User avatar
New Zealand zoom
Gendarme
Posts: 9314
Joined: Apr 26, 2015
ESO: Funnu
Location: New_Sweland

Re: Timezones

Post by zoom »

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.
That's exactly what the problem would be without time-zones. Comparing in terms of GMT, such factors become irrelevant.
User avatar
Brazil macacoalbino
Howdah
Posts: 1305
Joined: Apr 2, 2015
ESO: MacacoAlbino
Clan: 3Huss

Re: Timezones

Post by macacoalbino »

Always funnu
Image

Image
User avatar
Brazil macacoalbino
Howdah
Posts: 1305
Joined: Apr 2, 2015
ESO: MacacoAlbino
Clan: 3Huss

Re: Timezones

Post by macacoalbino »

Ppl in the forums aren't writing fucking scientific articles all the time, you know?
Image

Image
User avatar
Sweden Gendarme
Gendarme
Donator 03
Posts: 5132
Joined: Sep 11, 2016
ESO: Gendarme

Re: Timezones

Post by Gendarme »

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.
User avatar
Brazil macacoalbino
Howdah
Posts: 1305
Joined: Apr 2, 2015
ESO: MacacoAlbino
Clan: 3Huss

Re: Timezones

Post by macacoalbino »

I don't either...
Image

Image
User avatar
Brazil lemmings121
Jaeger
Posts: 2673
Joined: Mar 15, 2015
ESO: lemmings121

Re: Timezones

Post by lemmings121 »

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...
Image
User avatar
New Zealand zoom
Gendarme
Posts: 9314
Joined: Apr 26, 2015
ESO: Funnu
Location: New_Sweland

Re: Timezones

Post by zoom »

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.
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.

Who is online

Users browsing this forum: No registered users and 1 guest

Which top 10 players do you wish to see listed?

All-time

Active last two weeks

Active last month

Supremacy

Treaty

Official

ESOC Patch

Treaty Patch

1v1 Elo

2v2 Elo

3v3 Elo

Power Rating

Which streams do you wish to see listed?

Twitch

Age of Empires III

Age of Empires IV