Commit 8aa6c92b authored by hybrid's avatar hybrid

Added a RealTimeDate structure, which can be queried for time and date in human readable form.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3395 dfc29bdd-3216-0410-991c-e03cc46cb475
parent b645fdb2
...@@ -25,6 +25,41 @@ public: ...@@ -25,6 +25,41 @@ public:
*/ */
virtual u32 getRealTime() const = 0; virtual u32 getRealTime() const = 0;
enum EWeekday
{
EWD_SUNDAY=0,
EWD_MONDAY,
EWD_TUESDAY,
EWD_WEDNESDAY,
EWD_THURSDAY,
EWD_FRIDAY,
EWD_SATURDAY
};
struct RealTimeDate
{
// Hour of the day, from 0 to 23
u32 Hour;
// Minute of the hour, from 0 to 59
u32 Minute;
// Second of the minute, due to extra seconds from 0 to 61
u32 Second;
// Year of the gregorian calender
s32 Year;
// Month of the year, from 1 to 12
u32 Month;
// Day of the month, from 1 to 31
u32 Day;
// Weekday for the current day
EWeekday Weekday;
// Day of the year, from 1 to 366
u32 Yearday;
// Whether daylight saving is on
bool IsDST;
};
virtual RealTimeDate getRealTimeAndDate() const = 0;
//! Returns current virtual time in milliseconds. //! Returns current virtual time in milliseconds.
/** This value starts with 0 and can be manipulated using setTime(), /** This value starts with 0 and can be manipulated using setTime(),
stopTimer(), startTimer(), etc. This value depends on the set speed of stopTimer(), startTimer(), etc. This value depends on the set speed of
......
...@@ -29,6 +29,12 @@ namespace irr ...@@ -29,6 +29,12 @@ namespace irr
return os::Timer::getRealTime(); return os::Timer::getRealTime();
} }
//! Get current time and date in calendar form
virtual RealTimeDate getRealTimeAndDate() const
{
return os::Timer::getRealTimeAndDate();
}
//! Returns current virtual time in milliseconds. //! Returns current virtual time in milliseconds.
/** This value starts with 0 and can be manipulated using setTime(), stopTimer(), /** This value starts with 0 and can be manipulated using setTime(), stopTimer(),
startTimer(), etc. This value depends on the set speed of the timer if the timer startTimer(), etc. This value depends on the set speed of the timer if the timer
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <stdlib.h> #include <stdlib.h>
#define bswap_16(X) _byteswap_ushort(X) #define bswap_16(X) _byteswap_ushort(X)
#define bswap_32(X) _byteswap_ulong(X) #define bswap_32(X) _byteswap_ulong(X)
#define localtime _localtime_s
#elif defined(_IRR_OSX_PLATFORM_) #elif defined(_IRR_OSX_PLATFORM_)
#include <libkern/OSByteOrder.h> #include <libkern/OSByteOrder.h>
#define bswap_16(X) OSReadSwapInt16(&X,0) #define bswap_16(X) OSReadSwapInt16(&X,0)
...@@ -55,6 +56,7 @@ namespace os ...@@ -55,6 +56,7 @@ namespace os
#else #else
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <time.h>
#endif #endif
namespace irr namespace irr
...@@ -153,7 +155,6 @@ namespace os ...@@ -153,7 +155,6 @@ namespace os
gettimeofday(&tv, 0); gettimeofday(&tv, 0);
return (u32)(tv.tv_sec * 1000) + (tv.tv_usec / 1000); return (u32)(tv.tv_sec * 1000) + (tv.tv_usec / 1000);
} }
} // end namespace os } // end namespace os
#endif // end linux / windows #endif // end linux / windows
...@@ -223,6 +224,28 @@ namespace os ...@@ -223,6 +224,28 @@ namespace os
u32 Timer::StartRealTime = 0; u32 Timer::StartRealTime = 0;
u32 Timer::StaticTime = 0; u32 Timer::StaticTime = 0;
//! Get real time and date in calendar form
ITimer::RealTimeDate Timer::getRealTimeAndDate()
{
time_t rawtime;
time(&rawtime);
struct tm * timeinfo;
timeinfo = localtime(&rawtime);
ITimer::RealTimeDate date;
date.Hour=(u32)timeinfo->tm_hour;
date.Minute=(u32)timeinfo->tm_min;
date.Second=(u32)timeinfo->tm_sec;
date.Day=(u32)timeinfo->tm_mday;
date.Month=(u32)timeinfo->tm_mon+1;
date.Year=(u32)timeinfo->tm_year+1900;
date.Weekday=(ITimer::EWeekday)timeinfo->tm_wday;
date.Yearday=(u32)timeinfo->tm_yday+1;
date.IsDST=timeinfo->tm_isdst != 0;
return date;
}
//! returns current virtual time //! returns current virtual time
u32 Timer::getTime() u32 Timer::getTime()
{ {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "irrString.h" #include "irrString.h"
#include "path.h" #include "path.h"
#include "ILogger.h" #include "ILogger.h"
#include "ITimer.h"
namespace irr namespace irr
{ {
...@@ -67,6 +68,9 @@ namespace os ...@@ -67,6 +68,9 @@ namespace os
//! returns the current time in milliseconds //! returns the current time in milliseconds
static u32 getTime(); static u32 getTime();
//! get current time and date in calendar form
static ITimer::RealTimeDate getRealTimeAndDate();
//! initializes the real timer //! initializes the real timer
static void initTimer(); static void initTimer();
......
#include "testUtils.h" #include "testUtils.h"
#include <irrlicht.h>
#include <assert.h>
using namespace irr; using namespace irr;
using namespace core; using namespace core;
// Test the functionality of the Irrlicht timer // Test the functionality of the Irrlicht timer
bool testTimer(void) bool testTimer(void)
{ {
...@@ -14,6 +11,9 @@ bool testTimer(void) ...@@ -14,6 +11,9 @@ bool testTimer(void)
IrrlichtDevice* device = createDevice(video::EDT_NULL); IrrlichtDevice* device = createDevice(video::EDT_NULL);
if (!device) if (!device)
return false; return false;
logTestString("Testing virtual timer.\n");
ITimer* timer = device->getTimer(); ITimer* timer = device->getTimer();
// must be running at start // must be running at start
...@@ -43,6 +43,21 @@ bool testTimer(void) ...@@ -43,6 +43,21 @@ bool testTimer(void)
timer->start(); timer->start();
success &= !timer->isStopped(); success &= !timer->isStopped();
logTestString("Testing virtual timer done. %s\n", success?"Success":"Failure");
logTestString("Testing real timer.\n");
const u32 startVirtual = timer->getTime();
const u32 startReal = timer->getRealTime();
device->sleep(2);
if (startReal != timer->getRealTime())
logTestString("Warning: Real timer did not progress. Maybe the time slices are too coarse to see.\n");
if (startVirtual != timer->getTime())
logTestString("Warning: Virtual timer did not progress. Maybe the time slices are too coarse to see.\n");
irr::ITimer::RealTimeDate date = timer->getRealTimeAndDate();
logTestString("Real time and date. %d.%d.%d at %d:%d:%d\n", date.Day, date.Month, date.Year, date.Hour, date.Minute, date.Second);
logTestString("This is day %d of the year and weekday %d. The current time zone has daylight saving %s\n", date.Yearday, date.Weekday, date.IsDST?"enabled":"disabled");
device->drop(); device->drop();
return success; return success;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment