Wich day/month, trough scripts?
by Very Interactive Person · in Torque Game Engine · 09/18/2004 (7:59 am) · 11 replies
Are there date functions available in scripts? I'd like to be able to know wich day of the month we are, and wich month. Preferably cross platform offcourse. And offcourse the date according to the operating system, I don't mind if its not the real date.
#2
What I'm after is the system time, and more important wich month and wich day of that month we are.
09/18/2004 (8:13 am)
This is something completely different. This is just a manager that keeps track of time as long as the game is running. But its not initialised by the system clock.What I'm after is the system time, and more important wich month and wich day of that month we are.
#3
09/18/2004 (8:15 am)
I could let it connect to a server and read the time from there.... but the reason I want to know wich day of the month we are is not to connect to a certain server every time the user launches the game, but only every 10 days for example. So connecting to a sever just to read the time kind of defeats the original purpose of the function i want.
#5
that's the only way i can think of to do it in script, its been done in tribes 2 for a time system before the devs implemented formatstringtime console function, unfourtionately i dont have the script on hand.
09/18/2004 (9:57 pm)
You can try doing an absolute ban on a non-existant player and exporting the banlist. then read the file and there should be a number coorosponding to seconds after jan 1, 1969 that the player was banned. So you can read this number in, and if they ban say lasted 1 second, the number that is read is 1 second after the current time. You can fine tune this by modifying an offset, (dont remember if it was jan 1 actually) then simply doing division to get the minutes/hour/day/month/yearthat's the only way i can think of to do it in script, its been done in tribes 2 for a time system before the devs implemented formatstringtime console function, unfourtionately i dont have the script on hand.
#7
This is ANSI btw so should be as cross-platform as possible. Might want to test it a little to make sure I didn't screw up when writing it. It will only run well (due to ANSI version) until 2038. So better not be making Duke Nukem' Forever. You could write platform specific versions of each of the functions but I can't be bothered ;)
Create a new cc file, I called it date.cc and put it in core but it's probably more of a platform thing.
With contents:
09/19/2004 (2:46 am)
Threw this together in 10 min. I would be surprised if there isn't something like this already hidden in there somewhere. This is ANSI btw so should be as cross-platform as possible. Might want to test it a little to make sure I didn't screw up when writing it. It will only run well (due to ANSI version) until 2038. So better not be making Duke Nukem' Forever. You could write platform specific versions of each of the functions but I can't be bothered ;)
Create a new cc file, I called it date.cc and put it in core but it's probably more of a platform thing.
With contents:
#include <ctime>
#include <memory.h>
#include "console/console.h"
#include "platform/platformSemaphore.h"
/**
* Simple single semaphore used to guard the global static of CSTDLIB function localtime.
*
* @author Peter Andersson
*/
class DateSem
{
public:
inline
DateSem()
{
mSem = Semaphore::createSemaphore();
};
inline
~DateSem()
{
Semaphore::destroySemaphore(mSem);
};
inline void
get()
{
Semaphore::acquireSemaphore(mSem);
};
inline void
release()
{
Semaphore::releaseSemaphore(mSem);
}
private:
void *mSem;
}sem;
inline void
getLocalTime(tm <)
{
time_t ct;
time(&ct);
sem.get();
memcpy(<,localtime(&ct), sizeof(lt));
sem.release();
}
ConsoleFunctionGroupBegin(Date, "General date functions.");
/**
* Gets the current second in localtime.
*
* @author Peter Andersson
* @return Current second.
*/
ConsoleFunction(getSecond, S32, 0, 0, "Gets the current second in localtime")
{
tm lt;
getLocalTime(lt);
return lt.tm_sec;
}
/**
* Gets the current minute in localtime.
*
* @author Peter Andersson
* @return Current minute.
*/
ConsoleFunction(getMinute, S32, 0, 0, "Gets the current minute in localtime.")
{
tm lt;
getLocalTime(lt);
return lt.tm_min;
}
/**
* Gets the current hour in localtime.
*
* @author Peter Andersson
* @return Current hour.
*/
ConsoleFunction(getHour, S32, 0, 0, "Gets the current hour in localtime.")
{
tm lt;
getLocalTime(lt);
return lt.tm_hour;
}
/**
* Gets the current day of the week in localtime. 0 = Sun, 1 = Mon ...
*
* @author Peter Andersson
* @return Day of current week.
*/
ConsoleFunction(getDayOfWeek, S32, 0, 0, "Gets the current day of the week in localtime. 0 = Sun, 1 = Mon ...")
{
tm lt;
getLocalTime(lt);
return lt.tm_wday;
}
/**
* Gets the current day of the month in localtime. 1 = first day.
*
* @author Peter Andersson
* @return Day of current month.
*/
ConsoleFunction(getDayOfMonth, S32, 0, 0, "Gets the current day of the month in localtime. 1 = first day")
{
tm lt;
getLocalTime(lt);
return lt.tm_mday;
}
/**
* Gets the current day of the year in localtime.
*
* @author Peter Andersson
* @return Day of current year.
*/
ConsoleFunction(getDayOfYear, S32, 0, 0, "Gets the current day of the year in localtime.")
{
tm lt;
getLocalTime(lt);
return lt.tm_yday;
}
/**
* Gets the current month in localtime. 0 = Jan, 1 = Feb, ...
*
* @author Peter Andersson
* @return Current month.
*/
ConsoleFunction(getMonth, S32, 0, 0, "Gets the current month in localtime. 0 = Jan, 1 = Feb, ...")
{
tm lt;
getLocalTime(lt);
return lt.tm_mon;
}
/**
* Gets the current year in localtime.
*
* @author Peter Andersson
* @return Current year.
*/
ConsoleFunction(getYear, S32, 0, 0, "Gets the current year in localtime.")
{
tm lt;
getLocalTime(lt);
return lt.tm_year + 1900;
}
ConsoleFunctionGroupEnd(Date);
#8
09/19/2004 (3:41 am)
Remember that in a multiplayer game you could very well have people in different time zones or even different days. If you plan on using this code to determine something in game (AKA if its night or day) you will want the server to make those choices to avoid clients from being out a sync.
#9
So, does this work on Linux and Mac too? If so, can someone test that?
09/19/2004 (3:42 pm)
Hey thanks a lot !! Just tested it and it seems to work fine. So, does this work on Linux and Mac too? If so, can someone test that?
#10
07/20/2007 (3:44 am)
I added this to consolefunctions.cc to get the local (cmos) time:ConsoleFunctionGroupBegin(DateFunctions, "Some small datafunctions.");
ConsoleFunction(getLocalTime, const char *, 1, 1, "getLocalTime() - Return datetime of the cmos. YEAR MONTH DAY HOUR MIN SEC")
{
char *returnBuffer = Con::getReturnBuffer( 32 );
Platform::LocalTime lt;
Platform::getLocalTime(lt);
dSprintf(returnBuffer , sizeof(returnBuffer), "%04d %02d %02d %02d %02d %02d",
lt.year + 1900,
lt.month + 1,
lt.monthday,
lt.hour,
lt.min,
lt.sec);
return returnBuffer;
}
ConsoleFunctionGroupEnd( DateFunctions );
#11
Bye ;-)
11/16/2008 (2:47 am)
For TGEA 1.7.0 I use this modification :#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <ctime>
#include <string.h>
#include <memory.h>
#include "console/console.h"
#include "platform/platform.h"
//****************************************************************************//
ConsoleFunctionGroupBegin(Date, "General date functions.");
//****************************************************************************//
// return Current second.
//****************************************************************************//
ConsoleFunction(getSecond, S32, 0, 0, "Gets the current second in localtime")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.sec;
}
//****************************************************************************//
// return Current minute.
//****************************************************************************//
ConsoleFunction(getMinute, S32, 0, 0, "Gets the current minute in localtime.")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.min;
}
//****************************************************************************//
// return Current hour.
//****************************************************************************//
ConsoleFunction(getHour, S32, 0, 0, "Gets the current hour in localtime.")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.hour;
}
//****************************************************************************//
// return Day of current month.
//****************************************************************************//
ConsoleFunction(getDayOfMonth, S32, 0, 0, "Gets the current day of the month in localtime. 1 = first day")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.monthday;
}
//****************************************************************************//
// return Current month.
//****************************************************************************//
ConsoleFunction(getMonth, S32, 0, 0, "Gets the current month in localtime. 0 = Jan, 1 = Feb, ...")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.month+1;
}
//****************************************************************************//
// return Current year.
//****************************************************************************//
ConsoleFunction(getYear, S32, 0, 0, "Gets the current year in localtime.")
{
Platform::LocalTime lt;
Platform::getLocalTime(lt);
return lt.year + 1900;
}
ConsoleFunctionGroupEnd(Date);Bye ;-)
Torque Owner Erik Madison