Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bind.h"
#include "component/components.h"
#include "main.h"
#include "main_css.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this line is causing the build error

#include "settings.h"
#include "timer.h"

Expand Down
57 changes: 45 additions & 12 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,32 @@ long long ls_time_value(const char* string)
if (!string || !strlen(string)) {
return 0;
}
sscanf(string, "%[^.]%lf", seconds_part, &subseconds_part);
string = seconds_part;
if (string[0] == '-') {

// Split at the decimal point manually
char* dot_pos = strchr(string, '.');
if (dot_pos) {
strncpy(seconds_part, string, dot_pos - string);
seconds_part[dot_pos - string] = '\0';

// Manually parse the fractional part to avoid locale issues
char* frac_part = dot_pos + 1;
subseconds_part = 0.0;
double multiplier = 0.1;

for (char* p = frac_part; *p && *p >= '0' && *p <= '9'; p++) {
subseconds_part += (*p - '0') * multiplier;
multiplier *= 0.1;
}
} else {
strcpy(seconds_part, string);
subseconds_part = 0.0;
}

if (seconds_part[0] == '-') {
sign = -1;
++string;
memmove(seconds_part, seconds_part + 1, strlen(seconds_part));
}
switch (sscanf(string, "%d:%d:%d", &hours, &minutes, &seconds)) {
switch (sscanf(seconds_part, "%d:%d:%d", &hours, &minutes, &seconds)) {
case 2:
seconds = minutes;
minutes = hours;
Expand All @@ -41,7 +60,8 @@ long long ls_time_value(const char* string)
hours = 0;
break;
}
return sign * ((hours * 60 * 60 + minutes * 60 + seconds) * 1000000LL + (int)(subseconds_part * 1000000.));

return sign * ((hours * 60 * 60 + minutes * 60 + seconds) * 1000000LL + (long long)(subseconds_part * 1000000.));
}

static void ls_time_string_format(char* string,
Expand Down Expand Up @@ -398,12 +418,25 @@ int ls_game_save(const ls_game* game)
json_t* split = json_object();
json_object_set_new(split, "title",
json_string(game->split_titles[i]));
ls_time_string_serialized(str, game->split_times[i]);
json_object_set_new(split, "time", json_string(str));
ls_time_string_serialized(str, game->best_splits[i]);
json_object_set_new(split, "best_time", json_string(str));
ls_time_string_serialized(str, game->best_segments[i]);
json_object_set_new(split, "best_segment", json_string(str));

// Only save "time" if it's not zero
if (game->split_times[i] != 0) {
ls_time_string_serialized(str, game->split_times[i]);
json_object_set_new(split, "time", json_string(str));
}

// Only save "best_time" if it's not zero
if (game->best_splits[i] != 0) {
ls_time_string_serialized(str, game->best_splits[i]);
json_object_set_new(split, "best_time", json_string(str));
}

// Only save "best_segment" if it's not zero
if (game->best_segments[i] != 0) {
ls_time_string_serialized(str, game->best_segments[i]);
json_object_set_new(split, "best_segment", json_string(str));
}

json_array_append_new(splits, split);
}
json_object_set_new(json, "splits", splits);
Expand Down
Loading