public static string GetUtcOffsetText(DateTime d)
        {
            TimeSpan utcOffset = d.GetUtcOffset();

            return utcOffset.Hours.ToString("+00;-00", CultureInfo.InvariantCulture) + ":" + utcOffset.Minutes.ToString("00;00", CultureInfo.InvariantCulture);
        }
        public void SerializeShouldChangeNonUTCDates()
        {
            DateTime localDateTime = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);

            DateTimeTestClass c = new DateTimeTestClass();
            c.DateTimeField = localDateTime;
            c.PreField = "Pre";
            c.PostField = "Post";
            string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); //note that this fails without the Utc converter...
            c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
            string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });

            TimeSpan offset = localDateTime.GetUtcOffset();

            // if the current timezone is utc then local already equals utc
            if (offset == TimeSpan.Zero)
                Assert.AreEqual(json, json2);
            else
                Assert.AreNotEqual(json, json2);
        }
Example #3
0
        internal static int WriteDateTimeString(char[] chars, int start, DateTime value, TimeSpan? offset, DateTimeKind kind, DateFormatHandling format)
        {
            int pos = start;

            if (format == DateFormatHandling.MicrosoftDateFormat)
            {
                TimeSpan o = offset ?? value.GetUtcOffset();

                long javaScriptTicks = ConvertDateTimeToJavaScriptTicks(value, o);

                @"\/Date(".CopyTo(0, chars, pos, 7);
                pos += 7;

                string ticksText = javaScriptTicks.ToString(CultureInfo.InvariantCulture);
                ticksText.CopyTo(0, chars, pos, ticksText.Length);
                pos += ticksText.Length;

                switch (kind)
                {
                    case DateTimeKind.Unspecified:
                        if (value != DateTime.MaxValue && value != DateTime.MinValue)
                        {
                            pos = WriteDateTimeOffset(chars, pos, o, format);
                        }
                        break;
                    case DateTimeKind.Local:
                        pos = WriteDateTimeOffset(chars, pos, o, format);
                        break;
                }

                @")\/".CopyTo(0, chars, pos, 3);
                pos += 3;
            }
            else
            {
                pos = WriteDefaultIsoDate(chars, pos, value);

                switch (kind)
                {
                    case DateTimeKind.Local:
                        pos = WriteDateTimeOffset(chars, pos, offset ?? value.GetUtcOffset(), format);
                        break;
                    case DateTimeKind.Utc:
                        chars[pos++] = 'Z';
                        break;
                }
            }

            return pos;
        }
Example #4
0
        private static long ToUniversalTicks(DateTime dateTime)
        {
            if (dateTime.Kind == DateTimeKind.Utc)
            {
                return dateTime.Ticks;
            }

            return ToUniversalTicks(dateTime, dateTime.GetUtcOffset());
        }
 public static int WriteDateTimeString(char[] chars, int start, DateTime value, TimeSpan? offset, DateTimeKind kind, DateFormatHandling format)
 {
     int num2;
     if (format == DateFormatHandling.MicrosoftDateFormat)
     {
         TimeSpan offset2 = offset ?? value.GetUtcOffset();
         long num = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(value, offset2);
         "\\/Date(".CopyTo(0, chars, start, 7);
         num2 = start + 7;
         string text = num.ToString(CultureInfo.InvariantCulture);
         text.CopyTo(0, chars, num2, text.Length);
         num2 += text.Length;
         switch (kind)
         {
             case DateTimeKind.Unspecified:
                 if (value != DateTime.MaxValue && value != DateTime.MinValue)
                 {
                     num2 = DateTimeUtils.WriteDateTimeOffset(chars, num2, offset2, format);
                 }
                 break;
             case DateTimeKind.Local:
                 num2 = DateTimeUtils.WriteDateTimeOffset(chars, num2, offset2, format);
                 break;
         }
         ")\\/".CopyTo(0, chars, num2, 3);
         num2 += 3;
     }
     else
     {
         num2 = DateTimeUtils.WriteDefaultIsoDate(chars, start, value);
         switch (kind)
         {
             case DateTimeKind.Utc:
                 chars[num2++] = 'Z';
                 break;
             case DateTimeKind.Local:
                 num2 = DateTimeUtils.WriteDateTimeOffset(chars, num2, offset ?? value.GetUtcOffset(), format);
                 break;
         }
     }
     return num2;
 }
 public static long ToUniversalTicks(DateTime dateTime)
 {
     if (dateTime.Kind == DateTimeKind.Utc)
     {
         return dateTime.Ticks;
     }
     return DateTimeUtils.ToUniversalTicks(dateTime, dateTime.GetUtcOffset());
 }