Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Create Entity with a Timezone #9174


User avatar
0
marble68 created

I have entites that have start and stop times.

Do I store the time zone as a string? timeZoneId?


9 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    You can store start and stop time as utc.

    https://aspnetboilerplate.com/Pages/Documents/Timing

  • User Avatar
    0
    marble68 created

    Understood - What I was asking is if I have an entity where I need to note what time zone it exists in, how do I set it as a property.

    I'm currently using a string, and the name of the time zone.

    That seems to be how ABP does it.

  • User Avatar
    0
    musa.demir created

    If you want to allow multiple timezones, common approach is using utc datetime. If you set Clock.Provider = ClockProviders.Utc; zero will handle the rest (backend and ui). Than you can convert stored utc formatted date to any timezone and use data in any timezone you want. https://docs.microsoft.com/tr-tr/dotnet/standard/datetime/converting-between-time-zones

  • User Avatar
    0
    marble68 created

    Am I doing this correctly in all my projects' startup.cs?

                app.UseAbp(options =>
                {
                    Clock.Provider = ClockProviders.Utc;
                    options.UseAbpRequestLocalization = false; //used below: UseAbpRequestLocalization
                });
    
    
  • User Avatar
    0
    marble68 created

    I have an enitity where the user specifies a time of day to notify employees in a work shift.

    They set the time, for example, 7:30 AM.

    The DateTime kind of the field I get on save is UTC. So I think I'm doing this correctly.

    I used the TimeZone dropdown to let them pick the time zone for shift.

    When the view model comes down, the time is in UTC.

    Would I do better to modify the view objects and put the time for the selected time zone on that, use that in the views, then, on save, convert that with moment to the UTC property when posting back?

    Or -

    Would I do better to modify the view model and convert it to the time zone for the entity, and then convert it back when I save?

  • User Avatar
    0
    ismcagdas created
    Support Team

    on save, convert that with moment to the UTC property when posting back?

    @marble68 this is done automatically when you use ClockProviders.Utc

  • User Avatar
    0
    marble68 created

    The challenge I have is I need to keep a time property, set to a user selectable timezone, that was a property of an entity.

    For example, the user is in CST/CDT. The Tenant HQ is in EST. The entity is in PST.

    They want to set a time for when that entity's deadline. 8:00AM PST, for example.

    If using Tenant, the date time would be off by time difference between PST and EST. If the user is in CST, that time would be represented.

    ANZ helpfully converts 8:00AM, if entered into a datetime field, to UTC, and stores that to the database. This is normally good. Except, when my user selects PST for the entity, then chooses 8:00AM for the deadline, ANZ uses settings to convert that datetime to the time zone for the tenant and or the user.

    I need the time to be based on the time zone the user selected for the entity.

    I am trying to solve this by using the entity's selected time zone, and setting the selected time to that selected time zone for the entity, then converting that to UTC.

    				TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(input.TimeZone);
    
    				DateTime _dtTZ = DateTime.SpecifyKind(input.DeadlineTZ, DateTimeKind.Unspecified); // Strip the UTC from the time supplied
    				DateTime _dt = TimeZoneInfo.ConvertTime(_dtTZ, timeZoneInfo, TimeZoneInfo.Utc); // Convert it as unspecified to a UTC
    
    				input.Deadline = _dt;
    

    In this scenario, if the user picks 8:00AM PST, what's posted to the server is 8:00 AM UTC. I set it's kind to be unspecified, then convert the new DateTime using the TimeZoneInfo the user selected. When this gets converted from 8:00 AM PST, I get 3PM UTC.

    Basically, I may have a tenant in one time zone, with a user in another, setting a time for entity based on the ENTITY'S timezone.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @marble68

    Thank you for the detailed explanation. In that case, I suggest you to handle this case manually by yourself. AspNet ZeroSs (and AspNet Boilerplate's) TimeZone functionality is implemented by tenants and users, so it will not act according to entities.

    You can use [DisableDateTimeNormalization] attribute to suppress AspNet Zero's datetime normalization. So, AspNet Zero will not modify datetime values when you use this attribute. You can use it on AppService class, method or DTO properties.

  • User Avatar
    0
    marble68 created

    I've resolved this. Thank you.