Example #1
0
        public static DateTime Now(string timeZone)
        {
            var timeZoneInfo = TimeZoneKeeper.GetTimeZone(timeZone);
            var result       = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timeZoneInfo);

            return(result);
        }
Example #2
0
        public static DateTime ConvertFromOffset(string dateTime, string destinationTimeZone)
        {
            if (!DateTimeOffset.TryParse(dateTime, out var parsed))
            {
                throw new ArgumentException($"Not a valid offset value: {dateTime}", nameof(dateTime));
            }

            var result = TimeZoneInfo.ConvertTime(parsed, TimeZoneKeeper.GetTimeZone(destinationTimeZone));

            return(result.DateTime);
        }
Example #3
0
        private static string ToClientFormat(DateTime dateTime, string format, string timeZone)
        {
            if (dateTime == DateTime.MinValue)
            {
                return(string.Empty);
            }

            var invariant = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,
                                         dateTime.Hour, dateTime.Minute, dateTime.Second,
                                         DateTimeKind.Utc);

            var zone       = TimeZoneKeeper.GetTimeZone(timeZone);
            var offsetSpan = zone.GetUtcOffset(invariant);

            var offset = new DateTimeOffset(invariant.Ticks, offsetSpan);

            var result = offset.ToString(format);

            return(result);
        }