static dfsch_object_t* decoded_time_apply(decoded_time_t* time, dfsch_object_t* args, dfsch_tail_escape_t* esc){ dfsch_object_t* selector; DFSCH_OBJECT_ARG(args, selector); DFSCH_ARG_END(args); if (dfsch_compare_keyword(selector, "sec")){ return dfsch_make_number_from_long(time->tm.tm_sec); } else if (dfsch_compare_keyword(selector, "min")){ return dfsch_make_number_from_long(time->tm.tm_min); } else if (dfsch_compare_keyword(selector, "hour")){ return dfsch_make_number_from_long(time->tm.tm_hour); } else if (dfsch_compare_keyword(selector, "date")){ return dfsch_make_number_from_long(time->tm.tm_mday); } else if (dfsch_compare_keyword(selector, "month")){ return dfsch_make_number_from_long(time->tm.tm_mon + 1); } else if (dfsch_compare_keyword(selector, "year")){ return dfsch_make_number_from_long(time->tm.tm_year + 1900); } else if (dfsch_compare_keyword(selector, "day")){ return dfsch_make_number_from_long(time->tm.tm_wday); } else if (dfsch_compare_keyword(selector, "year-day")){ return dfsch_make_number_from_long(time->tm.tm_yday + 1); } else if (dfsch_compare_keyword(selector, "dst?")){ return dfsch_bool(time->tm.tm_isdst == 1); } dfsch_error("Unknown field requested", selector); }
static dfsch_object_t* convert_info(rrd_info_t * data){ dfsch_object_t* res = dfsch_make_idhash(); while (data) { dfsch_object_t* val = NULL; switch (data->type) { case RD_I_VAL: val = isnan(data->value.u_val) ? NULL : dfsch_make_number_from_double(data->value.u_val); break; case RD_I_CNT: val = dfsch_make_number_from_uint64(data->value.u_cnt); break; case RD_I_INT: val = dfsch_make_number_from_long(data->value.u_int); break; case RD_I_STR: val = dfsch_make_string_cstr(data->value.u_str); break; case RD_I_BLO: val = dfsch_make_byte_vector((char *) data->value.u_blo.ptr, data->value.u_blo.size); break; } dfsch_idhash_set((dfsch_hash_t*)res, dfsch_make_keyword(data->key), val); data = data->next; } return res; }
void dfsch__system_register(dfsch_object_t *ctx){ dfsch_defcanon_cstr(ctx, "<decoded-time>", &decoded_time_type); dfsch_defcanon_cstr(ctx, "decode-universal-time", DFSCH_PRIMITIVE_REF(decode_universal_time)); dfsch_defcanon_cstr(ctx, "encode-universal-time", DFSCH_PRIMITIVE_REF(encode_universal_time)); dfsch_defcanon_cstr(ctx, "get-decoded-time", DFSCH_PRIMITIVE_REF(get_decoded_time)); dfsch_defcanon_cstr(ctx, "get-universal-time", DFSCH_PRIMITIVE_REF(get_universal_time)); dfsch_defcanon_cstr(ctx, "iso-format-time", DFSCH_PRIMITIVE_REF(iso_format_time)); #ifdef unix dfsch_defcanon_cstr(ctx, "get-internal-real-time", DFSCH_PRIMITIVE_REF(get_internal_real_time)); dfsch_defcanon_cstr(ctx, "get-internal-run-time", DFSCH_PRIMITIVE_REF(get_internal_run_time)); dfsch_defcanon_cstr(ctx, "internal-time-units-per-second", dfsch_make_number_from_long(sysconf(_SC_CLK_TCK))); #endif dfsch_defcanon_cstr(ctx, "sleep", DFSCH_PRIMITIVE_REF(sleep)); }
DFSCH_DEFINE_PRIMITIVE(get_internal_run_time, NULL){ struct tms t; DFSCH_ARG_END(args); times(&t); return dfsch_make_number_from_long((t.tms_utime + t.tms_stime) & LONG_MAX); }
static dfsch_object_t* regex_substrings(regex_t* regex, char* string, int sub_count, int flags){ regmatch_t *match; int i; int count; dfsch_object_t* vector; match = GC_MALLOC_ATOMIC(sizeof(regmatch_t)*sub_count); if (regexec(regex, string, sub_count, match, flags) == REG_NOMATCH){ GC_FREE(match); return NULL; } count = 0; for (i = 0; i < sub_count; i++){ if (match[i].rm_so == -1) // No more substring matches break; count ++; } vector = dfsch_make_vector(count, NULL); for (i = 0; i < count; i++){ dfsch_vector_set(vector, i, dfsch_vector(3, dfsch_make_number_from_long(match[i].rm_so), dfsch_make_number_from_long(match[i].rm_eo), dfsch_make_string_buf(string+match[i].rm_so, match[i].rm_eo-match[i].rm_so))); } GC_FREE(match); return vector; }
DFSCH_DEFINE_PRIMITIVE(encode_universal_time, NULL){ struct tm tm; time_t t; DFSCH_LONG_ARG(args, tm.tm_sec); DFSCH_LONG_ARG(args, tm.tm_min); DFSCH_LONG_ARG(args, tm.tm_hour); DFSCH_LONG_ARG(args, tm.tm_mday); DFSCH_LONG_ARG(args, tm.tm_mon); DFSCH_LONG_ARG(args, tm.tm_year); DFSCH_ARG_END(args); tm.tm_mon -= 1; tm.tm_year -= 1900; tm.tm_isdst = -1; errno = 0; t = mktime(&tm); if (t == -1 && errno != 0){ dfsch_operating_system_error("mktime"); } return dfsch_make_number_from_long(t); }
DFSCH_DEFINE_PRIMITIVE(get_internal_real_time, NULL){ struct tms t; DFSCH_ARG_END(args); return dfsch_make_number_from_long(times(&t) & LONG_MAX); }
DFSCH_DEFINE_PRIMITIVE(get_universal_time, NULL){ DFSCH_ARG_END(args); return dfsch_make_number_from_long(time(NULL)); }
static void errno_error(char* name, dfsch_object_t* object, int e){ dfsch_error(name, dfsch_list(3, object, dfsch_make_number_from_long(e), dfsch_make_string_cstr(strerror(e)))); }