From 58314f359af20944c525911b07dea96e72fd4a6f Mon Sep 17 00:00:00 2001
From: Richard Harrison <rjh@zaretto.com>
Date: Sun, 20 Jan 2019 23:24:38 +0100
Subject: [PATCH] Change screenshot filename to have date time

Format fgfs-YYYYMMDDHHMMSS[-#] where the # is for the rare case when there are multiple screenshots per second.

The change makes the filenames sortable based on date and slightly improves run time performance when there are a lot of screenshots in a particular directory
---
 src/GUI/gui_funcs.cxx | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/GUI/gui_funcs.cxx b/src/GUI/gui_funcs.cxx
index 6ab2abac3..61ec83692 100644
--- a/src/GUI/gui_funcs.cxx
+++ b/src/GUI/gui_funcs.cxx
@@ -441,16 +441,20 @@ namespace
 
     SGPath nextScreenshotPath(const SGPath& screenshotDir)
     {
-        char filename[32];
-        static int count = 1;
-        while (count < 0x7fff) {
-            if (count < 1000) {
-                // zero-pad for backwards compatability
-                snprintf(filename, 32, "fgfs-screen-%03d.png", count++);
-            } else {
-                snprintf(filename, 32, "fgfs-screen-%d.png", count++);
-            }
-            
+        char filename[60];
+        int count = 0;
+        while (count < 100) { // 100 per second should be more than enough.
+            char time_str[20];
+            time_t calendar_time = time(NULL);
+            struct tm *tmUTC;
+            tmUTC = gmtime(&calendar_time);
+            strftime(time_str, sizeof(time_str), "%Y%m%d%H%M%S", tmUTC);
+
+            if (count)
+                snprintf(filename, 32, "fgfs-%s-%d.png", time_str, count++);
+            else
+                snprintf(filename, 32, "fgfs-%s.png", time_str);
+
             SGPath p = screenshotDir / filename;
             if (!p.exists()) {
                 return p;