Shifted to safer time functions and utc time

This commit is contained in:
Linus Probert 2019-02-24 19:42:35 +01:00
parent 563ebf9204
commit 5ee20c9c20
2 changed files with 23 additions and 13 deletions

View File

@ -32,7 +32,7 @@ generate_random_seeds(void)
{ {
// Use seed for generating map seeds // Use seed for generating map seeds
bh_srand(seed); bh_srand(seed);
info("Core random seed: %d", seed); debug("Core random seed: %d", seed);
for (int i = 0; i < 20; ++i) { for (int i = 0; i < 20; ++i) {
map_seeds[i] = bh_rand(); map_seeds[i] = bh_rand();
} }
@ -40,7 +40,7 @@ generate_random_seeds(void)
// Set a more random seed for runtime random // Set a more random seed for runtime random
runtime_seed = (unsigned int) time(NULL); runtime_seed = (unsigned int) time(NULL);
bh_srand(runtime_seed); bh_srand(runtime_seed);
info("Runtime random seed: %d", runtime_seed); debug("Runtime random seed: %d", runtime_seed);
} }
static void static void

View File

@ -23,24 +23,32 @@
#define SECONDS_PER_HOUR 3600 #define SECONDS_PER_HOUR 3600
#define SECONDS_PER_MINUTE 60 #define SECONDS_PER_MINUTE 60
#ifdef WIN32
#define m_gmtime gmtime_s
#else
#define m_gmtime gmtime_r
#endif
time_t time_t
time_get_weekly_seed(void) time_get_weekly_seed(void)
{ {
time_t now = time(NULL); time_t now = time(NULL);
struct tm *tm; struct tm tm;
tm = localtime(&now); m_gmtime(&now, &tm);
// Zero out the hour and minutes to 00:00:01 // Zero out the hour and minutes to 00:00:01
now -= tm->tm_hour * SECONDS_PER_HOUR; now -= tm.tm_hour * SECONDS_PER_HOUR;
now -= tm->tm_min * SECONDS_PER_MINUTE; now -= tm.tm_min * SECONDS_PER_MINUTE;
now -= tm->tm_sec; now -= tm.tm_sec;
now += 1; now += 60; // Set it to one minute past midnight
// Reverse time back to last monday // Reverse time back to last monday
unsigned int dayOfWeek = tm->tm_wday; unsigned int dayOfWeek = tm.tm_wday;
now -= (dayOfWeek == 0 ? 6 : dayOfWeek - 1) * SECONDS_PER_DAY; now -= (dayOfWeek == 0 ? 6 : dayOfWeek - 1) * SECONDS_PER_DAY;
debug("Weekly seed: %u", now);
return now; return now;
} }
@ -49,14 +57,16 @@ char *
time_get_weekly_lb_name(void) time_get_weekly_lb_name(void)
{ {
time_t seed = time_get_weekly_seed(); time_t seed = time_get_weekly_seed();
struct tm *tm = localtime(&seed); struct tm tm;
m_gmtime(&seed, &tm);
char *name = ec_malloc(sizeof(char) * 15); char *name = ec_malloc(sizeof(char) * 15);
m_sprintf(name, m_sprintf(name,
15, 15,
"%u%.2u%.2u_weekly", "%u%.2u%.2u_weekly",
tm->tm_year % 100, tm.tm_year % 100,
tm->tm_mon + 1, tm.tm_mon + 1,
tm->tm_mday tm.tm_mday
); );
debug("Weekly leaderboard: %s", name);
return name; return name;
} }