KellysOnTop23
KellysOnTop23

Reputation: 1435

Compensate for time zones in automated emails

So I have an application that has two types of users. Whenever these users are matched will get an email as soon as they're matched and then I wanted another email to be sent at a future date and time that I would save in my database (MySQL). My Issue is with the times to set for the automated email (using sendgrid as transport). I'm trying to make a method to calculate the right time for each user depending on their time zone (which they enter in a database per form) so that it sends at the same moment, based on the time saved. I guess I'm not understanding this correctly. Can someone explain to me how to get this done. This is what I have so far.

Date.prototype.scheduleDate = function(day, time, tz)
{
    var dat = new Date(this.valueOf());
    do{
        dat.setDate(dat.getDate() + 1);
    }
    while(dat.getDay() !== day);
    if(time === 'Morn'){
        switch(tz){
            case 'eastern':
                dat.setHours(10,30,0,0);
                break
            case 'central':
                dat.setHours(9,30,0,0);
                break
            case 'mountain':
                dat.setHours(8,30,0,0);
                break
            case 'pacific':
                dat.setHours(7,30,0,0);
                break
        }
    }else if(time === 'Noon'){
        switch(tz){
            case 'eastern':
                dat.setHours(15,30,0,0);
                break
            case 'central':
                dat.setHours(14,30,0,0);
                break
            case 'mountain':
                dat.setHours(13,30,0,0);
                break
            case 'pacific':
                dat.setHours(12,30,0,0);
                break
        }
    }else if(time === 'Eve'){
        switch(tz){
            case 'eastern':
                dat.setHours(19,30,0,0);
                break
            case 'central':
                dat.setHours(18,30,0,0);
                break
            case 'mountain':
                dat.setHours(17,30,0,0);
                break
            case 'pacific':
                dat.setHours(16,30,0,0);
                break
        }
    }else if(time === 'Night'){
        switch(tz){
            case 'eastern':
                dat.setHours(22,0,0,0);
                break
            case 'central':
                dat.setHours(21,0,0,0);
                break
            case 'mountain':
                dat.setHours(20,0,0,0);
                break
            case 'pacific':
                dat.setHours(19,0,0,0);
                break
        }
    }
    return dat;
}

Upvotes: 1

Views: 367

Answers (1)

Dan Nagle
Dan Nagle

Reputation: 5425

If your server uses UTC then clock changes due to Daylight Saving Time are not a concern. Client side time difference should be of no concern.

If you schedule an email to be sent by the server at 00:01 UTC, a recipient in New York will see it was sent at 19:01 (EST / UTC -5 Hours) and a recipient in San Francisco will see it was sent at 16:01 (PST / UTC -8 Hours). It's the exact same time.

Setting Linux Server Time Zone

Setting the server time zone depends on the server OS, I'm assuming the server is running Linux. Open a console on the server, the date command will give you the local time and time zone information.

$ date
Wed Nov  9 06:36:54 UTC 2016

If the system isn't using UTC it's easy to change.

Ubuntu: sudo dpkg-reconfigure tzdata

RedHat: sudo redhat-config-date

In the menu select "None of the above" or "Etc" and then "UTC".

Alternatively you can manually change the time zone by copying/linking the appropriate time zone information file.

$ ls /usr/share/zoneinfo
Africa      Cuba     GMT0         Japan              Pacific     Turkey
America     EET      GMT-0        Kwajalein          Poland      UCT
Antarctica  Egypt    GMT+0        leap-seconds.list  Portugal    Universal
Arctic      Eire     Greenwich    Libya              posix       US
Asia        EST      Hongkong     localtime          posixrules  UTC
Atlantic    EST5EDT  HST          MET                PRC         WET
Australia   Etc      Iceland      Mexico             PST8PDT     W-SU
Brazil      Europe   Indian       MST                right       zone.tab
Canada      Factory  Iran         MST7MDT            ROC         Zulu
CET         GB       iso3166.tab  Navajo             ROK
Chile       GB-Eire  Israel       NZ                 Singapore
CST6CDT     GMT      Jamaica      NZ-CHAT            SystemV

You want to change /etc/localtime to use the UTC information.

$ sudo unlink /etc/localtime 
$ sudo ln -s /usr/share/zoneinfo/UTC /etc/localtime

Setting MySQL Time Zone

Identify the system time zone, this is the time zone that was set when the server started:

SELECT @@system_time_zone;

Get the timestamp for the current session:

SELECT CURRENT_TIMESTAMP;

Get the UTC timestamp:

SELECT UTC_TIMESTAMP();

Get the time difference between the current session and UTC:

SELECT TIMEDIFF(CURRENT_TIMESTAMP, UTC_TIMESTAMP);

You can set the time zone for the current session by using an offset from UTC.

SET TIME_ZONE = '+00:00';

If you want to make the change permanent you'll have to find and edit the MySQL configuration file my.cnf and set the default time zone. Its location will depend upon how MySQL was installed on your system.

default_time_zone='+00:00'

Restart the server after making the change and use the above commands to verify the change.

Upvotes: 1

Related Questions