Created split_time() and solve_time() to be reused as a default format for parsing. Credits: lighta

This commit is contained in:
aleos89 2013-11-07 12:59:09 -05:00
parent b0957816fe
commit 56ac0eeae6
3 changed files with 107 additions and 104 deletions

View File

@ -109,28 +109,28 @@ static __inline uint64 _rdtsc(){
} t;
asm volatile("rdtsc":"=a"(t.dw[0]), "=d"(t.dw[1]) );
return t.qw;
}
static void rdtsc_calibrate(){
uint64 t1, t2;
int32 i;
ShowStatus("Calibrating Timer Source, please wait... ");
RDTSC_CLOCK = 0;
for(i = 0; i < 5; i++){
t1 = _rdtsc();
usleep(1000000); //1000 MS
t2 = _rdtsc();
RDTSC_CLOCK += (t2 - t1) / 1000;
RDTSC_CLOCK += (t2 - t1) / 1000;
}
RDTSC_CLOCK /= 5;
RDTSC_BEGINTICK = _rdtsc();
ShowMessage(" done. (Frequency: %u Mhz)\n", (uint32)(RDTSC_CLOCK/1000) );
}
@ -243,7 +243,7 @@ static int acquire_timer(void)
int add_timer(unsigned int tick, TimerFunc func, int id, intptr_t data)
{
int tid;
tid = acquire_timer();
timer_data[tid].tick = tick;
timer_data[tid].func = func;
@ -267,7 +267,7 @@ int add_timer_interval(unsigned int tick, TimerFunc func, int id, intptr_t data,
ShowError("add_timer_interval: invalid interval (tick=%u %p[%s] id=%d data=%d diff_tick=%d)\n", tick, func, search_timer_func_list(func), id, data, DIFF_TICK(tick, gettick()));
return INVALID_TIMER;
}
tid = acquire_timer();
timer_data[tid].tick = tick;
timer_data[tid].func = func;
@ -320,7 +320,7 @@ int addtick_timer(int tid, unsigned int tick)
int settick_timer(int tid, unsigned int tick)
{
size_t i;
// search timer position
ARR_FIND(0, BHEAP_LENGTH(timer_heap), i, BHEAP_DATA(timer_heap)[i] == tid);
if( i == BHEAP_LENGTH(timer_heap) )
@ -406,6 +406,78 @@ unsigned long get_uptime(void)
return (unsigned long)difftime(time(NULL), start_time);
}
/*
* Split given time into year, month, day, hour, minute, second
*/
void split_time(int timein, int* year, int* month, int* day, int* hour, int* minute, int *second) {
struct tm now_tm;
struct tm then_tm;
time_t now = time(NULL);
time_t then = now + timein; // Add time in seconds to the current time
now_tm = *localtime(&now);
then_tm = *localtime(&then);
mktime(&now_tm);
mktime(&then_tm);
*year = max(then_tm.tm_year - now_tm.tm_year,0);
*month = max(then_tm.tm_mon - now_tm.tm_mon,0);
*day = max(then_tm.tm_mday - now_tm.tm_mday,0);
*hour = max(then_tm.tm_hour - now_tm.tm_hour,0);
*minute = max(then_tm.tm_min - now_tm.tm_min,0);
*second = max(then_tm.tm_sec - now_tm.tm_sec,0);
}
/*
* Create a "timestamp" with the given argument
*/
int solve_time(char * modif_p) {
int totaltime = 0, value = 0;
struct tm then_tm;
time_t now = time(NULL);
time_t then = now;
then_tm = *localtime(&then);
while (modif_p[0] != '\0') {
value = atoi(modif_p);
if (value == 0)
modif_p++;
else {
if (modif_p[0] == '-' || modif_p[0] == '+')
modif_p++;
while (modif_p[0] >= '0' && modif_p[0] <= '9')
modif_p++;
if (modif_p[0] == 's') {
then_tm.tm_sec += value;
modif_p++;
} else if (modif_p[0] == 'n') {
then_tm.tm_min += value;
modif_p++;
} else if (modif_p[0] == 'm' && modif_p[1] == 'n') {
then_tm.tm_min += value;
modif_p = modif_p + 2;
} else if (modif_p[0] == 'h') {
then_tm.tm_hour += value;
modif_p++;
} else if (modif_p[0] == 'd' || modif_p[0] == 'j') {
then_tm.tm_yday += value;
modif_p++;
} else if (modif_p[0] == 'm') {
then_tm.tm_mon += value;
modif_p++;
} else if (modif_p[0] == 'y' || modif_p[0] == 'a') {
then_tm.tm_year += value;
modif_p++;
} else if (modif_p[0] != '\0') {
modif_p++;
}
}
}
then = mktime(&then_tm);
totaltime = difftime(then,now);
return totaltime;
}
void timer_init(void)
{
#if defined(ENABLE_RDTSC)

View File

@ -50,6 +50,9 @@ int add_timer_func_list(TimerFunc func, char* name);
unsigned long get_uptime(void);
void split_time(int time, int* year, int* month, int* day, int* hour, int* minute, int *second);
int solve_time(char * modif_p);
int do_timer(unsigned int tick);
void timer_init(void);
void timer_final(void);

View File

@ -3741,7 +3741,6 @@ ACMD_FUNC(reload) {
clif_displaymessage(fd, msg_txt(sd,516)); // Instance database has been reloaded.
}
return 0;
}
/*==========================================
@ -4525,34 +4524,6 @@ ACMD_FUNC(servertime)
return 0;
}
//Added by Coltaro
//We're using this function here instead of using time_t so that it only counts player's jail time when he/she's online (and since the idea is to reduce the amount of minutes one by one in status_change_timer...).
//Well, using time_t could still work but for some reason that looks like more coding x_x
static void get_jail_time(int jailtime, int* year, int* month, int* day, int* hour, int* minute)
{
const int factor_year = 518400; //12*30*24*60 = 518400
const int factor_month = 43200; //30*24*60 = 43200
const int factor_day = 1440; //24*60 = 1440
const int factor_hour = 60;
*year = jailtime/factor_year;
jailtime -= *year*factor_year;
*month = jailtime/factor_month;
jailtime -= *month*factor_month;
*day = jailtime/factor_day;
jailtime -= *day*factor_day;
*hour = jailtime/factor_hour;
jailtime -= *hour*factor_hour;
*minute = jailtime;
*year = *year > 0? *year : 0;
*month = *month > 0? *month : 0;
*day = *day > 0? *day : 0;
*hour = *hour > 0? *hour : 0;
*minute = *minute > 0? *minute : 0;
return;
}
/*==========================================
* @jail <char_name> by [Yor]
* Special warp! No check with nowarp and nowarpto flag
@ -4576,14 +4547,12 @@ ACMD_FUNC(jail)
return -1;
}
if (pc_get_group_level(sd) < pc_get_group_level(pl_sd))
{ // you can jail only lower or same GM
if (pc_get_group_level(sd) < pc_get_group_level(pl_sd)) { // you can jail only lower or same GM
clif_displaymessage(fd, msg_txt(sd,81)); // Your GM level don't authorise you to do this action on this player.
return -1;
}
if (pl_sd->sc.data[SC_JAILED])
{
if (pl_sd->sc.data[SC_JAILED]) {
clif_displaymessage(fd, msg_txt(sd,118)); // Player warped in jails.
return -1;
}
@ -4634,8 +4603,7 @@ ACMD_FUNC(unjail)
return -1;
}
if (!pl_sd->sc.data[SC_JAILED])
{
if (!pl_sd->sc.data[SC_JAILED]) {
clif_displaymessage(fd, msg_txt(sd,119)); // This player is not in jails.
return -1;
}
@ -4650,8 +4618,8 @@ ACMD_FUNC(unjail)
ACMD_FUNC(jailfor)
{
struct map_session_data *pl_sd = NULL;
int year, month, day, hour, minute, value;
char * modif_p;
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
char * modif_p, output[CHAT_SIZE_MAX];
int jailtime = 0,x,y;
short m_index = 0;
nullpo_retr(-1, sd);
@ -4664,41 +4632,13 @@ ACMD_FUNC(jailfor)
atcmd_output[sizeof(atcmd_output)-1] = '\0';
modif_p = atcmd_output;
year = month = day = hour = minute = 0;
while (modif_p[0] != '\0') {
value = atoi(modif_p);
if (value == 0)
modif_p++;
else {
if (modif_p[0] == '-' || modif_p[0] == '+')
modif_p++;
while (modif_p[0] >= '0' && modif_p[0] <= '9')
modif_p++;
if (modif_p[0] == 'n') {
minute = value;
modif_p++;
} else if (modif_p[0] == 'm' && modif_p[1] == 'n') {
minute = value;
modif_p = modif_p + 2;
} else if (modif_p[0] == 'h') {
hour = value;
modif_p++;
} else if (modif_p[0] == 'd' || modif_p[0] == 'j') {
day = value;
modif_p++;
} else if (modif_p[0] == 'm') {
month = value;
modif_p++;
} else if (modif_p[0] == 'y' || modif_p[0] == 'a') {
year = value;
modif_p++;
} else if (modif_p[0] != '\0') {
modif_p++;
}
}
}
jailtime = solve_time(modif_p)/60; // Change to minutes
if (year == 0 && month == 0 && day == 0 && hour == 0 && minute == 0) {
split_time(jailtime*60,&year,&month,&day,&hour,&minute,&second);
sprintf(output,msg_txt(sd,402),msg_txt(sd,1137),year,month,day,hour,minute); // %s in jail for %d years, %d months, %d days, %d hours and %d minutes
clif_displaymessage(fd, output);
if (jailtime == 0) {
clif_displaymessage(fd, msg_txt(sd,1136)); // Invalid time for jail command.
return -1;
}
@ -4713,17 +4653,8 @@ ACMD_FUNC(jailfor)
return -1;
}
jailtime = year*12*30*24*60 + month*30*24*60 + day*24*60 + hour*60 + minute; //In minutes
if(jailtime==0) {
clif_displaymessage(fd, msg_txt(sd,1136)); // Invalid time for jail command.
return -1;
}
//Added by Coltaro
if(pl_sd->sc.data[SC_JAILED] &&
pl_sd->sc.data[SC_JAILED]->val1 != INT_MAX)
{ //Update the player's jail time
// Added by Coltaro
if(pl_sd->sc.data[SC_JAILED] && pl_sd->sc.data[SC_JAILED]->val1 != INT_MAX) { // Update the player's jail time
jailtime += pl_sd->sc.data[SC_JAILED]->val1;
if (jailtime <= 0) {
jailtime = 0;
@ -4731,9 +4662,9 @@ ACMD_FUNC(jailfor)
clif_displaymessage(fd, msg_txt(sd,121)); // Player unjailed
} else {
get_jail_time(jailtime,&year,&month,&day,&hour,&minute);
sprintf(atcmd_output,msg_txt(sd,402),msg_txt(sd,1137),year,month,day,hour,minute); //%s in jail for %d years, %d months, %d days, %d hours and %d minutes
sprintf(atcmd_output,msg_txt(sd,402),msg_txt(sd,1137),year,month,day,hour,minute); // %s in jail for %d years, %d months, %d days, %d hours and %d minutes
clif_displaymessage(pl_sd->fd, atcmd_output);
sprintf(atcmd_output,msg_txt(sd,402),msg_txt(sd,1138),year,month,day,hour,minute); //This player is now in jail for %d years, %d months, %d days, %d hours and %d minutes
sprintf(atcmd_output,msg_txt(sd,402),msg_txt(sd,1138),year,month,day,hour,minute); // This player is now in jail for %d years, %d months, %d days, %d hours and %d minutes
clif_displaymessage(fd, atcmd_output);
}
} else if (jailtime < 0) {
@ -4741,14 +4672,13 @@ ACMD_FUNC(jailfor)
return -1;
}
//Jail locations, add more as you wish.
switch(rnd()%2)
{
case 1: //Jail #1
// Jail locations, add more as you wish.
switch(rnd()%2) {
case 1: // Jail #1
m_index = mapindex_name2id(MAP_JAIL);
x = 49; y = 75;
break;
default: //Default Jail
default: // Default Jail
m_index = mapindex_name2id(MAP_JAIL);
x = 24; y = 75;
break;
@ -4758,11 +4688,10 @@ ACMD_FUNC(jailfor)
return 0;
}
//By Coltaro
ACMD_FUNC(jailtime)
{
int year, month, day, hour, minute;
int year, month, day, hour, minute, second;
nullpo_retr(-1, sd);
@ -4781,10 +4710,9 @@ ACMD_FUNC(jailtime)
return -1;
}
//Get remaining jail time
get_jail_time(sd->sc.data[SC_JAILED]->val1,&year,&month,&day,&hour,&minute);
// Get remaining jail time
split_time(sd->sc.data[SC_JAILED]->val1*60,&year,&month,&day,&hour,&minute,&second);
sprintf(atcmd_output,msg_txt(sd,402),msg_txt(sd,1142),year,month,day,hour,minute); // You will remain in jail for %d years, %d months, %d days, %d hours and %d minutes
clif_displaymessage(fd, atcmd_output);
return 0;