MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

date functions

+132 -1
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.1.0.25') 77 + version_conf.set('ANT_VERSION', '0.1.0.26') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+131
src/ant.c
··· 8326 8326 return tov(timestamp_ms); 8327 8327 } 8328 8328 8329 + static double date_get_time(struct js *js, jsval_t this_val) { 8330 + jsoff_t time_off = lkp(js, this_val, "__time", 6); 8331 + if (time_off == 0) return NAN; 8332 + jsval_t time_val = resolveprop(js, mkval(T_PROP, time_off)); 8333 + if (vtype(time_val) != T_NUM) return NAN; 8334 + return tod(time_val); 8335 + } 8336 + 8337 + static jsval_t builtin_Date_getTime(struct js *js, jsval_t *args, int nargs) { 8338 + (void) args; 8339 + (void) nargs; 8340 + return tov(date_get_time(js, js->this_val)); 8341 + } 8342 + 8343 + static jsval_t builtin_Date_getFullYear(struct js *js, jsval_t *args, int nargs) { 8344 + (void) args; 8345 + (void) nargs; 8346 + double ms = date_get_time(js, js->this_val); 8347 + if (isnan(ms)) return tov(NAN); 8348 + time_t t = (time_t)(ms / 1000.0); 8349 + struct tm *tm = localtime(&t); 8350 + return tov((double)(tm->tm_year + 1900)); 8351 + } 8352 + 8353 + static jsval_t builtin_Date_getMonth(struct js *js, jsval_t *args, int nargs) { 8354 + (void) args; 8355 + (void) nargs; 8356 + double ms = date_get_time(js, js->this_val); 8357 + if (isnan(ms)) return tov(NAN); 8358 + time_t t = (time_t)(ms / 1000.0); 8359 + struct tm *tm = localtime(&t); 8360 + return tov((double)tm->tm_mon); 8361 + } 8362 + 8363 + static jsval_t builtin_Date_getDate(struct js *js, jsval_t *args, int nargs) { 8364 + (void) args; 8365 + (void) nargs; 8366 + double ms = date_get_time(js, js->this_val); 8367 + if (isnan(ms)) return tov(NAN); 8368 + time_t t = (time_t)(ms / 1000.0); 8369 + struct tm *tm = localtime(&t); 8370 + return tov((double)tm->tm_mday); 8371 + } 8372 + 8373 + static jsval_t builtin_Date_getHours(struct js *js, jsval_t *args, int nargs) { 8374 + (void) args; 8375 + (void) nargs; 8376 + double ms = date_get_time(js, js->this_val); 8377 + if (isnan(ms)) return tov(NAN); 8378 + time_t t = (time_t)(ms / 1000.0); 8379 + struct tm *tm = localtime(&t); 8380 + return tov((double)tm->tm_hour); 8381 + } 8382 + 8383 + static jsval_t builtin_Date_getMinutes(struct js *js, jsval_t *args, int nargs) { 8384 + (void) args; 8385 + (void) nargs; 8386 + double ms = date_get_time(js, js->this_val); 8387 + if (isnan(ms)) return tov(NAN); 8388 + time_t t = (time_t)(ms / 1000.0); 8389 + struct tm *tm = localtime(&t); 8390 + return tov((double)tm->tm_min); 8391 + } 8392 + 8393 + static jsval_t builtin_Date_getSeconds(struct js *js, jsval_t *args, int nargs) { 8394 + (void) args; 8395 + (void) nargs; 8396 + double ms = date_get_time(js, js->this_val); 8397 + if (isnan(ms)) return tov(NAN); 8398 + time_t t = (time_t)(ms / 1000.0); 8399 + struct tm *tm = localtime(&t); 8400 + return tov((double)tm->tm_sec); 8401 + } 8402 + 8403 + static jsval_t builtin_Date_getMilliseconds(struct js *js, jsval_t *args, int nargs) { 8404 + (void) args; 8405 + (void) nargs; 8406 + double ms = date_get_time(js, js->this_val); 8407 + if (isnan(ms)) return tov(NAN); 8408 + return tov(fmod(ms, 1000.0)); 8409 + } 8410 + 8411 + static jsval_t builtin_Date_getDay(struct js *js, jsval_t *args, int nargs) { 8412 + (void) args; 8413 + (void) nargs; 8414 + double ms = date_get_time(js, js->this_val); 8415 + if (isnan(ms)) return tov(NAN); 8416 + time_t t = (time_t)(ms / 1000.0); 8417 + struct tm *tm = localtime(&t); 8418 + return tov((double)tm->tm_wday); 8419 + } 8420 + 8421 + static jsval_t builtin_Date_toISOString(struct js *js, jsval_t *args, int nargs) { 8422 + (void) args; 8423 + (void) nargs; 8424 + double ms = date_get_time(js, js->this_val); 8425 + if (isnan(ms)) return js_mkerr(js, "Invalid Date"); 8426 + time_t t = (time_t)(ms / 1000.0); 8427 + struct tm *tm = gmtime(&t); 8428 + int millis = (int)fmod(ms, 1000.0); 8429 + if (millis < 0) millis += 1000; 8430 + char buf[32]; 8431 + snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", 8432 + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 8433 + tm->tm_hour, tm->tm_min, tm->tm_sec, millis); 8434 + return js_mkstr(js, buf, strlen(buf)); 8435 + } 8436 + 8437 + static jsval_t builtin_Date_toString(struct js *js, jsval_t *args, int nargs) { 8438 + (void) args; 8439 + (void) nargs; 8440 + double ms = date_get_time(js, js->this_val); 8441 + if (isnan(ms)) return js_mkstr(js, "Invalid Date", 12); 8442 + time_t t = (time_t)(ms / 1000.0); 8443 + char *s = ctime(&t); 8444 + size_t len = strlen(s); 8445 + if (len > 0 && s[len - 1] == '\n') len--; 8446 + return js_mkstr(js, s, len); 8447 + } 8448 + 8329 8449 static jsval_t builtin_Math_abs(struct js *js, jsval_t *args, int nargs) { 8330 8450 (void) js; 8331 8451 if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(NAN); ··· 13744 13864 13745 13865 jsval_t date_proto = js_mkobj(js); 13746 13866 set_proto(js, date_proto, object_proto); 13867 + setprop(js, date_proto, js_mkstr(js, "getTime", 7), js_mkfun(builtin_Date_getTime)); 13868 + setprop(js, date_proto, js_mkstr(js, "getFullYear", 11), js_mkfun(builtin_Date_getFullYear)); 13869 + setprop(js, date_proto, js_mkstr(js, "getMonth", 8), js_mkfun(builtin_Date_getMonth)); 13870 + setprop(js, date_proto, js_mkstr(js, "getDate", 7), js_mkfun(builtin_Date_getDate)); 13871 + setprop(js, date_proto, js_mkstr(js, "getHours", 8), js_mkfun(builtin_Date_getHours)); 13872 + setprop(js, date_proto, js_mkstr(js, "getMinutes", 10), js_mkfun(builtin_Date_getMinutes)); 13873 + setprop(js, date_proto, js_mkstr(js, "getSeconds", 10), js_mkfun(builtin_Date_getSeconds)); 13874 + setprop(js, date_proto, js_mkstr(js, "getMilliseconds", 15), js_mkfun(builtin_Date_getMilliseconds)); 13875 + setprop(js, date_proto, js_mkstr(js, "getDay", 6), js_mkfun(builtin_Date_getDay)); 13876 + setprop(js, date_proto, js_mkstr(js, "toISOString", 11), js_mkfun(builtin_Date_toISOString)); 13877 + setprop(js, date_proto, js_mkstr(js, "toString", 8), js_mkfun(builtin_Date_toString)); 13747 13878 13748 13879 jsval_t regexp_proto = js_mkobj(js); 13749 13880 set_proto(js, regexp_proto, object_proto);