+void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms)
+{
+ int is_leap = 0, i;
+
+ auto Sint64 DivMod64(Sint64 N, Sint64 D, Sint64 *R);
+
+ Sint64 DivMod64(Sint64 N, Sint64 D, Sint64 *R)
+ {
+ int sign = (N < 0) != (D < 0);
+ if(N < 0) N = -N;
+ if(D < 0) D = -D;
+ if(sign)
+ return -DivMod64U(N, D, (Uint64*)R);
+ else
+ return DivMod64U(N, D, (Uint64*)R);
+ }
+
+ // Get time
+ // TODO: Leap-seconds?
+ {
+ Sint64 rem;
+ TS = DivMod64( TS, 1000, &rem );
+ *ms = rem;
+ TS = DivMod64( TS, 60, &rem );
+ *sec = rem;
+ TS = DivMod64( TS, 60, &rem );
+ *mins = rem;
+ TS = DivMod64( TS, 24, &rem );
+ *hrs = rem;
+ }
+
+ // Adjust to Y2K
+ TS -= UNIX_TO_2K/(3600*24);
+
+ // Year (400 yr blocks) - (400/4-3) leap years
+ *year = 400 * DivMod64( TS, 365*400 + (400/4-3), &TS );
+ if( TS < 366 ) // First year in 400 is a leap
+ is_leap = 1;
+ else
+ {
+ // 100 yr blocks - 100/4-1 leap years
+ *year += 100 * DivMod64( TS, 365*100 + (100/4-1), &TS );
+ if( TS < 366 ) // First year in 100 isn't a leap
+ is_leap = 0;
+ else
+ {
+ *year += 4 * DivMod64( TS, 365*4 + 1, &TS );
+ if( TS < 366 ) // First year in 4 is a leap
+ is_leap = 1;
+ else
+ {
+ *year += DivMod64( TS, 356, &TS );
+ }
+ }
+ }
+ *year += 2000;
+
+ ASSERT(TS >= 0);
+
+ *day = 0;
+ // Month (if after the first of march, which is 29 Feb in a leap year)
+ if( is_leap && TS > DAYS_BEFORE[2] ) {
+ TS -= 1; // Shifts 29 Feb to 28 Feb
+ *day = 1;
+ }
+ // Get what month it is
+ for( i = 0; i < 12; i ++ ) {
+ if( TS < DAYS_BEFORE[i] )
+ break;
+ }
+ *month = i - 1;
+ // Get day
+ TS -= DAYS_BEFORE[i-1];
+ *day += TS; // Plus offset from leap handling above
+}
+