Browse Source

Fix `time.now`

gingerBill 5 years ago
parent
commit
c9d3b95b0d
2 changed files with 13 additions and 6 deletions
  1. 11 2
      core/time/time.odin
  2. 2 4
      core/time/time_windows.odin

+ 11 - 2
core/time/time.odin

@@ -127,8 +127,17 @@ clock :: proc(t: Time) -> (hour, min, sec: int) {
 
 
 
-ABSOLUTE_ZERO_YEAR :: -292277022399; // Day is chosen so that 2001-01-01 is Monday in the calculations
-UNIX_TO_ABSOLUTE :: (1969*365 + 1969/4 - 1969/100 + 1969/400 - ((ABSOLUTE_ZERO_YEAR - 1) * 365.2425)) * SECONDS_PER_DAY;
+ABSOLUTE_ZERO_YEAR :: i64(-292277022399); // Day is chosen so that 2001-01-01 is Monday in the calculations
+ABSOLUTE_TO_INTERNAL :: i64(-9223371966579724800); // i64((ABSOLUTE_ZERO_YEAR - 1) * 365.2425 * SECONDS_PER_DAY);
+INTERNAL_TO_ABSOLUTE :: i64(-ABSOLUTE_TO_INTERNAL);
+
+UNIX_TO_INTERNAL :: i64((1969*365 + 1969/4 - 1969/100 + 1969/400) * SECONDS_PER_DAY);
+INTERNAL_TO_UNIX :: i64(-UNIX_TO_INTERNAL);
+
+WALL_TO_INTERNAL :: i64((1884*365 + 1884/4 - 1884/100 + 1884/400) * SECONDS_PER_DAY);
+INTERNAL_TO_WALL :: i64(- WALL_TO_INTERNAL);
+
+UNIX_TO_ABSOLUTE :: i64(UNIX_TO_INTERNAL + INTERNAL_TO_ABSOLUTE);
 
 _is_leap_year :: proc(year: int) -> bool {
 	return year%4 == 0 && (year%100 != 0 || year%400 == 0);

+ 2 - 4
core/time/time_windows.odin

@@ -9,11 +9,9 @@ now :: proc() -> Time {
 
 	win32.get_system_time_as_file_time(&file_time);
 
-	quad := u64(file_time.lo) | u64(file_time.hi) << 32;
+	ft := i64(u64(file_time.lo) | u64(file_time.hi) << 32);
 
-	UNIX_TIME_START :: 0x019db1ded53e8000;
-
-	ns := (1e9/1e7)*(i64(quad) - UNIX_TIME_START);
+	ns := (ft - 0x019db1ded53e8000) * 100;
 	return Time{_nsec=ns};
 }