1

On my server side i am taking user current time according to user time i want to send a push notification per day what i have done the scheduler task which execute the push notification per day and send a notification per day as per server time and it works. i want to send the push notification every morning 6:00 AM as per user date-time not server time. so how to handle this ?:( situation is their is multiple user having multiple date-time zone so how to send the notification as per their time zone for each day morning 6 AM.

is their any function like

 var date =   (userDateTime to 6 : AM);

which convert given time to 6 AM date remain same

thanks in advance. :)

  • So a Webpage based Calendar App with Notifications? Do you have a client programm running on the users side? – Christopher Dec 17 '19 at 05:53
  • the notification part i have done using third party dll it send the push notification the task assigned to user per day as per server time zone –  Dec 17 '19 at 05:55
  • My point is this: If you got a application running on the users side, you could extract his current timezone and send it back to the server. That would at least deal with 1 issue. – Christopher Dec 17 '19 at 05:57
  • do you have any code sample how to extract that time –  Dec 17 '19 at 05:58

1 Answers1

2

You are dealing with Time and Timezones. That way lies madness.

My ideal advice is to scrap this idea. This is a Calendar App. Rather then push notification, use a Distributed Database Design (they had a massive surge in use cases since Mobile Apps have become a thing) and have the client App decide when to raise alarm based on the Client Computers time and timezone.

The .NET DateTime code is exceptionally good, in that it will extract the users Timezone and Culture settings during input and output from the OS. But as you realized, this will not help you with Server Applications - you only get the Servers Timezone automagically. The best you can usually do on Server side is store a "preferred timezone" or something like that. And then adapt it during processing.

I have 3 rules for dealing with Time and Timezones:

  1. Always store, transmit and retrieve the date in UTC. You do not want to get timezones causing issues here.
  2. Never store, transmit or retrieve them as strings.
  3. If you got to transmit them as strings, pick a fixed Culture Format and Encoding at all endpoints. You do not want to add that to your issues. XML and JSON take care of both parts for you.

A important thing to remember when changing a DateTime, is that it is a build-in struct. And those are usually immutable and DateTime is not a exception. You can not change a instance - only create a new one with a different values. So you need to store the results of any function calls in a DateTime variable.

This is what DateTimes Documentation says for your case:

The DateTime structure itself offers limited support for converting from one time zone to another. You can use the ToLocalTime method to convert UTC to local time, or you can use the ToUniversalTime method to convert from local time to UTC. However, a full set of time zone conversion methods is available in the TimeZoneInfo class. You convert the time in any one of the world's time zones to the time in any other time zone using these methods.

If you can move it to the user side, ToLocal() is the droid you are looking for.

If you keep it on the server side, TimeZoneInfo.ConvertTime() and it's overloads are what you need. Ideally you would want to have a collection of TimeZoneInfo Rulesets that you can let the user select. But I have no idea how to provide one.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • thanks for your valuable reply .simply i want to convert the user time to 6 am as per his time zone then convert to UTC so how to do this :( –  Dec 17 '19 at 06:18
  • @ketan You create a TimeZoneInfo instance with the proper values. Then feed it and the original date to this: https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime | At least that is what I can tell. As the support is so good, I always avoid doing stuff beyond local. – Christopher Dec 17 '19 at 06:20
  • @ketan The class TimeZoneInfo does two things: 1) Store all Data about a Time Zone in it's instance properties. 2) Provide the Static Functions to convert DateTime related types between Timezones. As it is not a inmutable struct, it is just: new. Set Values. Use. | Also I did find a way to list all avalible TimeZones: `TimeZoneInfo.GetSystemTimeZones()`. – Christopher Dec 17 '19 at 06:36
  • still not getting user send me his timezone in long format i am converting to by using common function. `public static DateTime LongToUTC(long UTCDate) { DateTime date; DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); date = start.AddMilliseconds(UTCDate).ToLocalTime(); return date; }` now my problem is how to convert this user date to 6 AM ? :( :( –  Dec 17 '19 at 06:57
  • Thanks a lot it works... i am having another issue like if trying trying to access the service using his time zone server replies with "The time zone id was not found on the local computer " so how to solve this :( ? how to make my server having all timezone info ? –  Dec 18 '19 at 05:08
  • @ketan Short of doing it yourself or using a 3rd party tool, you can not. Maybe you could store all the specific settings for a timezone in a Database? Timezone Info is stored in and retrieve from the Windows Registery. Different installations got different values | For 3rd party stuff, there is a thing called the Noda Project mentioned in this answer: https://stackoverflow.com/a/59370073/3346583 – Christopher Dec 18 '19 at 08:56
  • user mobile sends me the time zone in abbreviation like IST for "Indian standerd time". if i am trying to pass the IST to `TimeZoneInfo` it gives this error "The time zone id was not found on the local computer " so do you have any idea how to convert abbreviation to long form :( –  Dec 18 '19 at 09:16
  • @ketan Tell the Client Application/have it retreive wich Timezones are avalible, based on what is installed on the server. In a Client/Server situtation, it is always the **server** that has the final say what a valid value and the real state of the process is. – Christopher Dec 18 '19 at 09:34