Example #1
0
        public static object Load(ReaderSites /*!*/ sites, RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] MutableString /*!*/ source, [Optional] Proc proc)
        {
            BinaryReader  reader = new BinaryReader(new MemoryStream(source.ConvertToBytes()));
            MarshalReader loader = new MarshalReader(sites, reader, scope.GlobalScope, proc);

            return(loader.Load());
        }
Example #2
0
        public static DateTime Load(RubyContext /*!*/ context, object /*!*/ self, [NotNull] MutableString time)
        {
            byte[] data = time.ConvertToBytes();
            if (data.Length != 8 || (data[3] & 0x80) != 0x80)
            {
                throw RubyExceptions.CreateTypeError("marshaled time format differ");
            }
            bool isUtc  = (data[3] & 0x40) != 0;
            uint dword1 = GetUint(data, 0);
            int  year   = 1900 + (int)((dword1 >> 14) & 0xffff);
            int  month  = 1 + (int)((dword1 >> 10) & 0x0f);
            int  day    = (int)((dword1 >> 5) & 0x01f);
            int  hour   = (int)(dword1 & 0x01f);

            uint dword2 = GetUint(data, 4);
            int  minute = (int)((dword2 >> 26) & 0x2f);
            int  second = (int)((dword2 >> 20) & 0x2f);
            int  usec   = (int)(dword2 & 0xfffff);

            try {
                DateTime result = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc);
                result = result.AddTicks(usec * 10L);
                if (isUtc)
                {
                    result = result.ToLocalTime();
                }
                return(result);
            } catch (ArgumentOutOfRangeException) {
                throw RubyExceptions.CreateTypeError("marshaled time format differ");
            }
        }
Example #3
0
        public static RubyTime /*!*/ Load(RubyContext /*!*/ context, RubyClass /*!*/ self, [NotNull] MutableString /*!*/ time)
        {
            byte[] data = time.ConvertToBytes();
            if (data.Length != 8)
            {
                throw RubyExceptions.CreateTypeError("marshaled time format differ");
            }

            uint dword1 = GetUint(data, 0);
            uint dword2 = GetUint(data, 4);

            if ((data[3] & 0x80) == 0)
            {
                int  secondsSinceEpoch = (int)dword1;
                uint microseconds      = dword2;

                return(new RubyTime(RubyTime.ToLocalTime(RubyTime.Epoch.AddTicks(RubyTime.ToTicks(secondsSinceEpoch, microseconds)))));
            }
            else
            {
                bool isUtc = (data[3] & 0x40) != 0;
                int  year  = 1900 + (int)((dword1 >> 14) & 0xffff);
                int  month = 1 + (int)((dword1 >> 10) & 0x0f);
                int  day   = (int)((dword1 >> 5) & 0x01f);
                int  hour  = (int)(dword1 & 0x01f);

                int minute = (int)((dword2 >> 26) & 0x2f);
                int second = (int)((dword2 >> 20) & 0x2f);
                int usec   = (int)(dword2 & 0xfffff);

                DateTime result;
                try {
                    result = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc).AddTicks(usec * 10);
                } catch (ArgumentOutOfRangeException) {
                    throw RubyExceptions.CreateTypeError("marshaled time format differ");
                }

                return(new RubyTime(isUtc ? result : RubyTime.ToLocalTime(result)));
            }
        }
Example #4
0
 public static int FileControl(RubyIO /*!*/ self, [DefaultProtocol] int commandId, [Optional] MutableString arg)
 {
     return(self.FileControl(commandId, (arg != null) ? arg.ConvertToBytes() : null));
 }