示例#1
0
文件: native.c 项目: trmmy/bson-ruby
/**
 * Converts the milliseconds time to the raw BSON bytes. We need to
 * explicitly convert using 64 bit here.
 *
 * @example Convert the milliseconds value to BSON bytes.
 *    rb_time_to_bson(time, 2124132340000, encoded);
 *
 * @param [ Time ] self The Ruby Time object.
 * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
{
  double t = NUM2DBL(rb_funcall(self, rb_intern("to_f"), 0));
  int64_t milliseconds = (int64_t)(t * 1000);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  return int64_t_to_bson(milliseconds, encoded);
}
示例#2
0
/**
 * Converts the milliseconds time to the raw BSON bytes. We need to
 * explicitly convert using 64 bit here.
 *
 * @example Convert the milliseconds value to BSON bytes.
 *    rb_time_to_bson(time, 2124132340000, encoded);
 *
 * @param [ Time ] self The Ruby Time object.
 * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
{
  int64_t t = NUM2INT64(rb_funcall(self, rb_intern("to_i"), 0));
  int64_t milliseconds = (int64_t)(t * 1000);
  int32_t micro = NUM2INT(rb_funcall(self, rb_intern("usec"), 0));
  int64_t time = milliseconds + (micro / 1000);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  return int64_t_to_bson(time, encoded);
}
示例#3
0
文件: native.c 项目: trmmy/bson-ruby
/**
 * Convert the Ruby integer into a BSON as per the 64 bit specification,
 * which is 8 bytes.
 *
 * @example Convert the integer to 64bit BSON.
 *    rb_integer_to_bson_int64(128, encoded);
 *
 * @param [ Integer ] self The Ruby integer.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_to_bson_int64(VALUE self, VALUE encoded)
{
  return int64_t_to_bson(NUM2INT64(self), encoded);
}