My asp core 2.0 web api application stores DateTime in UTC format. I have user TimeZone information available in every request through user preference settings (stored in cookies).
The problem is that I need to return to user DateTime in his TimeZone instead of UTC. Do we have some build in utilities to do so?
Currently I follow the next flow:
User sends request with his preferences -> I retrieve his TimeZone
Process whatever request logic
In OnActionExecuting filter convert all response date time to appropriate TimeZone.
This approach seems to be very clumsy. I hope there is a better way to address such kind of issues. Please, suggest
Related
I have a problem where I need to handle changes in the timezones and changes in the days within the same timezone while saving and then retrieving the records in the database. The two solutions that I could think of yet are, use UTC+0 timezone on the server, OR, send timezone of client each time while saving the record and then use that timezone value while retrieving the record. The former one can handle changes in the timezones of the client but does not handle the problem where client wants to query all the records of a particular day because the change in day won't be at the same time for the server and the client. The latter solution handles the problem of difference in the day change time but does not handle change in the timezone of the same client. What other solution could be there which can handle both the problems?
When I send a DateTimeOffset value to an Azure Mobile App inside an object property it changes it to UTC, but I want to keep the TimeZone.
Both client and server JsonSerializerSettings are set to RoundtripKind by default but seems that this is not working.
How can I keep the TimeZonewhen working with DateTimeOffset properties?
When you send DateTimeOffset, you can pass the time zone information in TimeSpan.
You may go through this article for more information.
Also, you may try converting back to Time Zone from UTC again.
For reference here is the article which you can go through.
My server's machine timezone is in HST format. When I try to get the timezone by using HTTP request in JavaScript, it is giving me in UTC format. Is there any way to get the server's timezone.
A few things:
Never rely on the server's time zone to be set to anything in particular. It can easily be changed, or you may simply want to move your server somewhere else. Either should not affect your data.
The HTTP header will only give you the time in UTC/GMT. This part of the HTTP specification, in RFC7231 section 7.1.1.1 and 7.1.1.2.
The client knows nothing about the server's time zone, unless you specifically go out of your way to send it yourself. Due to the previous two points, this should not be required anyway, or should be used in very rare circumstances.
The server knows nothing about the client time zone either. If you want to display the value of the server's clock in the client's local time, you have two options:
Send the UTC time to the client, and use JavaScript to convert from UTC to Local time. The JavaScript Date object is sufficient for this, but you may also find libraries like Moment.js and others useful.
Determine the client's local time zone by some other means, either by asking the user, or by guessing. See this answer for more detail. Once you have the time zone ID (such as America/Los_Angeles, etc.) use this on the server-side. This approach is only useful if you have a lot of date/time manipulation to do on the server-side. If you are simply converting to local time for display, prefer option 1.
I am trying to do a service that is instantiated only one time. And then re-used any time I need it when a new user access my homepage.
What I am trying to do is a service once instantiated that set a datetime. When any user connect to my homepage, I send a datetime to my service and I compare the two datetimes (the one when the service had been instantiated and the one of the user), if hour >= 1 then do something.
My problem is that when I refresh my homepage the datetime of the service is the time of right now and not the time when I instantiated it for the first time. It seems that when I refresh it recreates a new service and that's not what I want it to do. I checked in prod and dev, it does not change anything. Is there any way I could do that or are the services not meant to work that way? If so, how could I do that?
Thank you guys for your help!
You can't achieve it in php directly. For php every call (page refresh) is a separate request, so every call creates a new instance of your class (defined as a service) - this new instance knows nothing about previous one with datetime set.
You can solve this problem in (at least) two ways:
1. Session
Create your service class and store current datetime in session. Next time your service is called (and instantiated) you will check previous request time. Downside of this solution is that session will time out and is specific to user and as far as I understood well you want to have this no matter which user calls your action. See solution number 2
2. Persistence
Save your datetime in some persistent layer - for example database or file. Then you can read in your service from this data source and tell what is time difference between current and previous request (and you don't worry about user or session timeout)
I thought this was the correct way to do this, but apparently it is not.
I write a date to an SQL Server database using the default of GetUtcDate().
I read it back in ASP.NET and display it to the user with theTime.ToLocalTime().
(It's already in UTC, so I need to convert it to localtime, NOT into UTC time again.)
Wouldn't that make the date/time display correctly any where in the world?
But it seems to be 1 hour off when:
The SQL Server is in 1 time zone (might be... or not be in a DST area or time of year).
The webserver running the ASP.NET is in a different timezone (it also may or may not be DST there).
The local user viewing the date/time is also in a totally different timezone (also with or without DST there).
(I'm not sure if it matters, but the webserver only has .net v1.1 and will NEVER be upgraded to v2 or anything else.)
Storing UTC values using GETUTCDATE is fine. Retrieving UTC values into a DateTime object on your web server is fine also. Calling ToLocalTime is not.
When you use ToLocalTime, you are using the local time zone of the machine where the code is running. In this case - your web server.
The web server has absolutely no knowledge of the timezone of your user. You have to manage that yourself in your application logic. Since you are stuck on .Net 1.1, you have no easy way to manage this server side. You'll have to take a client-side JavaScript approach:
Pass the UTC value to the client in a non-displaying manner. For example, an XHR call, or inline script.
Preferably, use the ISO-8601 format, such as 2014-06-10T09:30:00.000Z. You can get this from your DateTime object by using .ToString("o").
On the client, if you're using newer web browsers, you can pass this string directly into the constructor of a JavaScript Date object. However, you may prefer to use a more robust library for browser compatibility and enhanced formatting capabilities. I recommend moment.js.
By passing the UTC value to the browser, you've delegated the work of figuring out the user's time zone. Calling any of the output methods on the Date or moment objects will show the time converted to the user's time zone automatically.
There are plenty of server-side approaches, but they would require you update to a more reasonable version of the .NET Framework.
TimeZoneInfo and Noda Time work on .NET 3.5 and greater.
TZ4Net works on .NET 2.0
With any of the server-side mechanisms, you would need to change your application to:
Ask the user for their time zone in some settings part of your application.
Apply that time zone to the UTC date before delivering a string to the web browser.
Also, you should recognize that .NET 1.1 was released in 2003, and is way past its lifecycle end date. You should review the chart here and consider upgrading.