static VALUE
time_mload(VALUE time, VALUE str)
{
struct time_object *tobj;
unsigned long p, s;
time_t sec;
long usec;
unsigned char *buf;
struct tm tm;
int i, gmt;
long nsec;
VALUE submicro;
time_modify(time);
submicro = rb_attr_get(str, id_submicro);
if (submicro != Qnil) {
st_delete(rb_generic_ivar_table(str), (st_data_t*)&id_submicro, 0);
}
rb_copy_generic_ivar(time, str);
StringValue(str);
buf = (unsigned char *)RSTRING_PTR(str);
if (RSTRING_LEN(str) != 8) {
rb_raise(rb_eTypeError, "marshaled time format differ");
}
p = s = 0;
for (i=0; i<4; i++) {
p |= buf[i]<<(8*i);
}
for (i=4; i<8; i++) {
s |= buf[i]<<(8*(i-4));
}
if ((p & (1UL<<31)) == 0) {
gmt = 0;
sec = p;
usec = s;
nsec = usec * 1000;
}
else {
p &= ~(1UL<<31);
gmt = (p >> 30) & 0x1;
tm.tm_year = (p >> 14) & 0xffff;
tm.tm_mon = (p >> 10) & 0xf;
tm.tm_mday = (p >> 5) & 0x1f;
tm.tm_hour = p & 0x1f;
tm.tm_min = (s >> 26) & 0x3f;
tm.tm_sec = (s >> 20) & 0x3f;
tm.tm_isdst = 0;
sec = make_time_t(&tm, Qtrue);
usec = (long)(s & 0xfffff);
nsec = usec * 1000;
if (submicro != Qnil) {
unsigned char *ptr;
long len;
int digit;
ptr = (unsigned char*)StringValuePtr(submicro);
len = RSTRING_LEN(submicro);
if (0 < len) {
if (10 <= (digit = ptr[0] >> 4)) goto end_submicro;
nsec += digit * 100;
if (10 <= (digit = ptr[0] & 0xf)) goto end_submicro;
nsec += digit * 10;
}
if (1 < len) {
if (10 <= (digit = ptr[1] >> 4)) goto end_submicro;
nsec += digit;
}
end_submicro: ;
}
}
time_overflow_p(&sec, &nsec);
GetTimeval(time, tobj);
tobj->tm_got = 0;
tobj->gmt = gmt;
tobj->ts.tv_sec = sec;
tobj->ts.tv_nsec = nsec;
return time;
}