Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current user timezone

I have been using Session.getTimeZone() to get the current user's timezone. I just noticed it has been deprecated, and I can't see anything that replicates this functionality. My users are located all over the US and dates/times should be formatted accordingly. How can I get the current user's timezone and not that of the script/owner?

The project in question is a standalone script.

like image 757
Greg Avatar asked Jan 11 '23 05:01

Greg


2 Answers

Greg, normal javascript seems to be working as intended this might be new in gas. The second example of code's function is returning the correct timezone offset for me. I'm currently using the Session.getTimeZone() in a gasOffsetScript. Question here might be a good read for the future.
Using Calendar Timezone to set event Timezone, not the users

This next code will get the users timezone based off their default Calendar Setting, if the script is running as the user.

function getUserTimeZone() {
  var userTimeZone = CalendarApp.getDefaultCalendar().getTimeZone();
  Logger.log(userTimeZone)
}

Or you could modify the following using normal javascript.

Code that is returning users timezone Stackoverflow question

function userTimeZone() {
  function pad(number, length){
    var str = "" + number
    while (str.length < length) {
        str = '0'+str
    }
    return str
}

var offset = new Date().getTimezoneOffset()
offset = ((offset<0? '+':'-')+ // Note the reversed sign!
          pad(parseInt(Math.abs(offset/60)), 2)+
          pad(Math.abs(offset%60), 2))
  Logger.log(offset)
}

Good luck brother, timezones are thorns.

like image 123
LennyZ71 Avatar answered Jan 23 '23 02:01

LennyZ71


In Gmail Add-ons you can get the user timezone and locale in buildAddOn parameters by activating the useLocaleFromApp. Check the documentation of Accessing User Locale and Timezone

Step to get the timezone and locale:

  1. In manifest add useLocaleFromApp with true "useLocaleFromApp": true
  2. Add the scope "https://www.googleapis.com/auth/script.locale"
  3. In buildAddOn you will receive the parameter userLocale and userTimezone

Manifest example:

{
  "timeZone": "Etc/GMT"
  "oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/gmail.addons.current.message.readonly", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/script.locale"],
  "gmail": {
    "name": "TestApp",
    "logoUrl": "https://storage.googleapis.com/xxxxxxx/favicon.ico",
    "contextualTriggers": [{
      "unconditional": {
      },
      "onTriggerFunction": "buildAddOn"
    }],
    "primaryColor": "#fea001",
    "secondaryColor": "#fea001",
    "openLinkUrlPrefixes": ["https://mail.google.com/"],
    "version": "TRUSTED_TESTER_V2",
    "useLocaleFromApp": true
  }
}

Add-ons example:

function buildAddOn(e) 
{
  var cards = [];

  var card = CardService.newCardBuilder().setName("Test timeZone");

  card.setHeader(CardService.newCardHeader().setTitle("test"));

  var section = CardService.newCardSection();

  section.addWidget( CardService.newTextParagraph().setText(e.userLocale));
  section.addWidget( CardService.newTextParagraph().setText(e.userTimezone.offSet));
  section.addWidget( CardService.newTextParagraph().setText(e.userTimezone.id));

  card.addSection(section);

  cards.push( card.build());

  return cards;
}

Caution with userTimezone.offSet parameter. The parameter have distinct case that the referenced in the documentation. The parameter is userTimezone.offSet with S uppercase

like image 37
Rony Bichler Avatar answered Jan 23 '23 01:01

Rony Bichler