Exemplo n.º 1
0
 public DateTimeOffsetConverter(string format, DateTimeKind kind, byte filler, Type type)
 {
     length       = format.Length;
     entries      = DateTimeByteHelper.ParseDateTimeFormat(format, out hasDatePart);
     this.kind    = kind;
     this.filler  = filler;
     defaultValue = type.GetDefaultValue();
 }
Exemplo n.º 2
0
 public void Write(byte[] buffer, int index, object value)
 {
     if (value is null)
     {
         BytesHelper.Fill(buffer, index, length, filler);
     }
     else
     {
         DateTimeByteHelper.FormatDateTime(buffer, index, hasDatePart, entries, ((DateTimeOffset)value).UtcDateTime);
     }
 }
Exemplo n.º 3
0
        public object Read(byte[] buffer, int index)
        {
            if (DateTimeByteHelper.TryParseDateTime(buffer, index, entries, kind, out var result))
            {
                if (kind == DateTimeKind.Unspecified)
                {
                    return(new DateTimeOffset(result, TimeSpan.Zero));
                }

                if (kind == DateTimeKind.Utc)
                {
                    return(new DateTimeOffset(result));
                }

                var offset  = TimeZoneInfo.Local.GetUtcOffset(result);
                var utcTick = result.Ticks - offset.Ticks;
                if (DateTimeHelper.IsValidTicks(utcTick))
                {
                    return(new DateTimeOffset(result, offset));
                }
            }

            return(defaultValue);
        }
Exemplo n.º 4
0
 public object Read(byte[] buffer, int index)
 {
     return(DateTimeByteHelper.TryParseDateTime(buffer, index, entries, kind, out var result)
         ? result
         : defaultValue);
 }