Skip to content

Commit

Permalink
simlib: rework time.hh implementation and add and fix newly added war…
Browse files Browse the repository at this point in the history
…nings
  • Loading branch information
varqox committed May 25, 2024
1 parent 8264a65 commit 640c701
Show file tree
Hide file tree
Showing 45 changed files with 289 additions and 246 deletions.
5 changes: 3 additions & 2 deletions subprojects/sim/include/sim/inf_datetime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public:
}

InfDatetime& set_datetime(CStringView datetime) {
if (is_datetime(datetime)) {
if (is_datetime(datetime.c_str())) {
type = Type::DATE;
date = datetime;
} else {
Expand Down Expand Up @@ -158,7 +158,8 @@ inline InfDatetime inf_timestamp_to_InfDatetime(StringView str) {
} else if (str == "-inf") {
res.set_neg_inf();
} else {
res.set_datetime(from_unsafe{mysql_date(WONT_THROW(str2num<uint64_t>(str).value()))});
res.set_datetime(from_unsafe{utc_mysql_datetime(WONT_THROW(str2num<uint64_t>(str).value()))}
);
}

return res;
Expand Down
5 changes: 3 additions & 2 deletions subprojects/sim/include/sim/internal_files/internal_file.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ inline std::string path_of(decltype(InternalFile::id) id) {
return concat_tostr("internal_files/", id);
}

inline decltype(InternalFile::id)
new_internal_file_id(mysql::Connection& mysql, const std::string& curr_datetime = mysql_date()) {
inline decltype(InternalFile::id) new_internal_file_id(
mysql::Connection& mysql, const std::string& curr_datetime = utc_mysql_datetime()
) {
auto stmt =
mysql.execute(sql::InsertInto("internal_files (created_at)").values("?", curr_datetime));
return stmt.insert_id();
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/include/sim/old_sql_fields/datetime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public:
constexpr explicit Datetime(T&& str)
: Varbinary([&]() -> decltype(auto) {
auto s = concat_tostr(str);
throw_assert(is_datetime(s));
throw_assert(is_datetime(s.c_str()));
return s;
}()) {}

Expand All @@ -39,7 +39,7 @@ public:
int> = 0>
Datetime& operator=(T&& str) {
auto s = concat_tostr(str);
throw_assert(is_datetime(s));
throw_assert(is_datetime(s.c_str()));
Varbinary::operator=(s);
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/include/sim/sql/fields/datetime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public:

explicit Datetime(std::string str)
: Varbinary{[&]() -> decltype(auto) {
throw_assert(is_datetime(str));
throw_assert(is_datetime(str.c_str()));
return std::move(str);
}()} {}

Datetime& operator=(std::string str) {
throw_assert(is_datetime(str));
throw_assert(is_datetime(str.c_str()));
Varbinary::operator=(std::move(str));
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/include/sim/sql/fields/inf_datetime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public:

explicit InfDatetime(std::string str)
: Varbinary{[&]() -> decltype(auto) {
throw_assert(is_datetime(str));
throw_assert(is_datetime(str.c_str()));
return std::move(str);
}()} {}

InfDatetime& operator=(std::string str) {
throw_assert(is_datetime(str));
throw_assert(is_datetime(str.c_str()));
Varbinary::operator=(std::move(str));
return *this;
}
Expand Down
9 changes: 9 additions & 0 deletions subprojects/sim/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ if get_option('warning_level') > '0'
'-Wno-c++20-extensions',
'-Wno-gnu-zero-variadic-macro-arguments',
'-Wno-c++2b-extensions',
'-Wuninitialized',
'-Wnon-virtual-dtor',
'-Woverloaded-virtual',
'-Wduplicated-cond',
'-Wduplicated-branches',
'-Wlogical-op',
'-Wnull-dereference',
'-Wformat=2',
'-Wlifetime',
]
foreach warning : warnings
if cc.has_argument(warning)
Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main2(int argc, char** argv) {
"commit",
"-q",
"-m",
concat_tostr("Backup ", mysql_localdate(), " (", mysql_date(), " UTC)")}
concat_tostr("Backup ", local_mysql_datetime(), " (", utc_mysql_datetime(), " UTC)")}
);
run_command({"git", "--no-pager", "show", "--stat"});

Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/job_server/job_handlers/add_problem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void AddProblem::run(sim::mysql::Connection& mysql) {
return set_failure();
}

auto curr_datetime = mysql_date();
auto curr_datetime = utc_mysql_datetime();
auto create_package_res = add_or_reupload_problem::create_package_with_simfile(
job_log_holder_, mysql, input_package_path, *simfile, curr_datetime
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void ChangeProblemStatement::run(sim::mysql::Connection& mysql) {
auto pkg_path = sim::internal_files::old_path_of(problem_file_id);

old_mysql.prepare("INSERT INTO internal_files (created_at) VALUES(?)")
.bind_and_execute(mysql_date());
.bind_and_execute(utc_mysql_datetime());
uint64_t new_file_id = old_mysql.insert_id();
auto new_pkg_path = sim::internal_files::old_path_of(new_file_id);

Expand Down Expand Up @@ -82,7 +82,7 @@ void ChangeProblemStatement::run(sim::mysql::Connection& mysql) {

dest_zip.close(); // Write all data to the dest_zip

const auto current_date = mysql_date();
const auto current_date = utc_mysql_datetime();
// Add job to delete old problem file
old_mysql
.prepare("INSERT INTO jobs(file_id, creator, type, priority, status,"
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/src/job_server/job_handlers/delete_contest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void DeleteContest::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
contest_id_
);

Expand All @@ -52,7 +52,7 @@ void DeleteContest::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
contest_id_
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void DeleteContestProblem::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
contest_problem_id_
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void DeleteContestRound::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
contest_round_id_
);

Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/src/job_server/job_handlers/delete_problem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void DeleteProblem::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
problem_id_
);

Expand All @@ -64,7 +64,7 @@ void DeleteProblem::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
problem_id_
);

Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/job_server/job_handlers/delete_user.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void DeleteUser::run(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
user_id_
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void JudgeOrRejudge::run(sim::mysql::Connection& mysql) {
);
}

std::string judging_began = mysql_date();
std::string judging_began = utc_mysql_datetime();

job_log("Judging submission ", submission_id_, " (problem: ", problem_id, ')');
load_problem_package(sim::internal_files::old_path_of(problem_file_id));
Expand Down
6 changes: 3 additions & 3 deletions subprojects/sim/src/job_server/job_handlers/merge_problems.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void MergeProblems::run_impl(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
donor_problem_id_
);

Expand All @@ -87,7 +87,7 @@ void MergeProblems::run_impl(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Type::DELETE_FILE),
default_priority(OldJob::Type::DELETE_FILE),
EnumVal(OldJob::Status::PENDING),
mysql_date(),
utc_mysql_datetime(),
donor_problem_id_,
EnumVal(OldSubmission::Type::PROBLEM_SOLUTION)
);
Expand Down Expand Up @@ -127,7 +127,7 @@ void MergeProblems::run_impl(sim::mysql::Connection& mysql) {
EnumVal(OldJob::Status::PENDING),
default_priority(OldJob::Type::REJUDGE_SUBMISSION),
EnumVal(OldJob::Type::REJUDGE_SUBMISSION),
mysql_date(),
utc_mysql_datetime(),
donor_problem_id_
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void ResetProblemTimeLimits::run(sim::mysql::Connection& mysql) {
auto old_mysql = old_mysql::ConnectionView{mysql};

old_mysql.prepare("INSERT INTO internal_files (created_at) VALUES(?)")
.bind_and_execute(mysql_date());
.bind_and_execute(utc_mysql_datetime());
uint64_t new_file_id = old_mysql.insert_id();
auto new_pkg_path = sim::internal_files::old_path_of(new_file_id);

Expand All @@ -58,7 +58,7 @@ void ResetProblemTimeLimits::run(sim::mysql::Connection& mysql) {

dest_zip.close(); // Write all data to the dest_zip

const auto current_date = mysql_date();
const auto current_date = utc_mysql_datetime();
// Add job to delete old problem file
old_mysql
.prepare("INSERT INTO jobs(file_id, creator, type, priority, status,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void ReuploadProblem::run(sim::mysql::Connection& mysql) {
return set_failure();
}

auto curr_datetime = mysql_date();
auto curr_datetime = utc_mysql_datetime();
auto create_package_res = add_or_reupload_problem::create_package_with_simfile(
job_log_holder_, mysql, input_package_path, *simfile, curr_datetime
);
Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/sim/cpp_syntax_highlighter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ string CppSyntaxHighlighter::operator()(CStringView input) const {
begs[i++] = ESCAPED_CHARACTER;

if (is_digit(str[i])) { // Octals
i += (is_digit(str[i + 1]) ? 1 + bool(is_digit(str[i + 2])) : 0);
i += (is_digit(str[i + 1]) ? 1 + (is_digit(str[i + 2]) ? 1 : 0) : 0);
} else if (str[i] == 'x') { // Hexadecimal
i += 2;
} else if (str[i] == 'u') { // Unicode U+nnnn
Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/sim_merger/merger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ protected:
" with new id: ",
new_id,
" ",
mysql_date(system_clock::to_time_t(tp))
utc_mysql_datetime(system_clock::to_time_t(tp))
);
} else {
(void)new_id; // Suppress GCC warning
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sim/src/sim_merger/problems.hh
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public:
EnumVal(sim::jobs::OldJob::Status::PENDING),
default_priority(sim::jobs::OldJob::Type::MERGE_PROBLEMS),
EnumVal(sim::jobs::OldJob::Type::MERGE_PROBLEMS),
mysql_date(),
utc_mysql_datetime(),
src_id,
sim::jobs::MergeProblemsInfo(dest_id, false).dump()
);
Expand Down Expand Up @@ -232,7 +232,7 @@ private:
sim::jobs::OldJob::Type::RESET_PROBLEM_TIME_LIMITS_USING_MODEL_SOLUTION
),
EnumVal(sim::jobs::OldJob::Type::RESET_PROBLEM_TIME_LIMITS_USING_MODEL_SOLUTION),
mysql_date(),
utc_mysql_datetime(),
problem_new_id
);
}
Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/sim_upgrader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static int perform_upgrade(const string& sim_dir, sim::mysql::Connection& mysql)
.values(
"?, ?, ?, ?, ?, ?, ?, ?, ?",
sim::users::SIM_ROOT_ID,
mysql_date(),
utc_mysql_datetime(),
sim::users::User::Type::ADMIN,
"sim",
"sim",
Expand Down
8 changes: 4 additions & 4 deletions subprojects/sim/src/web_server/contest_entry_tokens/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::optional<TokensInfo> get_tokens_info_for(Context& ctx, decltype(Contest::id
if (not stmt.next()) {
return std::nullopt;
}
ti.curr_time = mysql_date();
ti.curr_time = utc_mysql_datetime();
ti.short_token_real_value = ti.short_token;
throw_assert(ti.short_token.has_value() == ti.short_token_expiration.has_value());
if (ti.short_token and ti.curr_time >= *ti.short_token_expiration) {
Expand Down Expand Up @@ -113,7 +113,7 @@ get_token_contest_info_for(Context& ctx, StringView token_or_short_token) {
"cet.token=? OR (cet.short_token=? AND cet.short_token_expiration>?)",
token_or_short_token,
token_or_short_token,
mysql_date()
utc_mysql_datetime()
)
);
stmt.res_bind(tci.contest_id, tci.contest_name, contest_is_public, cu_mode);
Expand Down Expand Up @@ -287,8 +287,8 @@ Response delete_(Context& ctx, decltype(Contest::id) contest_id) {
continue;
}

ti.short_token_expiration = mysql_date(
std::chrono::system_clock::now() + ContestEntryToken::SHORT_TOKEN_MAX_LIFETIME
ti.short_token_expiration = utc_mysql_datetime(
std::chrono::seconds{ContestEntryToken::SHORT_TOKEN_MAX_LIFETIME}.count()
);
ctx.mysql.execute(Update("contest_entry_tokens")
.set(
Expand Down
17 changes: 16 additions & 1 deletion subprojects/sim/src/web_server/http/response.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "response.hh"
#include "simlib/errmsg.hh"
#include "simlib/macros/throw.hh"

#include <simlib/concat_tostr.hh>
#include <simlib/time.hh>

namespace web_server::http {

void Response::set_cache(bool to_public, uint max_age, bool must_revalidate) {
headers["expires"] = date("%a, %d %b %Y %H:%M:%S GMT", time(nullptr) + max_age);
time_t curr_time;
if (time(&curr_time) == static_cast<time_t>(-1)) {
THROW("time()", errmsg());
}
curr_time += max_age;
struct tm t;
if (!gmtime_r(&curr_time, &t)) {
THROW("gmtime_r()", errmsg());
}
std::string datetime(64, '\0');
size_t len = strftime(datetime.data(), datetime.size(), "%a, %d %b %Y %H:%M:%S GMT", &t);
datetime.resize(len);

headers["expires"] = std::move(datetime);
headers["cache-control"] = concat_tostr(
(to_public ? "public" : "private"),
(must_revalidate ? "; must-revalidate" : ""),
Expand Down
Loading

0 comments on commit 640c701

Please sign in to comment.