The implementation code of the us eastern time and Beijing time conversion is analyzed

  • 2020-05-12 03:07:15
  • OfStack

Us eastern time is in the UTC-5 time zone and Beijing time is in the UTC + 8 time zone. Normally, us eastern time is 13 hours later than Beijing time. It is important to note that the United States has summer time, but Beijing does not have summer time, summer time in the United States will make the time 1 hour earlier, so that people can get up early and go to bed early, so the eastern time in the United States is 12 hours later than Beijing time.
The logic is as described above, and the code is as follows

/// <summary> 
/// AMESTime  Summary of : 
///  The conversion of eastern time  
///  
///  Time is in the east UTC-5 Time zone. The United States has daylight saving time, which is when daylight saving time is switched on and eastern time is earlier than its time zone 1 hours UTC-4  
///  When not in use, eastern time is greater than Beijing time ( UTC+8 Late time zone) 13 Hours, east time is later than Beijing time during daylight saving time 12 hours  
///  
///  
///  The U.S. congress 2005 Energy act, daylight saving time: from 2007 Year beginning year 3 On the first 2 Daylight saving time begins on a Sunday and ends on a Sunday 11 On the first 1 Sunday.  
/// </summary> 
public class AMESTime 
{ 
    private static DateTime _thisYearDaylightSavingTimeStart, 
        _thisYearDaylightSavingTimeEnd; 

    private const int TIMEZONE_OFFSET_DAY_SAVING_LIGHT = -12; 
    private const int TIMEZONE_OFFSET = -13; 

    public static DateTime BeijingTimeToAMESTime(DateTime beijingTime) 
    { 
        int offsetHours = (IsNowAMESDayLightSavingTime ? TIMEZONE_OFFSET_DAY_SAVING_LIGHT:TIMEZONE_OFFSET); 

        return beijingTime.AddHours(offsetHours); 
    } 

    public static DateTime AMESNow 
    { 
        get
        { 
            return BeijingTimeToAMESTime(DateTime.Now); 
        } 
    } 

    /// <summary> 
    ///  Determine if the current date is summer time  
    ///  from 2007 Year beginning year 3 On the first 2 Daylight saving time begins on a Sunday and ends on a Sunday 11 On the first 1 Sunday.  
    /// </summary> 
    /// <returns> Is that the return true , or for false</returns> 
    public static bool IsNowAMESDayLightSavingTime 
    { 
        get
        { 
            return DateTime.UtcNow > DayLightSavingStartTimeUtc 
                && DateTime.UtcNow < DayLightSavingEndTimeUtc; 
        } 
    } 

    /// <summary> 
    ///  Daylight saving time start time  
    /// </summary> 
    static DateTime DayLightSavingStartTimeUtc 
    { 
        get
        { 
            if(_thisYearDaylightSavingTimeStart.Year != DateTime.Now.Year) 
            { 
                DateTime temp = new DateTime(DateTime.Now.Year,3,8,0,0,0); 
                while(temp.DayOfWeek != DayOfWeek.Sunday) 
                { 
                    temp = temp.AddDays(1); 
                } 
                _thisYearDaylightSavingTimeStart = temp.AddHours(TIMEZONE_OFFSET); 
            } 

            return _thisYearDaylightSavingTimeStart; 
        } 
    } 

    /// <summary> 
    ///  End of daylight saving time  
    /// </summary> 
    static DateTime DayLightSavingEndTimeUtc 
    { 
        get
        { 
            if(_thisYearDaylightSavingTimeEnd.Year != DateTime.Now.Year) 
            { 
                DateTime temp = new DateTime(DateTime.Now.Year,11,1,0,0,0); 
                while(temp.DayOfWeek != DayOfWeek.Sunday) 
                { 
                    temp = temp.AddDays(1); 
                } 
                _thisYearDaylightSavingTimeEnd = temp.AddHours(TIMEZONE_OFFSET_DAY_SAVING_LIGHT); 
            } 
            return _thisYearDaylightSavingTimeEnd; 
        } 
    } 
}


Related articles: