fixed sub-second precision and rounding differences for time score
[rocksndiamonds.git] / src / libgame / misc.c
index 217895c035dfddb4b9adb8e46f9cb62b8bcd040c..569526af608cf081e8dd976e5e8d581f91567d8e 100644 (file)
@@ -888,6 +888,53 @@ char *getRealName(void)
   return real_name;
 }
 
+char *getFixedUserName(char *name)
+{
+  // needed because player name must be a fixed length string
+  char *name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1);
+
+  strncpy(name_new, name, MAX_PLAYER_NAME_LEN);
+  name_new[MAX_PLAYER_NAME_LEN] = '\0';
+
+  if (strlen(name) > MAX_PLAYER_NAME_LEN)              // name has been cut
+    if (strchr(name_new, ' '))
+      *strchr(name_new, ' ') = '\0';
+
+  return name_new;
+}
+
+char *getDefaultUserName(int nr)
+{
+  static char *user_name[MAX_PLAYER_NAMES] = { NULL };
+
+  nr = MIN(MAX(0, nr), MAX_PLAYER_NAMES - 1);
+
+  if (user_name[nr] == NULL)
+  {
+    user_name[nr] = (nr == 0 ? getLoginName() : EMPTY_PLAYER_NAME);
+    user_name[nr] = getFixedUserName(user_name[nr]);
+  }
+
+  return user_name[nr];
+}
+
+char *getTimestampFromEpoch(time_t epoch_seconds)
+{
+  struct tm *now = localtime(&epoch_seconds);
+  static char timestamp[20];
+
+  sprintf(timestamp, "%04d%02d%02d-%02d%02d%02d",
+         now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
+         now->tm_hour, now->tm_min, now->tm_sec);
+
+  return timestamp;
+}
+
+char *getCurrentTimestamp(void)
+{
+  return getTimestampFromEpoch(time(NULL));
+}
+
 time_t getFileTimestampEpochSeconds(char *filename)
 {
   struct stat file_status;