Home > Mobile >  How to set ABP.IO IHasCreationTime for TimeZone 'Pacific Standard Time' (EF and SQL Server
How to set ABP.IO IHasCreationTime for TimeZone 'Pacific Standard Time' (EF and SQL Server

Time:01-09

My local is 08:00, create an entity's CreationTime will be 8 in the database.

When I set AbpClockOptions to DateTimeKind.Utc, I will get 0 in the database.

What can I do for entity InsertAsync to get -8 in the database?

Configure<AbpClockOptions>(options =>
{
  options.Kind = DateTimeKind.Utc;
});

When I do an InsertAsync, the database's CreationTime will be 00:00 datetime value.

But I expecting -08:00 (Pacific Standard Time).

Ans1:

DomainModule

context.Services.Replace(ServiceDescriptor.Transient
    <IAuditPropertySetter, CustomAuditPropertySetter>());

CustomAuditPropertySetter

using System;
using Volo.Abp;
using Volo.Abp.Auditing;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing;
using Volo.Abp.Users;

namespace Volo.Abp.Auditing;

public class CustomAuditPropertySetter : AuditPropertySetter
{
    private const string TimeZone = "Pacific Standard Time";
    private readonly TimeZoneInfo _timeZoneInfo;

    public CustomAuditPropertySetter(
        ICurrentUser currentUser,
        ICurrentTenant currentTenant,
        IClock clock,
        ITimezoneProvider timezoneProvider
    ) : base(currentUser, currentTenant, clock)
    {
        _timeZoneInfo = timezoneProvider.GetTimeZoneInfo(TimeZone);
    }

    private DateTime TimeZoneConverter()
    {
        return TimeZoneInfo.ConvertTime(
            Clock.Now.ToUniversalTime(), _timeZoneInfo);
    }
    private DateTime? NullableTimeZoneConverter()
    {
        return TimeZoneConverter();
    }

    protected override void SetCreationTime(object targetObject)
    {
        if (!(targetObject is IHasCreationTime objectWithCreationTime))
        {
            return;
        }

        if (objectWithCreationTime.CreationTime == default)
        {
            ObjectHelper.TrySetProperty(objectWithCreationTime, x =>
                x.CreationTime, TimeZoneConverter);
        }
    }

    protected override void SetLastModificationTime(object targetObject)
    {
        if (targetObject is IHasModificationTime objectWithModificationTime)
        {
            ObjectHelper.TrySetProperty(objectWithModificationTime, x =>
                x.LastModificationTime, NullableTimeZoneConverter);
        }
    }

    protected override void SetDeletionTime(object targetObject)
    {
        if (targetObject is IHasDeletionTime objectWithDeletionTime)
        {
            if (objectWithDeletionTime.DeletionTime == null)
            {
                ObjectHelper.TrySetProperty(objectWithDeletionTime, x =>
                    x.DeletionTime, NullableTimeZoneConverter);
            }
        }
    }
}

CodePudding user response:

you can implement your own custom version of the 'IAuditPropertySetter' interface that sets that property

protected virtual void SetCreationTime(object targetObject)
{
    if (!(targetObject is IHasCreationTime objectWithCreationTime))
    {
        return;
    }

    if (objectWithCreationTime.CreationTime == default)
    {
        ObjectHelper.TrySetProperty(objectWithCreationTime, x => x.CreationTime, () => Clock.Now); // Replace Clock.Now by your dateTime with -8:00 utc
    }
}

the code is in

https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs

and you replace the default implementation with your custom version, you will also have to implement the other properties, just as it is in the original implemented code

context.Services.Replace(ServiceDescriptor.Transient<IAuditPropertySetter, CustomAuditPropertySetter>());
  • Related