Beispiel #1
0
        private object BytesToObject()
        {
            Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "DateTimeMarshaler.BytesToObject()");

            int          days              = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
            int          hour              = bytes[3];
            int          minute            = bytes[4] >> 2;
            int          second            = ((bytes[4] & 0x03) << 4) | (bytes[5] >> 4);
            long         fraction          = ((bytes[5] & 0x0f) << 16) | (bytes[6] << 8) | bytes[7];
            DateTimeType type              = (DateTimeType)(bytes[8] >> 5);
            int          tz_offset_minutes = ((bytes[8] & 0x03) << 8) | bytes[9];

            if ((bytes[8] & 0x04) != 0)
            {
                tz_offset_minutes |= (-1 & ~0x03ff);
            }

            Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "type: " + type);

            if (type == DateTimeType.DT_TYPE_TIME)
            {
                VirtuosoTimeSpan ts = new VirtuosoTimeSpan(0, hour, minute, second, fraction);
                Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "TimeSpan: " + ts);
                return(ts);
            }
#if ADONET3
            else if (type == DateTimeType.DT_TYPE_DATETIME)
            {
                int year, month, day_of_month;
                GetDate(days, out year, out month, out day_of_month);
                TimeSpan tz_offset = new TimeSpan(0, tz_offset_minutes, 0);

                VirtuosoDateTimeOffset dt = new VirtuosoDateTimeOffset(year, month, day_of_month, hour, minute, second, fraction, tz_offset);
                dt = dt.AddMinutes(tz_offset_minutes);
                Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
                return(dt);
            }
            else if (type == DateTimeType.DT_TYPE_DATE)
            {
                int year, month, day_of_month;
                GetDate(days, out year, out month, out day_of_month);

                VirtuosoDateTime dt = new VirtuosoDateTime(year, month, day_of_month, hour, minute, second, fraction);
                Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
                return(dt);
            }
#else
            else if (type == DateTimeType.DT_TYPE_DATETIME || type == DateTimeType.DT_TYPE_DATE)
            {
                int year, month, day_of_month;
                GetDate(days, out year, out month, out day_of_month);

                VirtuosoDateTime dt = new VirtuosoDateTime(year, month, day_of_month, hour, minute, second, fraction);
                dt = dt.AddMinutes(tz_offset_minutes);
                Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
                return(dt);
            }
#endif
            else
            {
                throw new InvalidCastException();
            }
        }
		private object BytesToObject ()
		{
			Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "DateTimeMarshaler.BytesToObject()");

			int days = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
			int hour = bytes[3];
			int minute = bytes[4] >> 2;
			int second = ((bytes[4] & 0x03) << 4) | (bytes[5] >> 4);
			long fraction = ((bytes[5] & 0x0f) << 16) | (bytes[6] << 8) | bytes[7];
			DateTimeType type = (DateTimeType) (bytes[8] >> 5);
			int tz_offset_minutes = ((bytes[8] & 0x03) << 8) | bytes[9];
			if ((bytes[8] & 0x04) != 0)
				tz_offset_minutes |= (-1 & ~0x03ff);

			Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "type: " + type);

			if (type == DateTimeType.DT_TYPE_TIME)
			{
				VirtuosoTimeSpan ts = new VirtuosoTimeSpan (0, hour, minute, second, fraction);
				Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "TimeSpan: " + ts);
				return ts;
			}
#if ADONET3
			else if (type == DateTimeType.DT_TYPE_DATETIME)
                        {
				int year, month, day_of_month;
				GetDate (days, out year, out month, out day_of_month);
				TimeSpan tz_offset = new TimeSpan (0, tz_offset_minutes, 0);

				VirtuosoDateTimeOffset dt = new VirtuosoDateTimeOffset (year, month, day_of_month, hour, minute, second, fraction, tz_offset);
				dt = dt.AddMinutes(tz_offset_minutes);
				Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
				return dt;
			}
                        else if (type == DateTimeType.DT_TYPE_DATE)
			{
				int year, month, day_of_month;
				GetDate (days, out year, out month, out day_of_month);

				VirtuosoDateTime dt = new VirtuosoDateTime (year, month, day_of_month, hour, minute, second, fraction);
				Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
				return dt;
			}
#else
                        else if (type == DateTimeType.DT_TYPE_DATETIME || type == DateTimeType.DT_TYPE_DATE)
			{
				int year, month, day_of_month;
				GetDate (days, out year, out month, out day_of_month);

				VirtuosoDateTime dt = new VirtuosoDateTime (year, month, day_of_month, hour, minute, second, fraction);
				dt = dt.AddMinutes(tz_offset_minutes);
				Debug.WriteLineIf (Marshaler.marshalSwitch.Enabled, "DateTime: " + dt);
				return dt;
			}
#endif
			else
				throw new InvalidCastException ();
		}
Beispiel #3
0
        private void ObjectToBytes(Object value, DateTimeType type)
        {
            Debug.WriteLineIf(Marshaler.marshalSwitch.Enabled, "DateTimeMarshaler.ObjectToBytes(" + value + ", " + type + ")");

            int days, hour, minute, second, fraction, tz_offset_minutes;

            if (value is DateTime)
            {
                DateTime dt = (DateTime)value;

                TimeZone tz        = TimeZone.CurrentTimeZone;
                TimeSpan tz_offset = tz.GetUtcOffset(dt);
                tz_offset_minutes = (int)(tz_offset.Hours * 60) + tz_offset.Minutes;

                dt -= tz_offset;

                int year         = dt.Year;
                int month        = dt.Month;
                int day_of_month = dt.Day;
                days = GetDays(year, month, day_of_month);

                if (type == DateTimeType.DT_TYPE_DATETIME)
                {
                    hour     = dt.Hour;
                    minute   = dt.Minute;
                    second   = dt.Second;
                    fraction = (int)(dt.Ticks % TimeSpan.TicksPerSecond / 10L);                    // dt.Millisecond * Values.MicrosPerMilliSec;
                }
                else if (type == DateTimeType.DT_TYPE_DATE)
                {
                    hour = minute = second = fraction = 0;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
            else if (value is TimeSpan)
            {
                if (type != DateTimeType.DT_TYPE_TIME)
                {
                    throw new InvalidCastException();
                }

                days = Values.DAY_ZERO;
                tz_offset_minutes = 0;

                TimeSpan ts = (TimeSpan)value;
                hour     = ts.Hours;
                minute   = ts.Minutes;
                second   = ts.Seconds;
                fraction = (int)(ts.Ticks % TimeSpan.TicksPerSecond / 10L);                 //ts.Milliseconds * Values.MicrosPerMilliSec;
            }
            else if (value is VirtuosoDateTime)
            {
                VirtuosoDateTime dt = (VirtuosoDateTime)value;

                TimeZone tz        = TimeZone.CurrentTimeZone;
                TimeSpan tz_offset = tz.GetUtcOffset(dt.Value);
                tz_offset_minutes = (int)(tz_offset.Hours * 60) + tz_offset.Minutes;

                long ticks = dt.Ticks - tz_offset.Ticks;
                dt = new VirtuosoDateTime(ticks);

                int year         = dt.Year;
                int month        = dt.Month;
                int day_of_month = dt.Day;
                days = GetDays(year, month, day_of_month);

                if (type == DateTimeType.DT_TYPE_DATETIME)
                {
                    hour     = dt.Hour;
                    minute   = dt.Minute;
                    second   = dt.Second;
                    fraction = (int)dt.Microsecond;
                }
                else if (type == DateTimeType.DT_TYPE_DATE)
                {
                    hour = minute = second = fraction = 0;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
#if ADONET3
            else if (value is VirtuosoDateTimeOffset)
            {
                VirtuosoDateTimeOffset dt = (VirtuosoDateTimeOffset)value;

                TimeSpan tz_offset = dt.Offset;
                tz_offset_minutes = (int)(tz_offset.Hours * 60) + tz_offset.Minutes;

                long ticks = dt.Ticks - tz_offset.Ticks;
                dt = new VirtuosoDateTimeOffset(ticks, new TimeSpan(0));

                int year         = dt.Year;
                int month        = dt.Month;
                int day_of_month = dt.Day;
                days = GetDays(year, month, day_of_month);

                if (type == DateTimeType.DT_TYPE_DATETIME)
                {
                    hour     = dt.Hour;
                    minute   = dt.Minute;
                    second   = dt.Second;
                    fraction = (int)dt.Microsecond;
                }
                else if (type == DateTimeType.DT_TYPE_DATE)
                {
                    hour = minute = second = fraction = 0;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
#endif
            else if (value is VirtuosoTimeSpan)
            {
                if (type != DateTimeType.DT_TYPE_TIME)
                {
                    throw new InvalidCastException();
                }

                days = Values.DAY_ZERO;
                tz_offset_minutes = 0;

                VirtuosoTimeSpan ts = (VirtuosoTimeSpan)value;
                hour     = ts.Hours;
                minute   = ts.Minutes;
                second   = ts.Seconds;
                fraction = (int)ts.Microseconds;
            }
            else
            {
                throw new InvalidCastException();
            }

            if (bytes == null)
            {
                bytes = new byte[10];
            }

            bytes[0] = (byte)(days >> 16);
            bytes[1] = (byte)(days >> 8);
            bytes[2] = (byte)days;
            bytes[3] = (byte)hour;
            bytes[4] = (byte)((minute << 2) | (second >> 4));
            bytes[5] = (byte)((second << 4) | (fraction >> 16));
            bytes[6] = (byte)(fraction >> 8);
            bytes[7] = (byte)fraction;
            bytes[8] = (byte)(((tz_offset_minutes >> 8) & 0x07) | ((int)type << 5));
            bytes[9] = (byte)tz_offset_minutes;
        }