Converting UTC to local Timezone in Azure WebRole - asp.net-mvc

Quick environment overview:
I have an ASP.NET MVC/AngularJS application that is ultimately being hosted as an Azure Webrole. The data is stored in MongoDB hosted in the same Azure data center (via MongoLab). Development environment and staging environment (Azure) point to same MongoDB database and thus see the exact same data.
Issue:
Individual users of the application are stored with their preferred timezone as a string (TimezoneID). All dates in the app are stored as UTC times. Conversion to and from UTC times to a users specific timezone is done on the server via an extension method which ultimately calls:
return TimeZoneInfo.ConvertTimeFromUtc(UtcDate, _lookup[TimeZoneID]);
Nowhere in the entire app is there a DateTime.Now or any other date conversion I would expect to use the server time settings.
In a particular area of the application a date is being stored in Mongo (UTC). I can confirm that the date is correctly stored as UTC (based on the offset from my local time). When I retrieve that date from the data store and convert it back to my local time (Central Standard Time) I step over the above line of code and the conversion works fine and the local time is set. The result is sent to the client's browser as part of a Json result of an API call and displayed to the user correctly. During transport the MVC Json serializer parses the datetime into a format similar to:
EventDate: "/Date(1399407153971)/"
I parse this in Javascript and display accordingly and all is good.
When I deploy the exact same code to Azure (which points to the exact same data store in Mongo) the result that comes down via the API call appears to have applied the UTC transformation twice (meaning the regular 6 hour UTC offset is applied twice and the displayed date is 12 hours earlier than UTC...6 hours off of what I want).
I've updated the stored time in Mongo by a minute or two to make sure both sides update and they do (verifying the same data). I've modified the date/time settings of my local machine to see if it has any effect on my local results and it does not. I've changed the timezone for the selected user in the app and both my local result AND the Azure returned result adjust by the UTC offset difference between Central Standard Time and the new time zone (meaning if I switch to Mountain Time the result of both the local result and the Azure result adjust by 1 hour...but are still 6 hours off). I've confirmed via Chrome Dev Tools that the date in question that is retrieved at the client (via Json) is different based on whether the site hit was the remove (Azure) environment or local environment (meaning it isn't being modified incorrectly anywhere on the client after-the-fact)
Here is the most telling thing:
If I convert the date to a string prior to returning it to the client the string sent down appears correct in the Json response. If I leave it as a .NET DateTime type (as a property of a complex object) the Json serializer appears to translate it again but ONLY when hosted in Azure. This has pointed me to the issue being somehow related to the DateTimeKind of the date I'm returning. Although, at this point, I'm too lost in the weeks to see why the difference between my local environment and the Azure host environment in serialization and why one things a time is correct and the other converts it twice. If the DateKindTime was wrong wouldn't both environments perform the same conversion?
Thanks for reading through this and I'd appreciate any ideas to address anyone has.

It's difficult to say for sure without seeing more code, but could it be that the problem is with conversion on the way in, rather than on the way out? You said that you're using TimeZoneInfo.ConvertTimeFromUtc for the output. Are you also using TimeZoneInfo.ConvertTimeToUtc on the other side? If so, that's likely the source of picking up your local time zone.
You might also somewhere have a call to DateTime.ToUniversalTime, which again - uses the local time zone.
Another area which might be the culprit - when you retrieve your value from Mongo, check the Kind of the values coming back. If they're Unspecified, then when they are serialized to JSON, they will be treated as if they were Local. basically, there's a ToUniversalTime call going on under the hood. So you may need to explicitly call DateTime.SpecifyKind to set the value to Utc before it goes out the door.
With specific regards to Azure, it follows best practices of setting the server time zone to UTC. You could try that on your own machine and see if you get similar results.
Of course, you really don't want the server's time zone to influence the result at all.
You may want to consider creating a new project with a minimal, complete, and verifiable example. That will help you verify your assumptions, and will likely track down the source of the problem. If it still fails, well then you'll be in much better shape for asking for help.

That was a long post :)
If I were you I would transfer all dateTimes in UTC format. The server basically must work with UTC time as far as I'm concerned. Transformation to client time must be applied at clients side. You can use momentjs library that proved to be very fine library for working with dates and times.
On client side do something like this
var date = {
utc: '/Date(1399407153971)/', //time that you have recieved
offset: 240
}
var localTime = moment.utc(date.utc).zone(date.offset).format('DD/MM/YYYY hh:mm');
I hope this helps
UPD
As Matt Johnson pointed out you can make it even shorter for current time zone.
var localTime = moment('/Date(1399407153971)/').format('DD/MM/YYYY hh:mm');

Related

How to use Dart's Difference Method and DateTime using a dynamic date?

The application I'm building uses a sort of "Days since" counter using Duration Difference and DateTime, but I want the counter to begin on the day they started using the app by default, and have the option to put in whichever date they desire.
My Question is, how do i dynamically generate the start date of the counter to be the day they start using the app, and would this be easiest achieved with a database?
Ok, so from my point of view a good way to do it seems to use shared_preferences. Shared preferences allow to store some key/value pair for the user, it's persistent and you can update it.
Here's the package: https://pub.dartlang.org/packages/shared_preferences
Here's a good article giving you details about it : https://www.didierboelens.com/2018/04/shared-preferences---how-to-save-flutter-application-settings-and-user-preferences-for-later-re-use/
The idea would be to create a function, checking if the key of first_use_date exists.
If it does do nothing (or you can actually update it)
If not you create it with the date of today.
Of course it's persistent since the app stays installed on the device.
For printing duration I let you check this class : https://docs.flutter.io/flutter/intl/DateFormat-class.html
And especially this method (which is not implemented yet) : https://docs.flutter.io/flutter/intl/DateFormat/formatDurationFrom.html
Hope it's helps !!

Ical.Net: Event's AsUtc flag is not updating correctly

I am facing this issue in Ical.Net Version=4.0.1.0 and lower, When we get "GMT-0500" in DTSTART/DTEND then it is giving same utc time as local time in AsUtc. But If I replace "GMT-0500" to "Canada/Eastern" manually in ics its giving correct utc time in AsUtc.
Following is not working fine -:
DTSTART;TZID=GMT-0500:20181213T190000
DTEND;TZID=GMT-0500:20181213T220000
Following works fine -:
DTSTART;TZID=Canada/Eastern:20181213T190000
DTEND;TZID=Canada/Eastern:20181213T220000
GMT-0500 isn't a valid IANA time zone. Here's the list of IANA time zones. You want Etc/GMT+5.
Canada/Eastern is deprecated. I suggest you use America/Toronto instead.
In general, I suggest you familiarize yourself with the actual time zone names instead of guessing as to what they might be, and hoping for the best.
There were also some bugs with caching AsUtc values, as I noted in the release notes. I suggest upgrading to 4.latest. It should be backwards compatible.

ToLocalTime shows wrong time

I am running a website in IIS on my local PC, I save a date as UTC and call .ToLocalTime() on it in the front end mvc website. If I run the website on my local PC it outputs the correct date and time adjusted for DST. If I run it in Azure in region UK West I get 1 hour behind. This hasn't been an issue until today i.e. the clocks changed on Saturday. Do I need to raise a ticket with Azure? Or am I doing it wrong?
Azure App server is using UTC time. If your site is not global just for UK. We can use the following code to get the UK local time.
DateTime utcNow = DateTime.UtcNow;
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); //UK time zone
DateTimeOffset time = TimeZoneInfo.ConvertTimeFromUtc(utcNow, tz);
lab_time.Text = time.DateTime.ToString(CultureInfo.InvariantCulture); //display the datetime with label.
If your website is to be global that you may need to use javascrpt to do that, more detail please refer to another SO Thread.
Time-zone depends on the the server location literally.
I had faced the same issue. I had my server in Singapore and it used to clash with my zone, India.
It would affect the time and date formats too.
So, Better not use the ToLocalTime() at the server side. just get the UTC time from the server and convert to whatever local time at the client side. Issue resolved!
Hope this helps. Thank you.

How to create Youtube Live Event with all options

I wrote a .NET program that creates live events in Youtube, but I cannot set the correct starting time and some settings like Keywords, and Region. This is what I do, when I create the Broadcast I put the starting Date and Time this way:
oLiveBroadcast.Snippet.ScheduledStartTime = String.Format(CDate(txtScheduledStart.Text), "yyyy-MM-ddTHH:mm:sszzzz")
...but the GMT is always referred to US (GMT -8). I think that the problem is in the receiving field .Snippet.ScheduledStartTime which has a dateTime format not including GMT. What can I do?
I also didn't find how and whare to set Region (in my case Itali IT-it) and searching Keywords
Thanks

generating ics file programmatically but never able to produce what timezone invitation was produced

Hi I was trying send meeting invites through my asp.net MVC application. There was a requirement to show which timezone this invite was created as in following image. I tried various things as told by Microsoft support, neither of them worked.
As per Microsoft Exchange Server support's advice I created following.
TZID:Sri Jayawardenepura
BEGIN:STANDARD
TZOFFSETFROM:+0530
TZOFFSETTO:+0530
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=Sri Jayawardenepura:20140416T033000Z
DTSTAMP:20140327T113138Z
DTEND;TZID=Sri Jayawardenepura:20140416T060000Z
LOCATION: Board room
After adding TZID inside DTSTART, it is not coming as invite to gmail.
but following works fine as invitation both in outlook configured with Exchange Server and webmails like gmail.
TZID:Sri Jayawardenepura
BEGIN:STANDARD
TZOFFSETFROM:+0530
TZOFFSETTO:+0530
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART:20140318T033000Z
DTSTAMP:20140327T122640Z
DTEND:20140318T060000Z
LOCATION: Board room
however that requirement of showing timezone where invite was created not fulfilled yet.
Can anyone help me. Thanks in advance.
If you look at http://www.kanzaki.com/docs/ical/dateTime.html it states that UTC time is identified by a Z suffix character as well as The TZID property parameter MUST NOT be applied to DATE-TIME properties whose time values are specified in UTC.
I would suspect that if you are using a timezone identifier that you need to remove the Z from the end of the date to get it to work properly.
EDIT: So, I was having troubles with timezones before as well, so I had only used UTC time, however I just got one to work with a timezone, so I hope this helps. Inside VTIMEZONE, you have a STANDARD, but you do not have a DTSTART inside the STANDARD. Again, quoting from the above link, The standard or daylight component MUST include the "DTSTART", "TZOFFSETFROM" and "TZOFFSETTO" properties.
It is not RFC5545 compliant but many calendars will expect a TZID to be compatible with the Olson DB and also may expect to have the calendar property X-WR-TIMEZONE set.
What I have seen is that when doing all of above you increase your calendar compatibility ratio.

Resources