Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

DateTimeHelper.cs

using System.Collections.ObjectModel;
using Nop.Core;
using Nop.Core.Domain.Customers;

namespace Nop.Services.Helpers;

/// 
/// Represents a datetime helper
/// 
public partial class DateTimeHelper : IDateTimeHelper
{
    #region Fields

    protected readonly DateTimeSettings _dateTimeSettings;
    protected readonly IWorkContext _workContext;

    #endregion

    #region Ctor

    public DateTimeHelper(DateTimeSettings dateTimeSettings,
        IWorkContext workContext)
    {
        _dateTimeSettings = dateTimeSettings;
        _workContext = workContext;
    }

    #endregion

    #region Utilities

    /// 
    /// Retrieves a System.TimeZoneInfo object from the registry based on its identifier.
    /// 
    /// The time zone identifier, which corresponds to the System.TimeZoneInfo.Id property.
    /// A System.TimeZoneInfo object whose identifier is the value of the id parameter.
    protected virtual TimeZoneInfo FindTimeZoneById(string id)
    {
        return TimeZoneInfo.FindSystemTimeZoneById(id);
    }

    #endregion

    #region Methods

    /// 
    /// Returns a sorted collection of all the time zones
    /// 
    /// A read-only collection of System.TimeZoneInfo objects.
    public virtual ReadOnlyCollection GetSystemTimeZones()
    {
        return TimeZoneInfo.GetSystemTimeZones();
    }

    /// 
    /// Converts the date and time to current user date and time
    /// 
    /// The date and time (represents local system time or UTC time) to convert.
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains a DateTime value that represents time that corresponds to the dateTime parameter in customer time zone.
    /// 
    public virtual async Task ConvertToUserTimeAsync(DateTime dt)
    {
        return await ConvertToUserTimeAsync(dt, dt.Kind);
    }

    /// 
    /// Converts the date and time to current user date and time
    /// 
    /// The date and time (represents local system time or UTC time) to convert.
    /// The source datetimekind
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains a DateTime value that represents time that corresponds to the dateTime parameter in customer time zone.
    /// 
    public virtual async Task ConvertToUserTimeAsync(DateTime dt, DateTimeKind sourceDateTimeKind)
    {
        dt = DateTime.SpecifyKind(dt, sourceDateTimeKind);
        if (sourceDateTimeKind == DateTimeKind.Local && TimeZoneInfo.Local.IsInvalidTime(dt))
            return dt;

        var currentUserTimeZoneInfo = await GetCurrentTimeZoneAsync();
        return TimeZoneInfo.ConvertTime(dt, currentUserTimeZoneInfo);
    }

    /// 
    /// Converts the date and time to current user date and time
    /// 
    /// The date and time to convert.
    /// The time zone of dateTime.
    /// The time zone to convert dateTime to.
    /// A DateTime value that represents time that corresponds to the dateTime parameter in customer time zone.
    public virtual DateTime ConvertToUserTime(DateTime dt, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
    {
        if (sourceTimeZone.IsInvalidTime(dt))
            return dt;

        return TimeZoneInfo.ConvertTime(dt, sourceTimeZone, destinationTimeZone);
    }

    /// 
    /// Converts the date and time to Coordinated Universal Time (UTC)
    /// 
    /// The date and time (represents local system time or UTC time) to convert.
    /// A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.
    public virtual DateTime ConvertToUtcTime(DateTime dt)
    {
        return ConvertToUtcTime(dt, dt.Kind);
    }

    /// 
    /// Converts the date and time to Coordinated Universal Time (UTC)
    /// 
    /// The date and time (represents local system time or UTC time) to convert.
    /// The source datetimekind
    /// A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.
    public virtual DateTime ConvertToUtcTime(DateTime dt, DateTimeKind sourceDateTimeKind)
    {
        dt = DateTime.SpecifyKind(dt, sourceDateTimeKind);
        if (sourceDateTimeKind == DateTimeKind.Local && TimeZoneInfo.Local.IsInvalidTime(dt))
            return dt;

        return TimeZoneInfo.ConvertTimeToUtc(dt);
    }

    /// 
    /// Converts the date and time to Coordinated Universal Time (UTC)
    /// 
    /// The date and time to convert.
    /// The time zone of dateTime.
    /// A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.
    public virtual DateTime ConvertToUtcTime(DateTime dt, TimeZoneInfo sourceTimeZone)
    {
        if (sourceTimeZone.IsInvalidTime(dt))
        {
            //could not convert
            return dt;
        }

        return TimeZoneInfo.ConvertTimeToUtc(dt, sourceTimeZone);
    }

    /// 
    /// Gets a customer time zone
    /// 
    /// Customer
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer time zone; if customer is null, then default store time zone
    /// 
    public virtual Task GetCustomerTimeZoneAsync(Customer customer)
    {
        if (!_dateTimeSettings.AllowCustomersToSetTimeZone)
            return Task.FromResult(DefaultStoreTimeZone);

        TimeZoneInfo timeZoneInfo = null;

        var timeZoneId = string.Empty;
        if (customer != null)
            timeZoneId = customer.TimeZoneId;

        try
        {
            if (!string.IsNullOrEmpty(timeZoneId))
                timeZoneInfo = FindTimeZoneById(timeZoneId);
        }
        catch
        {
            //ignore
        }

        return Task.FromResult(timeZoneInfo ?? DefaultStoreTimeZone);
    }

    /// 
    /// Gets the current user time zone
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the current user time zone
    /// 
    public virtual async Task GetCurrentTimeZoneAsync()
    {
        return await GetCustomerTimeZoneAsync(await _workContext.GetCurrentCustomerAsync());
    }

    /// 
    /// Gets or sets a default store time zone
    /// 
    public virtual TimeZoneInfo DefaultStoreTimeZone
    {
        get
        {
            TimeZoneInfo timeZoneInfo = null;
            try
            {
                if (!string.IsNullOrEmpty(_dateTimeSettings.DefaultStoreTimeZoneId))
                    timeZoneInfo = FindTimeZoneById(_dateTimeSettings.DefaultStoreTimeZoneId);
            }
            catch
            {
                //ignore
            }

            return timeZoneInfo ?? TimeZoneInfo.Local;
        }
    }

    #endregion
}