From e7a13421df7d1ca500abec82ee03bdc801b7b5ab Mon Sep 17 00:00:00 2001 From: cebix <> Date: Fri, 6 Jul 2001 17:36:09 +0000 Subject: [PATCH] replaced TIME_OFFSET constant by portable TimeToMacTime() function --- BasiliskII/ChangeLog | 2 ++ BasiliskII/src/AmigaOS/sysdeps.h | 3 -- BasiliskII/src/AmigaOS/timer_amiga.cpp | 5 ++- BasiliskII/src/BeOS/sysdeps.h | 3 -- BasiliskII/src/BeOS/timer_beos.cpp | 8 ++--- BasiliskII/src/Unix/sysdeps.h | 3 -- BasiliskII/src/Unix/timer_unix.cpp | 11 ++---- BasiliskII/src/extfs.cpp | 16 ++++----- BasiliskII/src/include/macos_util.h | 3 +- BasiliskII/src/macos_util.cpp | 50 ++++++++++++++++++-------- 10 files changed, 54 insertions(+), 50 deletions(-) diff --git a/BasiliskII/ChangeLog b/BasiliskII/ChangeLog index 9fffecc0..21e9cfe1 100644 --- a/BasiliskII/ChangeLog +++ b/BasiliskII/ChangeLog @@ -5,6 +5,8 @@ V1.0 (snapshot) - like a 68882/68040 FPU, eliminating the need for 68060 FPU patches - added support for platform-independant mutexes, used by adb.cpp for thread-safe mouse handling + - the TIME_OFFSET constant has been replaced by a (portable) function + TimeToMacTime(); file dates in ExtFS should now be correct - Unix: windowed display mode supports different resolutions and color depth, which can be switched on-the-fly - Unix: Ctrl-F5 grabs mouse in windowed mode (enhanced compatibility diff --git a/BasiliskII/src/AmigaOS/sysdeps.h b/BasiliskII/src/AmigaOS/sysdeps.h index b5f8c43b..8c4e1683 100644 --- a/BasiliskII/src/AmigaOS/sysdeps.h +++ b/BasiliskII/src/AmigaOS/sysdeps.h @@ -61,9 +61,6 @@ typedef unsigned long long loff_t; // Time data type for Time Manager emulation typedef struct timeval tm_time_t; -// Offset Mac->AmigaOS time in seconds -#define TIME_OFFSET 0x8b31ef80 - // Endianess conversion (not needed) #define ntohs(x) (x) #define ntohl(x) (x) diff --git a/BasiliskII/src/AmigaOS/timer_amiga.cpp b/BasiliskII/src/AmigaOS/timer_amiga.cpp index 64394939..3d88b119 100644 --- a/BasiliskII/src/AmigaOS/timer_amiga.cpp +++ b/BasiliskII/src/AmigaOS/timer_amiga.cpp @@ -51,10 +51,9 @@ void Microseconds(uint32 &hi, uint32 &lo) uint32 TimerDateTime(void) { - ULONG secs; - ULONG mics; + ULONG secs, mics; CurrentTime(&secs, &mics); - return secs + TIME_OFFSET; + return secs + 0x8b31ef80; } diff --git a/BasiliskII/src/BeOS/sysdeps.h b/BasiliskII/src/BeOS/sysdeps.h index b7c0747d..be343273 100644 --- a/BasiliskII/src/BeOS/sysdeps.h +++ b/BasiliskII/src/BeOS/sysdeps.h @@ -55,9 +55,6 @@ // Time data type for Time Manager emulation typedef bigtime_t tm_time_t; -// Offset Mac->BeOS time in seconds -#define TIME_OFFSET 0x7c25b080 - // 64 bit file offsets typedef off_t loff_t; diff --git a/BasiliskII/src/BeOS/timer_beos.cpp b/BasiliskII/src/BeOS/timer_beos.cpp index 39d3cb34..234b30ef 100644 --- a/BasiliskII/src/BeOS/timer_beos.cpp +++ b/BasiliskII/src/BeOS/timer_beos.cpp @@ -21,6 +21,7 @@ #include #include "sysdeps.h" +#include "macos_util.h" #include "timer.h" #define DEBUG 0 @@ -46,12 +47,7 @@ void Microseconds(uint32 &hi, uint32 &lo) uint32 TimerDateTime(void) { - time_t utc_now = time(NULL); - long tz = timezone; - time_t local_now = utc_now - tz; - if (daylight) - local_now += 3600; - return (uint32)local_now + TIME_OFFSET; + return TimeToMacTime(time(NULL)); } diff --git a/BasiliskII/src/Unix/sysdeps.h b/BasiliskII/src/Unix/sysdeps.h index 9e93936b..38e3a495 100644 --- a/BasiliskII/src/Unix/sysdeps.h +++ b/BasiliskII/src/Unix/sysdeps.h @@ -155,9 +155,6 @@ typedef struct timespec tm_time_t; typedef struct timeval tm_time_t; #endif -/* Offset Mac->Unix time in seconds */ -#define TIME_OFFSET 0x7c25b080 - /* UAE CPU data types */ #define uae_s8 int8 #define uae_u8 uint8 diff --git a/BasiliskII/src/Unix/timer_unix.cpp b/BasiliskII/src/Unix/timer_unix.cpp index f422b5c3..bf3c06a8 100644 --- a/BasiliskII/src/Unix/timer_unix.cpp +++ b/BasiliskII/src/Unix/timer_unix.cpp @@ -19,6 +19,7 @@ */ #include "sysdeps.h" +#include "macos_util.h" #include "timer.h" #define DEBUG 0 @@ -57,15 +58,7 @@ void Microseconds(uint32 &hi, uint32 &lo) uint32 TimerDateTime(void) { - time_t utc_now = time(NULL); -#if defined(__linux__) || defined(__SVR4) - time_t local_now = utc_now - timezone; -#elif defined(__FreeBSD__) || defined(__NetBSD__) - time_t local_now = utc_now + localtime(&utc_now)->tm_gmtoff; -#else - time_t local_now = utc_now; -#endif - return (uint32)local_now + TIME_OFFSET; + return TimeToMacTime(time(NULL)); } diff --git a/BasiliskII/src/extfs.cpp b/BasiliskII/src/extfs.cpp index 5638a606..2e3a9a95 100644 --- a/BasiliskII/src/extfs.cpp +++ b/BasiliskII/src/extfs.cpp @@ -976,11 +976,11 @@ static int16 fs_volume_mount(uint32 pb) // Init VCB WriteMacInt16(vcb + vcbSigWord, 0x4244); #if defined(__BEOS__) || defined(WIN32) - WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET); + WriteMacInt32(vcb + vcbCrDate, TimeToMacTime(root_stat.st_crtime)); #else WriteMacInt32(vcb + vcbCrDate, 0); #endif - WriteMacInt32(vcb + vcbLsMod, root_stat.st_mtime + TIME_OFFSET); + WriteMacInt32(vcb + vcbLsMod, TimeToMacTime(root_stat.st_mtime)); WriteMacInt32(vcb + vcbVolBkUp, 0); WriteMacInt16(vcb + vcbNmFls, 1); //!! WriteMacInt16(vcb + vcbNmRtDirs, 1); //!! @@ -1039,11 +1039,11 @@ static int16 fs_get_vol_info(uint32 pb, bool hfs) if (ReadMacInt32(pb + ioNamePtr)) pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME); #if defined(__BEOS__) || defined(WIN32) - WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET); + WriteMacInt32(pb + ioVCrDate, TimeToMacTime(root_stat.st_crtime)); #else WriteMacInt32(pb + ioVCrDate, 0); #endif - WriteMacInt32(pb + ioVLsMod, root_stat.st_mtime + TIME_OFFSET); + WriteMacInt32(pb + ioVLsMod, TimeToMacTime(root_stat.st_mtime)); WriteMacInt16(pb + ioVAtrb, 0); WriteMacInt16(pb + ioVNmFls, 1); //!! WriteMacInt16(pb + ioVBitMap, 0); @@ -1231,11 +1231,11 @@ read_next_de: WriteMacInt32(pb + ioDirID, fs_item->id); #if defined(__BEOS__) || defined(WIN32) - WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); + WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); #else WriteMacInt32(pb + ioFlCrDat, 0); #endif - WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET); + WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(st.st_mtime)); get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false); @@ -1353,7 +1353,7 @@ read_next_de: WriteMacInt32(pb + ioDirID, fs_item->id); WriteMacInt32(pb + ioFlParID, fs_item->parent_id); #if defined(__BEOS__) || defined(WIN32) - WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); + WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); #else WriteMacInt32(pb + ioFlCrDat, 0); #endif @@ -1363,7 +1363,7 @@ read_next_de: fs_item->mtime = mtime; cached = false; } - WriteMacInt32(pb + ioFlMdDat, mtime + TIME_OFFSET); + WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(mtime)); WriteMacInt32(pb + ioFlBkDat, 0); get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode)); diff --git a/BasiliskII/src/include/macos_util.h b/BasiliskII/src/include/macos_util.h index f3aaf4f1..61eb5e52 100644 --- a/BasiliskII/src/include/macos_util.h +++ b/BasiliskII/src/include/macos_util.h @@ -263,7 +263,8 @@ extern void EnqueueMac(uint32 elem, uint32 list); // Enqueue QElem in list extern int FindFreeDriveNumber(int num); // Find first free drive number, starting at "num" extern void MountVolume(void *fh); // Mount volume with given file handle (see sys.h) extern void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_size); // Calculate disk image file layout given file size and first 256 data bytes -extern uint32 DebugUtil(uint32 Selector); // DebugUtil() Replacement +extern uint32 DebugUtil(uint32 Selector); // DebugUtil() Replacement +extern uint32 TimeToMacTime(time_t t); // Convert time_t value to MacOS time // Construct four-character-code #define FOURCC(a,b,c,d) (((uint32)(a) << 24) | ((uint32)(b) << 16) | ((uint32)(c) << 8) | (uint32)(d)) diff --git a/BasiliskII/src/macos_util.cpp b/BasiliskII/src/macos_util.cpp index da7088fa..d0159858 100644 --- a/BasiliskII/src/macos_util.cpp +++ b/BasiliskII/src/macos_util.cpp @@ -106,19 +106,41 @@ void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_s uint32 DebugUtil(uint32 Selector) { - switch (Selector) - { - case duDebuggerGetMax: - return 3; - case duDebuggerEnter: - return 0; - case duDebuggerExit: - return 0; - case duDebuggerPoll: - ADBInterrupt(); - return 0; - default: - return (uint32) paramErr; - } + switch (Selector) { + case duDebuggerGetMax: + return 3; + case duDebuggerEnter: + return 0; + case duDebuggerExit: + return 0; + case duDebuggerPoll: + ADBInterrupt(); + return 0; + default: + return (uint32) paramErr; + } } + +/* + * Convert time_t value to MacOS time (seconds since 1.1.1904) + */ + +uint32 TimeToMacTime(time_t t) +{ + // This code is taken from glibc 2.2 + + // Convert to number of seconds elapsed since 1-Jan-1904 + struct tm *local = localtime(&t); + const int TM_EPOCH_YEAR = 1900; + const int MAC_EPOCH_YEAR = 1904; + int a4 = ((local->tm_year + TM_EPOCH_YEAR) >> 2) - !(local->tm_year & 3); + int b4 = (MAC_EPOCH_YEAR >> 2) - !(MAC_EPOCH_YEAR & 3); + int a100 = a4 / 25 - (a4 % 25 < 0); + int b100 = b4 / 25 - (b4 % 25 < 0); + int a400 = a100 >> 2; + int b400 = b100 >> 2; + int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); + uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; + return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); +}