summaryrefslogtreecommitdiff
path: root/lib/cmp_filetime.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/cmp_filetime.h')
-rw-r--r--lib/cmp_filetime.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/cmp_filetime.h b/lib/cmp_filetime.h
new file mode 100644
index 00000000..e8cd6f50
--- /dev/null
+++ b/lib/cmp_filetime.h
@@ -0,0 +1,65 @@
+#ifndef CMP_FILETIME_H_INCLUDED
+#define CMP_FILETIME_H_INCLUDED
+
+#include <wx/stopwatch.h>
+#include <zen/int64.h>
+
+namespace zen
+{
+//---------------------------------------------------------------------------------------------------------------
+inline
+bool sameFileTime(const Int64& a, const Int64& b, size_t tolerance)
+{
+ if (a < b)
+ return b <= a + static_cast<int>(tolerance);
+ else
+ return a <= b + static_cast<int>(tolerance);
+}
+//---------------------------------------------------------------------------------------------------------------
+
+//number of seconds since Jan 1st 1970 + 1 year (needn't be too precise)
+static const long oneYearFromNow = wxGetUTCTime() + 365 * 24 * 3600; //init at program startup -> avoid MT issues
+
+
+class CmpFileTime
+{
+public:
+ CmpFileTime(size_t tolerance) : tolerance_(tolerance) {}
+
+ enum Result
+ {
+ TIME_EQUAL,
+ TIME_LEFT_NEWER,
+ TIME_RIGHT_NEWER,
+ TIME_LEFT_INVALID,
+ TIME_RIGHT_INVALID
+ };
+
+ Result getResult(const Int64& lhs, const Int64& rhs) const
+ {
+ if (lhs == rhs)
+ return TIME_EQUAL;
+
+ //check for erroneous dates (but only if dates are not (EXACTLY) the same)
+ if (lhs < 0 || lhs > oneYearFromNow) //earlier than Jan 1st 1970 or more than one year in future
+ return TIME_LEFT_INVALID;
+
+ if (rhs < 0 || rhs > oneYearFromNow)
+ return TIME_RIGHT_INVALID;
+
+ if (sameFileTime(lhs, rhs, tolerance_)) //last write time may differ by up to 2 seconds (NTFS vs FAT32)
+ return TIME_EQUAL;
+
+ //regular time comparison
+ if (lhs < rhs)
+ return TIME_RIGHT_NEWER;
+ else
+ return TIME_LEFT_NEWER;
+ }
+
+private:
+ const size_t tolerance_;
+};
+}
+
+#endif // CMP_FILETIME_H_INCLUDED
bgstack15