Skip to content
Timezone issue with...
Clear all

Timezone issue with scheduler

2 Posts
2 Users
Posts: 2
Topic starter
Active Member
Joined: 1 year ago
How can I ensure scheduled reports go out based on users timezone? I have several scheduled reports setup, and they go out as expected, however, the user receives it based on our server's timezone instead of their own. 
Please advise.  
1 Reply
Posts: 33
Joined: 3 years ago
Hi Senthil
Please see the details below on one approach on how to send scheduled email per user's timezone. 
1. Make sure you set the value for UserIdForSchedule in the settings:
        public IActionResult GetUsersAndRoles()
            var settings = GetSettings();
            return Ok(new
                noAccount = string.IsNullOrEmpty(settings.AccountApiToken) || settings.AccountApiToken == "Your Public Account Api Token",
                users = settings.CanUseAdminMode ? settings.Users : new List<dynamic>(),
                userRoles = settings.CanUseAdminMode ? settings.UserRoles : new List<string>(),
                currentUserId = settings.UserId,
                currentUserRoles = settings.UserRoles,
                currentUserName = settings.UserName,
                allowAdminMode = settings.CanUseAdminMode,
                userIdForSchedule = settings.UserIdForSchedule, // <--- settings.UserIdForSchedule should be logged in user id
                dataFilters = settings.DataFilters,
                clientId = settings.ClientId
2. In DotNetReportJob.cs, when the scheduler api call comes back:
  var response = await client.GetAsync($"{apiUrl}/ReportApi/GetScheduledReports?account={accountApiKey}&dataConnect={databaseApiKey}&clientId={clientId}");
This response should have the same user id, and that basically gives you the user who saved the schedule. 
Next you would need to get the user's time zone based on the user Id. This is something you need to completely handle on your end, maybe add a timezone setting on your manage user page if it doesn't exist. 
3. Next, you need to update code in the scheduler to convert to user's time zone instead of local time zone. 
The chron.GetTimeAfter will basically calculate it off the "lastRun" value that you pass, which is time offset expected in UTC:
This method expects an UTC offset, and returns a UTC offset as well. 
The original code to convert it was:
schedule.NextRun = (nextRun.HasValue ? nextRun.Value.ToLocalTime().DateTime : (DateTime?)null);

 You would have to replace the above to convert to user's timezone. Something like this:
                             var chron = new CronExpression(schedule.Schedule);
                            var lastRun = !String.IsNullOrEmpty(schedule.LastRun) ? Convert.ToDateTime(schedule.LastRun) : DateTimeOffset.UtcNow.AddMinutes(-10);
                            // Get User's time zone
                            string userTimeZone = userService.GetTimeZoneByUserId(schedule.UserId); // <-- userService is just an example here
                            TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(userTimeZone);
                            // convert last run to user's local time zone
                            lastRun = TimeZoneInfo.ConvertTimeFromUtc(lastRun.UtcDateTime, timeZoneInfo);
                            var nextRun = chron.GetTimeAfter(lastRun);
                            // get current time in user's time zone
                            DateTime currentTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), timeZoneInfo);
                            schedule.NextRun = (nextRun.HasValue ? nextRun.Value.ToLocalTime().DateTime : (DateTime?)null);
                            if (schedule.NextRun.HasValue && currentTime>= schedule.NextRun && (!String.IsNullOrEmpty(schedule.LastRun) || lastRun <= schedule.NextRun))
                                // need to run this report
This would be the way to run the scheduled report by user's time zone. Let me know if you have any questions about this. 
I'm looking in to the Join issue as well, and will let you know soon. 
Thank you
Dotnet Report Team