You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为ABP.IO的IHasCreationTime设置太平洋标准时间(EF与SQL Server)

问题描述

我的本地时区为+08:00,默认创建实体时数据库的CreationTime会存储为+8时区的时间。当我将AbpClockOptions设置为DateTimeKind.Utc后,数据库中存储的是+0时区的时间。请问执行InsertAsync操作时,如何让数据库中的CreationTime存储为太平洋标准时间(-08:00)?

当前配置

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

当前执行InsertAsync时,数据库的CreationTime为+00:00时间值,但期望存储为-08:00(太平洋标准时间)。

解决方案

步骤1:替换审计属性设置器服务

在DomainModule中添加以下代码,替换默认的IAuditPropertySetter实现:

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

步骤2:实现自定义审计属性设置器

创建CustomAuditPropertySetter类,继承自AuditPropertySetter,重写时间设置逻辑,将时间转换为太平洋标准时间:

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);
    }

    // 将当前UTC时间转换为太平洋标准时间
    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);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Jakeuj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:10:12