// Helper functions for write_* and update_* since they do almost the same thing. static PyObject* _cdb_write_or_update_records(PyObject* self, PyObject* list, int type) { int len = PyList_Size(list); uint64_t i = 0; uint64_t cnt = 0; bool ret = false; cdb_t *cdb = ((StorageObject*)self)->cdb; cdb_record_t *records = alloca(sizeof(cdb_record_t) * len); // Turn any None's into NaN for (i = 0; i < len; i++) { PyObject* record = PyList_GetItem(list, i); PyObject* value = PyList_GetItem(record, 1); records[i].time = _parse_time(PyList_GetItem(record, 0)); records[i].value = (value == Py_None ? CDB_NAN : PyFloat_AsDouble(value)); } if (type == PY_CDB_WRITE) { ret = cdb_write_records(cdb, records, len, &cnt); } else { ret = cdb_update_records(cdb, records, len, &cnt); } if (ret != CDB_SUCCESS) { return PyErr_SetFromErrno(PyExc_IOError); } _cdb_py_update_header(self, cdb); return PyInt_FromLong(cnt); }
static void _rtc_setalarm(char **argv) { struct tm now; if (_parse_time(argv, &now) == 0) { if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) { puts("rtc: error setting alarm"); } } }
static void _rtc_settime(char **argv) { struct tm now; if (_parse_time(argv, &now) == 0) { if (rtc_set_time(&now) == -1) { puts("rtc: error setting time"); } } }
static PyObject* _cdb_write_or_update_record(PyObject* self, PyObject* time, PyObject* value, int type) { bool ret = false; cdb_t *cdb = ((StorageObject*)self)->cdb; // Convert None to NAN, which is what the circulardb code expects if (value == Py_None) { value = PyFloat_FromDouble(CDB_NAN); } if (type == PY_CDB_WRITE) { ret = cdb_write_record(cdb, _parse_time(time), PyFloat_AsDouble(value)); } else { ret = cdb_update_record(cdb, _parse_time(time), PyFloat_AsDouble(value)); } if (ret == false) { return PyErr_SetFromErrno(PyExc_IOError); } _cdb_py_update_header(self, cdb); return PyInt_FromLong(ret); }
/*JSON{ "type" : "staticmethod", "class" : "Date", "name" : "parse", "generate" : "jswrap_date_parse", "params" : [ ["str","JsVar","A String"] ], "return" : ["float","The number of milliseconds since 1970"] } Parse a date string and return milliseconds since 1970. Data can be either '2011-10-20T14:48:00', '2011-10-20' or 'Mon, 25 Dec 1995 13:30:00 +0430' */ JsVarFloat jswrap_date_parse(JsVar *str) { if (!jsvIsString(str)) return 0; TimeInDay time; time.daysSinceEpoch = 0; time.hour = 0; time.min = 0; time.sec = 0; time.ms = 0; time.zone = 0; CalendarDate date = getCalendarDate(0); JsLex lex; jslInit(&lex, str); if (lex.tk == LEX_ID) { date.month = getMonth(jslGetTokenValueAsString(&lex)); date.dow = getDay(jslGetTokenValueAsString(&lex)); if (date.month>=0) { // Aug 9, 1995 jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk==',') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { _parse_time(&lex, &time, 0); } } } } } else if (date.dow>=0) { date.month = 0; jslGetNextToken(&lex); if (lex.tk==',') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_ID && getMonth(jslGetTokenValueAsString(&lex))>=0) { date.month = getMonth(jslGetTokenValueAsString(&lex)); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { _parse_time(&lex, &time, 0); } } } } } } else { date.dow = 0; date.month = 0; } } else if (lex.tk == LEX_INT) { // assume 2011-10-10T14:48:00 format date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk=='-') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.month = _parse_int(&lex) - 1; jslGetNextToken(&lex); if (lex.tk=='-') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_ID && jslGetTokenValueAsString(&lex)[0]=='T') { _parse_time(&lex, &time, 1); } } } } } } jslKill(&lex); time.daysSinceEpoch = fromCalenderDate(&date); return fromTimeInDay(&time); }
static void _handle_time (player *pl, world *w, command_reader& reader) { std::ostringstream ss; if (!reader.has_next ()) { pl->message ("§6Displaying world time for " + w->get_colored_name () + "§e:"); pl->message ("§7 " + _get_time_str (w->get_time ()) + (w->is_time_frozen () ? " [Frozen]" : "")); return; } std::string arg = reader.next (); if (sutils::iequals (arg, "set")) { if (!pl->has ("command.world.world.time")) { pl->message (messages::not_allowed ()); return; } if (!reader.has_next ()) { pl->message ("§c * §7Usage§f: §e/world time set §ctime"); return; } unsigned long long time = 0; arg = reader.next ().as_str (); if (!_parse_time (arg, time)) { pl->message ("§c * §7Invalid time string§c."); return; } w->set_time (time); ss << "§eWorld time for " << w->get_colored_name () << " §ehas been set to§f: §9" << _get_time_str (time) << " §f[§9" << (time % 24000) << " ticks§f]"; pl->message (ss.str ()); } else if (sutils::iequals (arg, "stop")) { if (!pl->has ("command.world.world.time")) { pl->message (messages::not_allowed ()); return; } if (w->is_time_frozen ()) { pl->message ("§c * §Time is already frozen in this world§c."); return; } w->stop_time (); pl->message ("§eWorld time has been stopped in world " + w->get_colored_name ()); } else if (sutils::iequals (arg, "resume")) { if (!pl->has ("command.world.world.time")) { pl->message (messages::not_allowed ()); return; } if (!w->is_time_frozen ()) { pl->message ("§c * §Time is not frozen in this world§c."); return; } w->resume_time (); pl->message ("§eWorld time has been resumed in world " + w->get_colored_name ()); } else { pl->message ("§c * §7Unknown sub-command§f: §ctime." + arg); } }