/// <summary>
        /// Returns a <see cref="DateTime"/> instance read from the <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The extended <see cref="Stream"/> instance.</param>
        /// <param name="format">The <see cref="DateTimeDataFormat"/> format in which the data is stored.</param>
        /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
        /// <returns>The value read from the current stream.</returns>
        public static DateTime ReadDateTime(this Stream stream,
                                            DateTimeDataFormat format = DateTimeDataFormat.NetTicks, ByteConverter converter = null)
        {
            switch (format)
            {
            case DateTimeDataFormat.NetTicks:
                return(new DateTime(ReadInt64(stream, converter)));

            case DateTimeDataFormat.CTime:
                return(_cTimeBase.AddSeconds(ReadUInt32(stream, converter)));

            case DateTimeDataFormat.CTime64:
                return(_cTimeBase.AddSeconds(ReadInt64(stream, converter)));

            default:
                throw new ArgumentException($"Invalid {nameof(DateTimeDataFormat)}.", nameof(format));
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes a <see cref="DateTime"/> value to the <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The extended <see cref="Stream"/> instance.</param>
        /// <param name="value">The value to write.</param>
        /// <param name="format">The <see cref="DateTimeDataFormat"/> format in which the data is stored.</param>
        /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
        public static void Write(this Stream stream, DateTime value,
                                 DateTimeDataFormat format = DateTimeDataFormat.NetTicks, ByteConverter converter = null)
        {
            converter = converter ?? ByteConverter.System;
            switch (format)
            {
            case DateTimeDataFormat.NetTicks:
                Write(stream, value.Ticks, converter);
                break;

            case DateTimeDataFormat.CTime:
                Write(stream, (uint)(new DateTime(1970, 1, 1) - value).TotalSeconds, converter);
                break;

            case DateTimeDataFormat.CTime64:
                Write(stream, (ulong)(new DateTime(1970, 1, 1) - value).TotalSeconds, converter);
                break;

            default:
                throw new ArgumentException($"Invalid {nameof(DateTimeDataFormat)}.", nameof(format));
            }
        }
        /// <summary>
        /// Returns an array of <see cref="DateTime"/> instances read from the <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The extended <see cref="Stream"/> instance.</param>
        /// <param name="count">The number of values to read.</param>
        /// <param name="format">The <see cref="DateTimeDataFormat"/> format in which the data is stored.</param>
        /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
        /// <returns>The array of values read from the current stream.</returns>
        public static DateTime[] ReadDateTimes(this Stream stream, int count,
                                               DateTimeDataFormat format = DateTimeDataFormat.NetTicks, ByteConverter converter = null)
        {
            var values = new DateTime[count];

            lock (stream)
            {
                switch (format)
                {
                case DateTimeDataFormat.NetTicks:
                    for (int i = 0; i < count; i++)
                    {
                        values[i] = new DateTime(ReadInt64(stream, converter));
                    }
                    break;

                case DateTimeDataFormat.CTime:
                    for (int i = 0; i < count; i++)
                    {
                        values[i] = _cTimeBase.AddSeconds(ReadUInt32(stream, converter));
                    }
                    break;

                case DateTimeDataFormat.CTime64:
                    for (int i = 0; i < count; i++)
                    {
                        values[i] = _cTimeBase.AddSeconds(ReadInt64(stream, converter));
                    }
                    break;

                default:
                    throw new ArgumentException($"Invalid {nameof(BooleanDataFormat)}.", nameof(format));
                }
            }
            return(values);
        }
Exemple #4
0
 /// <summary>
 /// Writes an enumeration of <see cref="DateTime"/> values to this stream. The <see cref="DateTime"/> values
 /// will be available in the specified binary format.
 /// </summary>
 /// <param name="values">The <see cref="DateTime"/> values to write.</param>
 /// <param name="format">The binary format in which the <see cref="DateTime"/> values will be written.</param>
 public void Write(IEnumerable <DateTime> values, DateTimeDataFormat format = DateTimeDataFormat.NetTicks)
 {
     BaseStream.Write(values, format, ByteConverter);
 }
Exemple #5
0
 /// <summary>
 /// Writes a <see cref="DateTime"/> value to this stream. The <see cref="DateTime"/> will be available in the
 /// specified binary format.
 /// </summary>
 /// <param name="value">The <see cref="DateTime"/> value to write.</param>
 /// <param name="format">The binary format in which the <see cref="DateTime"/> will be written.</param>
 public void Write(DateTime value, DateTimeDataFormat format = DateTimeDataFormat.NetTicks)
 {
     BaseStream.Write(value, format, ByteConverter);
 }
Exemple #6
0
 /// <summary>
 /// Reads the specified number of <see cref="T:System.DateTime" /> values from the current stream into a
 /// <see cref="T:System.DateTime" /> array. The <see cref="T:System.DateTime" /> values are available in the specified binary
 /// format.
 /// </summary>
 /// <param name="count">The number of <see cref="T:System.DateTime" /> values to read.</param>
 /// <param name="format">The binary format, in which the <see cref="T:System.DateTime" /> values will be read.</param>
 /// <returns>The <see cref="T:System.DateTime" /> array read from the current stream.</returns>
 public DateTime[] ReadDateTimes(int count, DateTimeDataFormat format = DateTimeDataFormat.NetTicks)
 {
     return(this.BaseStream.ReadDateTimes(count, format, this.ByteConverter));
 }
Exemple #7
0
 /// <summary>
 /// Reads a <see cref="T:System.DateTime" /> from the current stream. The <see cref="T:System.DateTime" /> is available in the
 /// specified binary format.
 /// </summary>
 /// <param name="format">The binary format, in which the <see cref="T:System.DateTime" /> will be read.</param>
 /// <returns>The <see cref="T:System.DateTime" /> read from the current stream.</returns>
 public DateTime ReadDateTime(DateTimeDataFormat format = DateTimeDataFormat.NetTicks)
 {
     return(this.BaseStream.ReadDateTime(format, this.ByteConverter));
 }
Exemple #8
0
 /// <summary>
 /// Writes an array of <see cref="DateTime"/> values to the <paramref name="stream"/>.
 /// </summary>
 /// <param name="stream">The extended <see cref="Stream"/> instance.</param>
 /// <param name="values">The values to write.</param>
 /// <param name="format">The <see cref="DateTimeDataFormat"/> format in which the data is stored.</param>
 /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
 public static void Write(this Stream stream, IEnumerable <DateTime> values,
                          DateTimeDataFormat format = DateTimeDataFormat.NetTicks, ByteConverter converter = null)
 {
     converter = converter ?? ByteConverter.System;
 }