Esempio n. 1
0
 private static string GetCultureValue(LanguageCultureName culture)
 {
     return(LanguageCultureNames[Convert.ToInt32(culture.ToString("D"))]);
 }
Esempio n. 2
0
        /// <summary>
        /// ใช้สำหรับแสดงวันเวลาปัจจุบัน (UTC) ให้อยู่ในรูปแบบตามที่ระบุ โดยใช้ Time Zone ไทย (GMT +07:00 - SE Asia Standard Time)
        /// </summary>
        /// <param name="format"></param>
        /// <param name="culture"></param>
        /// <param name="isShowOffset"></param>
        /// <returns></returns>
        public static string GetCurrentUtcDateTimeInThaiTimeZone(DateTimeFormat format, LanguageCultureName culture, DateTimeUtcOffset utcOffset)
        {
            var formatValue   = GetDateTimeFormatValue(format);
            var cultureValue  = GetCultureValue(culture);
            var currentUtc    = DateTime.UtcNow;
            var timezone      = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time"); //https://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx
            var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtc, timezone);
            var offset        = timezone.GetUtcOffset(currentUtc);

            if (utcOffset != DateTimeUtcOffset.Null)
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)) + GetDateTimeUtcOffsetValue(offset, utcOffset));
            }
            else
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// ใช้สำหรับแปลงวันเวลาที่ระบุ ให้เป็นวันเวลา (UTC) อยู่ในรูปแบบตามที่ระบุ โดยใช้ Time Zone ตามที่ระบุ
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="timeZoneId"></param>
        /// <param name="format"></param>
        /// <param name="culture"></param>
        /// <param name="utcOffset"></param>
        /// <returns></returns>
        public static string ConvertToUtcDateTimeByTimeZoneId(DateTime datetime, TimeZoneDisplayName timezoneId, DateTimeFormat format, LanguageCultureName culture, DateTimeUtcOffset utcOffset)
        {
            var timezoneValue = GetTimeZoneValue(timezoneId);
            var formatValue   = GetDateTimeFormatValue(format);
            var cultureValue  = GetCultureValue(culture);
            var datetimeUtc   = datetime.ToUniversalTime();
            var timezone      = TimeZoneInfo.FindSystemTimeZoneById(timezoneValue); //https://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx
            var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(datetimeUtc, timezone);
            var offset        = timezone.GetUtcOffset(datetimeUtc);

            if (utcOffset != DateTimeUtcOffset.Null)
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)) + GetDateTimeUtcOffsetValue(offset, utcOffset));
            }
            else
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// ใช้สำหรับแปลงวันเวลาที่ระบุ ให้เป็นวันเวลา (UTC) อยู่ในรูปแบบตามที่ระบุ โดยใช้ Time Zone ไทย (GMT +07:00 - SE Asia Standard Time)
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="format"></param>
        /// <param name="culture"></param>
        /// <param name="isShowOffset"></param>
        /// <returns></returns>
        public static string ConvertToUtcDateTimeInThaiTimeZone(DateTime datetime, DateTimeFormat format, LanguageCultureName culture, DateTimeUtcOffset utcOffset)
        {
            var formatValue  = GetDateTimeFormatValue(format);
            var cultureValue = GetCultureValue(culture);
            var datetimeUtc  = datetime.ToUniversalTime();
            var timezone     = TimeZoneInfo.Local;

            try
            {
                timezone = TimeZoneInfo.FindSystemTimeZoneById("Asia/Bangkok"); //https://docs.microsoft.com/en-us/rest/api/maps/timezone/gettimezoneenumwindows
            }
            catch
            {
                try
                {
                    timezone = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time"); //https://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx
                }
                catch
                {
                    timezone = TimeZoneInfo.Local;
                }
            }
            var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(datetimeUtc, timezone);
            var offset        = timezone.GetUtcOffset(datetimeUtc);

            if (utcOffset != DateTimeUtcOffset.Null)
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)) + GetDateTimeUtcOffsetValue(offset, utcOffset));
            }
            else
            {
                return(convertedTime.ToString(formatValue, CultureInfo.CreateSpecificCulture(cultureValue)));
            }
        }