Пример #1
0
        public virtual void WriteValue(DateTimeOffset value)
        {
            WriteStarted(InternalState.Value);
            WriteUTFByte(Constants.Quote);
            {
                // yyyy-mm-ddThh:mm:ss.fffffff+00:00
                var time = value.DateTime;
                DateTimeHelpers.GetDateValues(time, out var year, out var month, out var day);

                var digits = Constants.TwoDigitsBytes[year / 100];
                var buffer = m_output.GetSpan(19);

                buffer[00] = digits.First;
                buffer[01] = digits.Second;

                digits = Constants.TwoDigitsBytes[year % 100];

                buffer[02] = digits.First;
                buffer[03] = digits.Second;
                buffer[04] = (Byte)'-';

                digits = Constants.TwoDigitsBytes[month];

                buffer[05] = digits.First;
                buffer[06] = digits.Second;
                buffer[07] = (Byte)'-';

                digits = Constants.TwoDigitsBytes[day];

                buffer[08] = digits.First;
                buffer[09] = digits.Second;
                buffer[10] = (Byte)'T';

                digits = Constants.TwoDigitsBytes[time.Hour];

                buffer[11] = digits.First;
                buffer[12] = digits.Second;
                buffer[13] = (Byte)':';

                digits = Constants.TwoDigitsBytes[time.Minute];

                buffer[14] = digits.First;
                buffer[15] = digits.Second;
                buffer[16] = (Byte)':';

                digits = Constants.TwoDigitsBytes[time.Second];

                buffer[17] = digits.First;
                buffer[18] = digits.Second;

                m_output.Advance(19);
                var remainingTicks = (UInt32)(time.Ticks % 10000000L);

                if (remainingTicks > 0)
                {
                    // Remove trailing zeroes
                    Byte precision = 7;

                    while (remainingTicks % 10 == 0)
                    {
                        remainingTicks /= 10;
                        --precision;
                    }

                    WriteUTFByte((Byte)'.');
                    WriteUInt32(remainingTicks, new StandardFormat('D', precision));
                }

                var offset = value.Offset;
                digits = Constants.TwoDigitsBytes[Math.Abs(offset.Hours)];
                buffer = m_output.GetSpan(6);

                buffer[0] = offset >= TimeSpan.Zero ? (Byte)'+' : (Byte)'-';
                buffer[1] = digits.First;
                buffer[2] = digits.Second;
                buffer[3] = (Byte)':';

                digits = Constants.TwoDigitsBytes[Math.Abs(offset.Minutes)];

                buffer[4] = digits.First;
                buffer[5] = digits.Second;

                m_output.Advance(6);
            }
            WriteUTFByte(Constants.Quote);
        }
Пример #2
0
        public virtual void WriteValue(DateTime value)
        {
            WriteStarted(InternalState.Value);
            WriteUTFByte(Constants.Quote);
            {
                // Unspecified: "yyyy-mm-ddThh:mm:ss.fffffff"
                // UTC:         "yyyy-mm-ddThh:mm:ss.fffffffZ"
                // Local:       "yyyy-mm-ddThh:mm:ss.fffffff+00:00"
                DateTimeHelpers.GetDateValues(value, out var year, out var month, out var day);

                var digits = Constants.TwoDigitsBytes[year / 100];
                {
                    var buffer = m_output.GetSpan(19);
                    buffer[00] = digits.First;
                    buffer[01] = digits.Second;

                    digits     = Constants.TwoDigitsBytes[year % 100];
                    buffer[02] = digits.First;
                    buffer[03] = digits.Second;
                    buffer[04] = (Byte)'-';

                    digits     = Constants.TwoDigitsBytes[month];
                    buffer[05] = digits.First;
                    buffer[06] = digits.Second;
                    buffer[07] = (Byte)'-';

                    digits     = Constants.TwoDigitsBytes[day];
                    buffer[08] = digits.First;
                    buffer[09] = digits.Second;
                    buffer[10] = (Byte)'T';

                    digits     = Constants.TwoDigitsBytes[value.Hour];
                    buffer[11] = digits.First;
                    buffer[12] = digits.Second;
                    buffer[13] = (Byte)':';

                    digits     = Constants.TwoDigitsBytes[value.Minute];
                    buffer[14] = digits.First;
                    buffer[15] = digits.Second;
                    buffer[16] = (Byte)':';

                    digits = Constants.TwoDigitsBytes[value.Second];

                    buffer[17] = digits.First;
                    buffer[18] = digits.Second;

                    m_output.Advance(19);
                }

                var remainingTicks = (UInt32)(value.Ticks % 10000000L);

                if (remainingTicks > 0)
                {
                    // Remove trailing zeroes
                    Byte precision = 7;

                    while (remainingTicks % 10 == 0)
                    {
                        remainingTicks /= 10;
                        --precision;
                    }

                    WriteUTFByte((Byte)'.');
                    WriteUInt32(remainingTicks, new StandardFormat('D', precision));
                }

                if (value.Kind == DateTimeKind.Utc)
                {
                    WriteUTFByte((Byte)'Z');
                }
                else if (value.Kind == DateTimeKind.Local)
                {
                    // We need to write the time offset as +HH:mm
                    var offset = TimeZoneInfo.Local.GetUtcOffset(value);
                    var buffer = m_output.GetSpan(6);

                    digits    = Constants.TwoDigitsBytes[Math.Abs(offset.Hours)];
                    buffer[0] = offset >= TimeSpan.Zero ? (Byte)'+' : (Byte)'-';
                    buffer[1] = digits.First;
                    buffer[2] = digits.Second;
                    buffer[3] = (Byte)':';

                    digits    = Constants.TwoDigitsBytes[Math.Abs(offset.Minutes)];
                    buffer[4] = digits.First;
                    buffer[5] = digits.Second;

                    m_output.Advance(6);
                }
            }
            WriteUTFByte(Constants.Quote);
        }