From 9e50599234d07056c791351d9b4ed40ada5fa23b Mon Sep 17 00:00:00 2001 From: aleos89 Date: Thu, 7 Nov 2013 14:26:14 -0500 Subject: [PATCH] Follow-up to 56ac0ee. Corrected compile errors. --- src/common/timer.c | 51 +++++++++++++++++++++++++++++---------------- src/common/timer.h | 5 +++-- src/map/atcommand.c | 21 ++++++++++++------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/common/timer.c b/src/common/timer.c index c064edc10d..8c3d3c37a6 100644 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -406,32 +406,47 @@ unsigned long get_uptime(void) return (unsigned long)difftime(time(NULL), start_time); } +void time2str(char *timestr, char *format, int timein) { + time_t timeout = time(NULL) + timein; + strftime(timestr, 24, format, localtime(&timeout)); +} + /* - * Split given time into year, month, day, hour, minute, second + * Split given timein 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); + const int factor_min = 60; + const int factor_hour = factor_min*60; + const int factor_day = factor_hour*24; + const int factor_month = factor_day*30; // Approx + const int factor_year = factor_month*12; // Even worse approx - *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); + *year = timein/factor_year; + timein -= *year*factor_year; + *month = timein/factor_month; + timein -= *month*factor_month; + *day = timein/factor_day; + timein -= *day*factor_day; + *hour = timein/factor_hour; + timein -= *hour*factor_hour; + *minute = timein/factor_min; + timein -= *minute*factor_min; + *second = timein; + + *year = max(0,*year); + *month = max(0,*month); + *day = max(0,*day); + *hour = max(0,*hour); + *minute = max(0,*minute); + *second = max(0,*second); } /* * Create a "timestamp" with the given argument */ -int solve_time(char * modif_p) { - int totaltime = 0, value = 0; +double solve_time(char* modif_p) { + double totaltime = 0; + int value = 0; struct tm then_tm; time_t now = time(NULL); time_t then = now; @@ -459,7 +474,7 @@ int solve_time(char * modif_p) { then_tm.tm_hour += value; modif_p++; } else if (modif_p[0] == 'd' || modif_p[0] == 'j') { - then_tm.tm_yday += value; + then_tm.tm_mday += value; modif_p++; } else if (modif_p[0] == 'm') { then_tm.tm_mon += value; diff --git a/src/common/timer.h b/src/common/timer.h index 10884aabe3..735d441afa 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -50,8 +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); +void time2str(char* timestr, char* format, int timein); +void split_time(int time, int* year, int* month, int* day, int* hour, int* minute, int* second); +double solve_time(char* modif_p); int do_timer(unsigned int tick); void timer_init(void); diff --git a/src/map/atcommand.c b/src/map/atcommand.c index a6b5b7e60c..d9f9519bda 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -4618,8 +4618,7 @@ ACMD_FUNC(unjail) ACMD_FUNC(jailfor) { struct map_session_data *pl_sd = NULL; - int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; - char * modif_p, output[CHAT_SIZE_MAX]; + char * modif_p; int jailtime = 0,x,y; short m_index = 0; nullpo_retr(-1, sd); @@ -4632,14 +4631,11 @@ ACMD_FUNC(jailfor) atcmd_output[sizeof(atcmd_output)-1] = '\0'; modif_p = atcmd_output; - jailtime = solve_time(modif_p)/60; // Change to minutes - - 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); + jailtime = (int)solve_time(modif_p)/60; // Change to minutes if (jailtime == 0) { clif_displaymessage(fd, msg_txt(sd,1136)); // Invalid time for jail command. + clif_displaymessage(fd, msg_txt(sd,702)); // Time parameter format is +/- to alter. y/a = Year, m = Month, d/j = Day, h = Hour, n/mn = Minute, s = Second. return -1; } @@ -4661,11 +4657,17 @@ ACMD_FUNC(jailfor) clif_displaymessage(pl_sd->fd, msg_txt(sd,120)); // GM has discharge you. clif_displaymessage(fd, msg_txt(sd,121)); // Player unjailed } else { - get_jail_time(jailtime,&year,&month,&day,&hour,&minute); + int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; + char timestr[CHAT_SIZE_MAX]; + split_time(jailtime*60,&year,&month,&day,&hour,&minute,&second); 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 clif_displaymessage(fd, atcmd_output); + time2str(timestr,"%Y-%m-%d %H:%M",jailtime*60); + sprintf(atcmd_output,"Release date is: %s",timestr); + clif_displaymessage(pl_sd->fd, atcmd_output); + clif_displaymessage(fd, atcmd_output); } } else if (jailtime < 0) { clif_displaymessage(fd, msg_txt(sd,1136)); @@ -4692,6 +4694,7 @@ ACMD_FUNC(jailfor) ACMD_FUNC(jailtime) { int year, month, day, hour, minute, second; + char timestr[CHAT_SIZE_MAX]; nullpo_retr(-1, sd); @@ -4714,6 +4717,8 @@ ACMD_FUNC(jailtime) 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); + time2str(timestr,"%Y-%m-%d %H:%M",sd->sc.data[SC_JAILED]->val1*60); + sprintf(atcmd_output,"Release date is: %s",timestr); return 0; }