Exemple #1
0
        /// <summary>
        /// Convert a <see cref="uint"/> back to a <see cref="DateTime"/> value. Zero input is returned as a NULL value.
        /// </summary>
        /// <param name="value">The value from the <see cref="ToUInt(DateTime, DateTime?)"/> conversion.</param>
        /// <param name="offset">The base starting point for the conversion. If NULL, the <see cref="CURRENT_EPOCH"/> constant is used.</param>
        /// <returns>The original DateTime value</returns>
        public static DateTime?FromUInt(this uint value, DateTime?offset = null)
        {
            if (value == 0)
            {
                return(null);
            }
            if (offset == null)
            {
                offset = CURRENT_EPOCH;
            }

            byte[] vs = BitConverter.GetBytes(value);
            Array.Reverse(vs);
            byte[] dArr = new byte[2];
            byte[] mArr = new byte[2];

            Array.Copy(vs, dArr, 2);
            Array.Reverse(dArr);
            Array.Copy(vs, 2, mArr, 0, 2);
            TwoNybbles nybbles = new TwoNybbles(mArr[1]);

            mArr[1] = nybbles.High;

            ushort day = BitConverter.ToUInt16(dArr, 0);
            ushort min = BitConverter.ToUInt16(mArr, 0);

            return(offset.Value.AddDays(day).AddMinutes(min).AddSeconds(nybbles.Low * 4));
        }
Exemple #2
0
        public virtual void SetSchedule(SchedulePage schedulePage, Schedule schedule)
        {
            var commandData = schedule.ToBytes();

            commandData[0] = new TwoNybbles((byte)schedulePage, commandData[0]).Value;
            Set(SicpCommands.SchedulingSet, commandData);
        }
Exemple #3
0
        /// <summary>
        /// Convert a <see cref="DateTime?"/> value to a <see cref="uint"/> (null values become zero)
        /// </summary>
        /// <param name="dt">The DateTime value to convert</param>
        /// <param name="offset">The base starting point for the conversion. If NULL, the <see cref="CURRENT_EPOCH"/> constant is used.</param>
        /// <returns>The input represented by a uint</returns>
        public static uint ToUInt(this DateTime dt, DateTime?offset = null)
        {
            if (offset == null)
            {
                offset = CURRENT_EPOCH;
            }
            TimeSpan ts   = dt - offset.Value;
            ushort   days = (ushort)ts.TotalDays;
            ushort   min  = (ushort)(ts.Hours * 60 + ts.Minutes);
            byte     sec  = (byte)(ts.Seconds / 4);

            byte[] vs   = new byte[4];
            byte[] dArr = BitConverter.GetBytes(days);
            Array.Reverse(dArr);
            byte[] mArr = BitConverter.GetBytes(min);
            Array.Copy(dArr, vs, 2);
            TwoNybbles nybbles = new TwoNybbles(mArr[1], sec);

            mArr[1] = nybbles.Full;
            Array.Copy(mArr, 0, vs, 2, 2);
            Array.Reverse(vs);
            return(BitConverter.ToUInt32(vs, 0));
        }