Skip to content

Commit

Permalink
Add TIME_MILLIS counter
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Feb 1, 2019
1 parent dad6b81 commit 9e734e7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.txt
Expand Up @@ -9,6 +9,8 @@ FEATURES
example, if a robot is selected on the board before switching
to overlay mode, the robot will still be in the buffer when
the user switches back to board mode.
+ New counter: TIME_MILLIS. Returns the milliseconds of the
current system time.

FIXES

Expand Down
54 changes: 35 additions & 19 deletions src/counter.c
Expand Up @@ -25,6 +25,8 @@
#include <assert.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>

#include "configure.h"
#include "counter.h"
Expand All @@ -40,7 +42,6 @@
#include "robot.h"
#include "sprite.h"
#include "str.h"
#include "time.h"
#include "util.h"
#include "world.h"
#include "world_struct.h"
Expand Down Expand Up @@ -1165,52 +1166,66 @@ static void timereset_write(struct world *mzx_world,
mzx_world->current_board->time_limit = CLAMP(value, 0, 32767);
}

static struct tm *system_time(void)
{
static struct tm err;
struct timeval tv;
time_t e_time;
struct tm *t;

if(!gettimeofday(&tv, NULL))
e_time = tv.tv_sec;
else
e_time = time(NULL);

// If localtime returns NULL, return a tm with all zeros instead of crashing.
t = localtime(&e_time);
return t ? t : &err;
}

static int date_day_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_mday;
return system_time()->tm_mday;
}

static int date_year_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_year + 1900;
return system_time()->tm_year + 1900;
}

static int date_month_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_mon + 1;
return system_time()->tm_mon + 1;
}

static int time_hours_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_hour;
return system_time()->tm_hour;
}

static int time_minutes_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_min;
return system_time()->tm_min;
}

static int time_seconds_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
time_t e_time = time(NULL);
struct tm *t = localtime(&e_time);
return t->tm_sec;
return system_time()->tm_sec;
}

static int time_millis_read(struct world *mzx_world,
const struct function_counter *counter, const char *name, int id)
{
struct timeval tv;
if(!gettimeofday(&tv, NULL))
return (tv.tv_usec / 1000) % 1000;
return 0;
}

static int random_seed_read(struct world *mzx_world,
Expand Down Expand Up @@ -2510,6 +2525,7 @@ static const struct function_counter builtin_counters[] =
{ "this_color", V260, this_color_read, NULL },
{ "timereset", ALL, timereset_read, timereset_write },
{ "time_hours", V260, time_hours_read, NULL },
{ "time_millis", V292, time_millis_read, NULL },
{ "time_minutes", V260, time_minutes_read, NULL },
{ "time_seconds", V260, time_seconds_read, NULL },
{ "uch!,!", V284, uch_read, NULL },
Expand Down
7 changes: 7 additions & 0 deletions src/editor/debug.c
Expand Up @@ -194,6 +194,13 @@ static const char *universal_var_list[] =
{
"random_seed0",
"random_seed1",
"date_year*",
"date_month*",
"date_day*",
"time_hours*",
"time_minutes*",
"time_seconds*",
"time_millis*",
};

static const char *world_var_list[] =
Expand Down

0 comments on commit 9e734e7

Please sign in to comment.