Browse Source

Merge pull request #47 from cwalther/Plasma

---

The attached commit changes the fSecs field of plUnifiedTime from UInt32 to time_t. This fixes various date formatting problems, e.g. missing log timestamps or output of the Net.GetServerTime console command, when building on a system where time_t is 64-bit (e.g. Visual Studio 2010, and possibly also 2008, though I didn’t test that), and, as a bonus, extends the range past 2038 on such systems.

The wire protocol is left at 32 bit for now, we might change that to 64 when other reasons to break compatibility have accumulated.

Avoiding unnecessary back-and-forth type conversions makes the change ripple out into several other files. Changes have been tested as far as I easily could, and introduce no new compiler warnings.

The minimal-invasive way of fixing only the date formatting, should you prefer that, is in branch [cwalther:timet-minimal](https://github.com/cwalther/Plasma/branches/timet-minimal) (9b54fb05c9471c1b3317a5f8ed6cd4c11977e9e1).
Branan Purvine-Riley 13 years ago
parent
commit
8fd2ab129b
  1. 14
      Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp
  2. 6
      Sources/Plasma/FeatureLib/pfPython/cyMisc.h
  3. 6
      Sources/Plasma/FeatureLib/pfPython/cyMiscGlue.cpp
  4. 4
      Sources/Plasma/FeatureLib/pfPython/pyDniInfoSource.cpp
  5. 2
      Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp
  6. 2
      Sources/Plasma/PubUtilLib/plStatusLog/plStatusLog.cpp
  7. 8
      Sources/Plasma/PubUtilLib/plUnifiedTime/plTimeSpan.cpp
  8. 46
      Sources/Plasma/PubUtilLib/plUnifiedTime/plUnifiedTime.cpp
  9. 8
      Sources/Plasma/PubUtilLib/plUnifiedTime/plUnifiedTime.h

14
Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp

@ -602,7 +602,7 @@ UInt32 cyMisc::GetAgeTime( void )
UInt32 cyMisc::GetDniTime(void) time_t cyMisc::GetDniTime(void)
{ {
const plUnifiedTime utime = plNetClientMgr::GetInstance()->GetServerTime(); const plUnifiedTime utime = plNetClientMgr::GetInstance()->GetServerTime();
if ( utime.GetSecs() != 0) if ( utime.GetSecs() != 0)
@ -611,7 +611,7 @@ UInt32 cyMisc::GetDniTime(void)
return 0; return 0;
} }
UInt32 cyMisc::GetServerTime(void) time_t cyMisc::GetServerTime(void)
{ {
const plUnifiedTime utime = plNetClientMgr::GetInstance()->GetServerTime(); const plUnifiedTime utime = plNetClientMgr::GetInstance()->GetServerTime();
return utime.GetSecs(); return utime.GetSecs();
@ -626,10 +626,10 @@ float cyMisc::GetAgeTimeOfDayPercent(void)
#define kOneHour (UInt32)3600 #define kOneHour (UInt32)3600
#define kOneDay (UInt32)86400 #define kOneDay (UInt32)86400
UInt32 cyMisc::ConvertGMTtoDni(UInt32 gtime) time_t cyMisc::ConvertGMTtoDni(time_t gtime)
{ {
// convert to mountain time // convert to mountain time
UInt32 dtime = gtime - kMST; time_t dtime = gtime - kMST;
plUnifiedTime utime = plUnifiedTime(); plUnifiedTime utime = plUnifiedTime();
utime.SetSecs(dtime); utime.SetSecs(dtime);
// check for daylight savings time in New Mexico and adjust // check for daylight savings time in New Mexico and adjust
@ -638,10 +638,10 @@ UInt32 cyMisc::ConvertGMTtoDni(UInt32 gtime)
plUnifiedTime dstStart = plUnifiedTime(); plUnifiedTime dstStart = plUnifiedTime();
dstStart.SetGMTime(utime.GetYear(),4,1,2,0,0); dstStart.SetGMTime(utime.GetYear(),4,1,2,0,0);
// find first Sunday after 4/1 (first sunday of April) // find first Sunday after 4/1 (first sunday of April)
UInt32 days_to_go = 7 - dstStart.GetDayOfWeek(); int days_to_go = 7 - dstStart.GetDayOfWeek();
if (days_to_go == 7) if (days_to_go == 7)
days_to_go = 0; days_to_go = 0;
UInt32 dstStartSecs = dstStart.GetSecs() + days_to_go * kOneDay; time_t dstStartSecs = dstStart.GetSecs() + days_to_go * kOneDay;
plUnifiedTime dstEnd = plUnifiedTime(); plUnifiedTime dstEnd = plUnifiedTime();
dstEnd.SetGMTime(utime.GetYear(),10,25,1,0,0); dstEnd.SetGMTime(utime.GetYear(),10,25,1,0,0);
@ -649,7 +649,7 @@ UInt32 cyMisc::ConvertGMTtoDni(UInt32 gtime)
days_to_go = 7 - dstEnd.GetDayOfWeek(); days_to_go = 7 - dstEnd.GetDayOfWeek();
if (days_to_go == 7) if (days_to_go == 7)
days_to_go = 0; days_to_go = 0;
UInt32 dstEndSecs = dstEnd.GetSecs() + days_to_go * kOneDay; time_t dstEndSecs = dstEnd.GetSecs() + days_to_go * kOneDay;
if ( dtime > dstStartSecs && dtime < dstEndSecs ) if ( dtime > dstStartSecs && dtime < dstEndSecs )
// add hour for daylight savings time // add hour for daylight savings time

6
Sources/Plasma/FeatureLib/pfPython/cyMisc.h

@ -274,9 +274,9 @@ public:
static PyObject* GetPrevAgeInfo(); static PyObject* GetPrevAgeInfo();
// current time in current age // current time in current age
static UInt32 GetAgeTime( void ); static UInt32 GetAgeTime( void );
static UInt32 GetDniTime(void); static time_t GetDniTime(void);
static UInt32 ConvertGMTtoDni(UInt32 time); static time_t ConvertGMTtoDni(time_t time);
static UInt32 GetServerTime( void ); // returns the current server time in GMT static time_t GetServerTime( void ); // returns the current server time in GMT
static float GetAgeTimeOfDayPercent(void); static float GetAgeTimeOfDayPercent(void);
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////

6
Sources/Plasma/FeatureLib/pfPython/cyMiscGlue.cpp

@ -59,12 +59,12 @@ PYTHON_GLOBAL_METHOD_DEFINITION_NOARGS(PtGetPrevAgeInfo, "Returns ptAgeInfoStruc
PYTHON_GLOBAL_METHOD_DEFINITION_NOARGS(PtGetDniTime, "Returns current D'Ni time") PYTHON_GLOBAL_METHOD_DEFINITION_NOARGS(PtGetDniTime, "Returns current D'Ni time")
{ {
return PyLong_FromUnsignedLong(cyMisc::GetDniTime()); return PyLong_FromUnsignedLong((unsigned long)cyMisc::GetDniTime());
} }
PYTHON_GLOBAL_METHOD_DEFINITION_NOARGS(PtGetServerTime, "Returns the current time on the server (which is GMT)") PYTHON_GLOBAL_METHOD_DEFINITION_NOARGS(PtGetServerTime, "Returns the current time on the server (which is GMT)")
{ {
return PyLong_FromUnsignedLong(cyMisc::GetServerTime()); return PyLong_FromUnsignedLong((unsigned long)cyMisc::GetServerTime());
} }
PYTHON_GLOBAL_METHOD_DEFINITION(PtGMTtoDniTime, args, "Params: gtime\nConverts GMT time (passed in) to D'Ni time") PYTHON_GLOBAL_METHOD_DEFINITION(PtGMTtoDniTime, args, "Params: gtime\nConverts GMT time (passed in) to D'Ni time")
@ -75,7 +75,7 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtGMTtoDniTime, args, "Params: gtime\nConverts G
PyErr_SetString(PyExc_TypeError, "PtGMTtoDniTime expects a long"); PyErr_SetString(PyExc_TypeError, "PtGMTtoDniTime expects a long");
PYTHON_RETURN_ERROR; PYTHON_RETURN_ERROR;
} }
return PyLong_FromUnsignedLong(cyMisc::ConvertGMTtoDni(gtime)); return PyLong_FromUnsignedLong((unsigned long)cyMisc::ConvertGMTtoDni(gtime));
} }
PYTHON_GLOBAL_METHOD_DEFINITION(PtGetClientName, args, "Params: avatarKey=None\nThis will return the name of the client that is owned by the avatar\n" PYTHON_GLOBAL_METHOD_DEFINITION(PtGetClientName, args, "Params: avatarKey=None\nThis will return the name of the client that is owned by the avatar\n"

4
Sources/Plasma/FeatureLib/pfPython/pyDniInfoSource.cpp

@ -55,10 +55,10 @@ UInt32 pyDniInfoSource::GetAgeTime( void ) const
if (!node) if (!node)
return 0; return 0;
unsigned result; UInt32 result;
VaultAgeInfoNode ageInfo(node); VaultAgeInfoNode ageInfo(node);
if (const plUnifiedTime * utime = ageInfo.GetAgeTime()) if (const plUnifiedTime * utime = ageInfo.GetAgeTime())
result = utime->GetSecs(); result = (UInt32)utime->GetSecs();
else else
result = 0; result = 0;
node->DecRef(); node->DecRef();

2
Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp

@ -338,7 +338,7 @@ void plAgeDescription::Write(hsStream* stream) const
char buf[256]; char buf[256];
// Write the date/time // Write the date/time
sprintf(buf, "StartDateTime=%010u\n", fStart.GetSecs()); sprintf(buf, "StartDateTime=%010lu\n", (unsigned long)fStart.GetSecs());
stream->WriteString(buf); stream->WriteString(buf);
// Write the day length // Write the day length

2
Sources/Plasma/PubUtilLib/plStatusLog/plStatusLog.cpp

@ -786,7 +786,7 @@ bool plStatusLog::IPrintLineToFile( const char *line, UInt32 count )
} }
if ( fFlags & kTimeInSeconds ) if ( fFlags & kTimeInSeconds )
{ {
StrPrintf(work, arrsize(work), "(%u) ", plUnifiedTime(kNow).GetSecs()); StrPrintf(work, arrsize(work), "(%lu) ", (unsigned long)plUnifiedTime(kNow).GetSecs());
StrPack(buf, work, arrsize(buf)); StrPack(buf, work, arrsize(buf));
} }
if ( fFlags & kTimeAsDouble ) if ( fFlags & kTimeAsDouble )

8
Sources/Plasma/PubUtilLib/plUnifiedTime/plTimeSpan.cpp

@ -28,22 +28,22 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
long plTimeSpan::GetTotalDays() const long plTimeSpan::GetTotalDays() const
{ {
return fSecs / (24*3600L); return (long)(fSecs / (24*3600L));
} }
long plTimeSpan::GetTotalHours() const long plTimeSpan::GetTotalHours() const
{ {
return fSecs / 3600; return (long)(fSecs / 3600);
} }
long plTimeSpan::GetTotalMinutes() const long plTimeSpan::GetTotalMinutes() const
{ {
return fSecs / 60; return (long)(fSecs / 60);
} }
long plTimeSpan::GetTotalSeconds() const long plTimeSpan::GetTotalSeconds() const
{ {
return fSecs; return (long)fSecs;
} }

46
Sources/Plasma/PubUtilLib/plUnifiedTime/plUnifiedTime.cpp

@ -59,7 +59,7 @@ hsBool plUnifiedTime::SetFromWinFileTime(const FILETIME ft)
if (ffsecs >= MAGICWINDOWSOFFSET) // make sure we won't end up negatice if (ffsecs >= MAGICWINDOWSOFFSET) // make sure we won't end up negatice
{ {
fSecs = (UInt32)(ffsecs-MAGICWINDOWSOFFSET); fSecs = (time_t)(ffsecs-MAGICWINDOWSOFFSET);
fMicros = (UInt32)(ff % 10000000)/10; fMicros = (UInt32)(ff % 10000000)/10;
return true; return true;
} }
@ -200,14 +200,14 @@ const plUnifiedTime & plUnifiedTime::operator=(const plUnifiedTime * src)
const plUnifiedTime & plUnifiedTime::operator=(time_t src) const plUnifiedTime & plUnifiedTime::operator=(time_t src)
{ {
fSecs = (UInt32)src; fSecs = src;
fMicros = 0; fMicros = 0;
return *this; return *this;
} }
const plUnifiedTime & plUnifiedTime::operator=(unsigned long src) const plUnifiedTime & plUnifiedTime::operator=(unsigned long src)
{ {
fSecs = src; fSecs = (time_t)src;
fMicros = 0; fMicros = 0;
return *this; return *this;
} }
@ -222,7 +222,7 @@ const plUnifiedTime & plUnifiedTime::operator=(const struct timeval & src)
const plUnifiedTime & plUnifiedTime::operator=(const struct tm & src) const plUnifiedTime & plUnifiedTime::operator=(const struct tm & src)
{ {
struct tm atm = src; struct tm atm = src;
fSecs = (UInt32)mktime(&atm); // this won't work after 2030 something, sorry fSecs = mktime(&atm);
return *this; return *this;
} }
@ -231,16 +231,10 @@ void plUnifiedTime::SetSecsDouble(double secs)
hsAssert(secs>=0, "plUnifiedTime::SetSecsDouble negative time"); hsAssert(secs>=0, "plUnifiedTime::SetSecsDouble negative time");
double x,y; double x,y;
x = modf(secs,&y); x = modf(secs,&y);
fSecs = (UInt32)y; fSecs = (time_t)y;
fMicros = (UInt32)(x*1000000); fMicros = (UInt32)(x*1000000);
} }
void plUnifiedTime::FromMillis(UInt32 millis)
{
fSecs = millis/1000;
fMicros = 0;
}
void plUnifiedTime::ToCurrentTime() void plUnifiedTime::ToCurrentTime()
{ {
@ -268,7 +262,7 @@ hsBool plUnifiedTime::SetTime(short year, short month, short day, short hour, sh
atm.tm_mon = month - 1; atm.tm_mon = month - 1;
atm.tm_year = year - 1900; atm.tm_year = year - 1900;
atm.tm_isdst = dst; atm.tm_isdst = dst;
fSecs = (UInt32)mktime(&atm); // this won't work after 2030 something, sorry fSecs = mktime(&atm);
if (fSecs == -1) if (fSecs == -1)
return false; return false;
if (fMicros >= 1000000) if (fMicros >= 1000000)
@ -280,7 +274,7 @@ hsBool plUnifiedTime::SetTime(short year, short month, short day, short hour, sh
hsBool plUnifiedTime::GetTime(short &year, short &month, short &day, short &hour, short &minute, short &second) const hsBool plUnifiedTime::GetTime(short &year, short &month, short &day, short &hour, short &minute, short &second) const
{ {
struct tm* time = IGetTime((const time_t *)&fSecs); struct tm* time = IGetTime(&fSecs);
if (!time) if (!time)
return false; return false;
year = time->tm_year+1900; year = time->tm_year+1900;
@ -308,8 +302,8 @@ const char* plUnifiedTime::Print() const
const char* plUnifiedTime::PrintWMillis() const const char* plUnifiedTime::PrintWMillis() const
{ {
static std::string s; static std::string s;
xtl::format(s,"%s,s:%d,ms:%d", xtl::format(s,"%s,s:%lu,ms:%d",
Print(), GetSecs(), GetMillis() ); Print(), (unsigned long)GetSecs(), GetMillis() );
return s.c_str(); return s.c_str();
} }
@ -317,11 +311,11 @@ struct tm * plUnifiedTime::GetTm(struct tm * ptm) const
{ {
if (ptm != nil) if (ptm != nil)
{ {
*ptm = *IGetTime((const time_t *)&fSecs); *ptm = *IGetTime(&fSecs);
return ptm; return ptm;
} }
else else
return IGetTime((const time_t *)&fSecs); return IGetTime(&fSecs);
} }
int plUnifiedTime::GetYear() const int plUnifiedTime::GetYear() const
@ -367,15 +361,12 @@ double plUnifiedTime::GetSecsDouble() const
} }
#pragma optimize( "", on ) // restore optimizations to their defaults #pragma optimize( "", on ) // restore optimizations to their defaults
UInt32 plUnifiedTime::AsMillis()
{
return GetSecs()*1000;
}
void plUnifiedTime::Read(hsStream* s) void plUnifiedTime::Read(hsStream* s)
{ {
s->LogSubStreamStart("UnifiedTime"); s->LogSubStreamStart("UnifiedTime");
s->LogReadSwap(&fSecs,"Seconds"); UInt32 secs;
s->LogReadSwap(&secs,"Seconds");
fSecs = (time_t)secs;
s->LogReadSwap(&fMicros,"MicroSeconds"); s->LogReadSwap(&fMicros,"MicroSeconds");
s->LogSubStreamEnd(); s->LogSubStreamEnd();
// preserve fMode // preserve fMode
@ -383,7 +374,7 @@ void plUnifiedTime::Read(hsStream* s)
void plUnifiedTime::Write(hsStream* s) const void plUnifiedTime::Write(hsStream* s) const
{ {
s->WriteSwap(fSecs); s->WriteSwap((UInt32)fSecs);
s->WriteSwap(fMicros); s->WriteSwap(fMicros);
// preserve fMode // preserve fMode
} }
@ -442,7 +433,12 @@ bool plUnifiedTime::operator>=(const plUnifiedTime & rhs) const
plUnifiedTime::operator timeval() const plUnifiedTime::operator timeval() const
{ {
#if HS_BUILD_FOR_WIN32
// tv_secs should be a time_t, but on Windows it is a long
struct timeval t = {(long)fSecs, (long)fMicros};
#else
struct timeval t = {fSecs, fMicros}; struct timeval t = {fSecs, fMicros};
#endif
return t; return t;
} }
@ -456,7 +452,7 @@ plUnifiedTime::operator struct tm() const
std::string plUnifiedTime::Format(const char * fmt) const std::string plUnifiedTime::Format(const char * fmt) const
{ {
char buf[128]; char buf[128];
struct tm * t = IGetTime((const time_t *)&fSecs); struct tm * t = IGetTime(&fSecs);
if (t == nil || if (t == nil ||
!strftime(buf, sizeof(buf), fmt, t)) !strftime(buf, sizeof(buf), fmt, t))
buf[0] = '\0'; buf[0] = '\0';

8
Sources/Plasma/PubUtilLib/plUnifiedTime/plUnifiedTime.h

@ -59,7 +59,7 @@ public:
}; };
protected: protected:
UInt32 fSecs; time_t fSecs;
UInt32 fMicros; UInt32 fMicros;
Mode fMode; Mode fMode;
@ -92,7 +92,7 @@ public:
const plUnifiedTime & operator=(const struct tm & src); const plUnifiedTime & operator=(const struct tm & src);
// getters // getters
UInt32 GetSecs() const { return fSecs; } time_t GetSecs() const { return fSecs; }
UInt32 GetMicros() const { return fMicros; } UInt32 GetMicros() const { return fMicros; }
double GetSecsDouble() const; // get the secs and micros as a double floating point value double GetSecsDouble() const; // get the secs and micros as a double floating point value
hsBool GetTime(short &year, short &month, short &day, short &hour, short &minute, short &second) const; hsBool GetTime(short &year, short &month, short &day, short &hour, short &minute, short &second) const;
@ -106,10 +106,9 @@ public:
int GetMillis() const; int GetMillis() const;
int GetDayOfWeek() const; int GetDayOfWeek() const;
int GetMode() const {return fMode;} // local or gmt. int GetMode() const {return fMode;} // local or gmt.
UInt32 AsMillis();
// setters // setters
void SetSecs(const UInt32 secs) { fSecs = secs; } void SetSecs(const time_t secs) { fSecs = secs; }
void SetSecsDouble(double secs); void SetSecsDouble(double secs);
void SetMicros(const UInt32 micros) { fMicros = micros; } void SetMicros(const UInt32 micros) { fMicros = micros; }
hsBool SetTime(short year, short month, short day, short hour, short minute, short second, unsigned long usec=0, int dst=-1); hsBool SetTime(short year, short month, short day, short hour, short minute, short second, unsigned long usec=0, int dst=-1);
@ -118,7 +117,6 @@ public:
void ToCurrentTime(); void ToCurrentTime();
void ToEpoch() { fSecs = 0; fMicros = 0;} void ToEpoch() { fSecs = 0; fMicros = 0;}
void SetMode(Mode mode) { fMode=mode;} void SetMode(Mode mode) { fMode=mode;}
void FromMillis(UInt32 millis);
#if HS_BUILD_FOR_WIN32 #if HS_BUILD_FOR_WIN32
hsBool SetFromWinFileTime(const FILETIME ft); hsBool SetFromWinFileTime(const FILETIME ft);
#endif #endif

Loading…
Cancel
Save