Class: ATProto::TID

Inherits:
Object
  • Object
show all
Defined in:
lib/at_protocol/tid_ext.rb,
ext/at_protocol/tid.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/at_protocol/tid.c', line 21

static VALUE tid_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE time_arg = Qnil;
  rb_scan_args(argc, argv, "01", &time_arg);

  TID *tid;
  Data_Get_Struct(self, TID, tid);

  if (NIL_P(time_arg)) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    tid->timestamp = (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec;
  } else {
    if (!rb_obj_is_kind_of(time_arg, rb_cTime)) {
      rb_raise(rb_eTypeError, "Argument must be a Time object or nil");
      return Qnil;
    }

    struct timeval tv;
    tv.tv_sec = NUM2LONG(rb_funcall(time_arg, rb_intern("tv_sec"), 0));
    tv.tv_usec = NUM2LONG(rb_funcall(time_arg, rb_intern("tv_usec"), 0));
    tid->timestamp = (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec;
  }

  tid->clock_identifier = rand() & 0x3FF;

  return self;
}

Class Method Details

.from_string(str) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/at_protocol/tid.c', line 75

static VALUE tid_from_string(VALUE klass, VALUE str) {
  Check_Type(str, T_STRING);

  TID *tid;
  VALUE tid_obj = Data_Make_Struct(klass, TID, NULL, xfree, tid);

  if (RSTRING_LEN(str) != 13) {
    rb_raise(rb_eArgError, "TID string must be 13 characters long");
    return Qnil;
  }

  char tid_str[14];
  strncpy(tid_str, StringValueCStr(str), sizeof(tid_str));
  tid_str[13] = '\0';

  // Convert the TID string back to a TID object
  uint64_t timestamp = 0;
  for (int i = 0; i < 11; i++) {
    char c = tid_str[i];
    const char *pos = strchr(b32_chars, c);
    if (pos == NULL) {
      rb_raise(rb_eArgError, "Invalid character in TID string");
      return Qnil;
    }
    int index = (int)(pos - b32_chars);
    timestamp = (timestamp << 5) | (index & 0x1F);
  }

  uint64_t clock_identifier = 0;
  for (int i = 11; i < 13; i++) {
    char c = tid_str[i];
    const char *pos = strchr(b32_chars, c);
    if (pos == NULL) {
      rb_raise(rb_eArgError, "Invalid character in TID string");
      return Qnil;
    }
    int index = (int)(pos - b32_chars);
    clock_identifier = (clock_identifier << 5) | (index & 0x1F);
  }

  tid->timestamp = timestamp;
  tid->clock_identifier = clock_identifier;

  return tid_obj;
}

Instance Method Details

#inspectObject



16
17
18
# File 'lib/at_protocol/tid_ext.rb', line 16

def inspect
  "#<ATProto::TID(#{self.to_s})>"
end

#to_sObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/at_protocol/tid.c', line 59

static VALUE tid_to_s(VALUE self) {
  TID *tid;
  Data_Get_Struct(self, TID, tid);

  char tid_str[14];
  for (int i = 0; i < 11; i++) {
    int index = (int)((tid->timestamp >> (50 - i * 5)) & 0x1F);
    tid_str[i] = b32_chars[index];
  }
  tid_str[11] = b32_chars[(int)(tid->clock_identifier >> 6) & 0x1F];
  tid_str[12] = b32_chars[(int)tid->clock_identifier & 0x1F];
  tid_str[13] = '\0';

  return rb_str_new_cstr(tid_str);
}

#to_timeObject



49
50
51
52
53
54
55
56
57
# File 'ext/at_protocol/tid.c', line 49

static VALUE tid_to_time(VALUE self) {
  TID *tid;
  Data_Get_Struct(self, TID, tid);

  VALUE sec = LL2NUM(tid->timestamp / 1000000);
  VALUE usec = LL2NUM(tid->timestamp % 1000000);

  return rb_funcall(rb_cTime, rb_intern("at"), 2, sec, usec);
}