1

I have a simple console application running as continuous webjob in Azure, Locally when I debug it gives proper output time in UTC when I convert time using new DateTime(Time).ToUniversalTime() but when I run it in webjob it still gives local time instead of universal time.

Console.WriteLine(MyData.Time.ToUniversalTime() + " "+ DateTime.Now.ToUniversalTime());

at 3:43 PM PST

Kudu console (webjob): 6/7/2017 3:38:46 PM 6/7/2017 10:43:52 PM

on local Console: 6/7/2017 10:38:46 PM 6/7/2017 10:43:52 PM

  • Please try to isolate to a non-WebJobs repo per [this page](https://github.com/projectkudu/kudu/wiki/Isolating-WebJobs-and-Deployment-script-issues). Also, what specific local time are you seeing in Azure? By default, local time is UTC on Azure VMs. – David Ebbo Jun 07 '17 at 20:05
  • Even if I convert local time i.e PST in UTC it still shows time in PST.. where as, if i run it locally the converted time show in UTC. – Radhika Bhala Jun 07 '17 at 21:28
  • Please do try to isolate as suggested. – David Ebbo Jun 07 '17 at 21:30
  • Tried the isolated one, it still shows me time in PST – Radhika Bhala Jun 07 '17 at 21:38
  • Can you add the full console app test code that you are using? Also, are you setting your timezone to a non-default value in your Azure Web App? – David Ebbo Jun 07 '17 at 21:40
  • I just tried a one-line console app with just `Console.WriteLine(DateTime.Now.ToUniversalTime());` and it printed UTC time. So no issue there. – David Ebbo Jun 07 '17 at 21:45
  • I did not change timezone settings.. Is there a way I can check if there are any settings through Azure portal? – Radhika Bhala Jun 07 '17 at 21:45
  • Did you try the one-line I gave above? If that doesn't print correct UTC time, please share the Web App name and I'll take a look. – David Ebbo Jun 07 '17 at 21:54
  • So in the above code Console.WriteLine(datum.Time.ToUniversalTime()+ " ? "+time) datum.Time.ToUniversalTime() gives out local time(PST) in webjob and Kudu console, where as in debug mode it gives out UTC as expected. Also to add 'time' in the code is in UTC so yes DateTime.Now.ToUniversalTime() gives expected result. – Radhika Bhala Jun 07 '17 at 21:58
  • Sorry, I don't follow this last comment. The code in your question is far more complicated than it needs to be, and most of it looks irrelevant to the question. Please change it to the minimal runnable code that reproduces the issue you are seeing. – David Ebbo Jun 07 '17 at 22:29
  • Tried this `Console.WriteLine(new DateTime(2017, 05, 23, 8, 00, 00, 0).ToUniversalTime() + " "+ DateTime.Now.ToUniversalTime());` Even this does not give expected result on Kudu Console, on Kudu Console: 5/23/2017 8:00:00 AM 6/7/2017 11:04:23 PM on Local Console: 5/23/2017 3:00:00 PM 6/7/2017 11:04:23 PM – Radhika Bhala Jun 07 '17 at 23:07

3 Answers3

3

The behavior is by design, and has nothing to do with Azure. When you write

var dt = new DateTime(2017, 05, 23, 8, 00, 00, 0);

It treats it as local time. So of course you get a different UTC time based on the timezone of the machine you're on (which in the case of Azure VMs happens to be the same as UTC time).

To treat the time you enter as UTC, you need to write:

var dt2 = DateTime.SpecifyKind(dt, DateTimeKind.Utc);

See also C# DateTime to UTC Time without changing the time

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • I get Time of my data in local time so i need to convert it in universal time and then specify its kind as Utc as below: So In Azure I want my time to be in UTC i.e for `LocalTime = new DateTime(2017, 05, 23, 8, 00, 00, 0);` it should print out 5/23/2017 3:00:00 PM so to achieve this `var dt = new DateTime(2017, 05, 23, 8, 00, 00, 0); var dt2 = DateTime.SpecifyKind(dt.ToUniversalTime(), DateTimeKind.Utc);` should work right? – Radhika Bhala Jun 08 '17 at 00:08
  • You say you get your data in local time, but you don't specify in what timezone. You cannot meaningfully work with local times without being clear on the time zone. If you want to set your web app to use a specific local time zone, user [WEBSITE_TIME_ZONE](https://github.com/projectkudu/kudu/wiki/Configurable-settings#set-the-time-zone). – David Ebbo Jun 08 '17 at 00:14
  • I changed the time of the website. Thanks for all the help. – Radhika Bhala Jun 08 '17 at 16:52
0

Tried this Console.WriteLine(new DateTime(2017, 05, 23, 8, 00, 00, 0).ToUniversalTime() + " "+ DateTime.Now.ToUniversalTime()); Even this does not give expected result on Kudu Console, on Kudu Console: 5/23/2017 8:00:00 AM 6/7/2017 11:04:23 PM on Local Console: 5/23/2017 3:00:00 PM 6/7/2017 11:04:23 PM

The differences between your local and Azure Web App instance is that the default time zone in Azure Web App instances is UTC. According the result of your test, the time zone on your local is UTC+07:00. If you create a new instance of DateTime by year/month/day/hour/month/minute, it will use the default time zone of your system.

It is not recommended to save a datetime to a string with this format 'year/month/day/hour/month/minute'. It will lose the time zone information. If your code runs on a machine with different time zone, it will cause some issues.

If you did need to store the datetime information in a string, you could store the datetime information with a time zone symbol(ex. 2017, 05, 23, 8, 00, 00, 00 UTC+07:00). To get the datetime back from the string, we need to add the offset hours.

var dateTime = new DateTime(2017, 05, 23, 8, 00, 00, 0, DateTimeKind.Utc).AddHours(7);

Or you could convert datetime to GMC time before save it. After that, you could create a new instance of DateTime with a DateTimeKind parameter.

var dateTime = new DateTime(2017, 05, 23, 8, 00, 00, 0, DateTimeKind.Utc);
Amor
  • 8,325
  • 2
  • 19
  • 21
0

I changed the timezone of the website on Azure portal. Thanks for all the help.

  • Since your issue is solved, please mark helpful replies as answer. It will help others quick find and fix similar problem. – Amor Jun 09 '17 at 03:03