zaela-Server/common/eqtime.cpp

206 lines
4.9 KiB
C++
Raw Permalink Normal View History

2013-05-09 10:44:08 -04:00
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2002 EQEMu Development Team (http://eqemu.org)
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
2013-02-16 16:14:39 -08:00
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
2013-05-09 10:44:08 -04:00
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2013-02-16 16:14:39 -08:00
*/
#include <fstream>
#include "../common/global_define.h"
#include "../common/eqemu_logsys.h"
2013-02-16 16:14:39 -08:00
#include "../common/eqtime.h"
#include "../common/eq_packet_structs.h"
#include <memory.h>
#include <iostream>
/*#ifdef _CRTDBG_MAP_ALLOC
#undef new
#endif*/
/*#ifdef _CRTDBG_MAP_ALLOC
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif*/
#define EQT_VERSION 1000
//Constructor
//Input: Starting EQ Time, Starting Real Time.
EQTime::EQTime(TimeOfDay_Struct start_eq, time_t start_real)
{
eqTime.start_eqtime=start_eq;
eqTime.start_realtime=start_real;
timezone=0;
}
EQTime::EQTime()
{
timezone = 0;
2013-02-16 16:14:39 -08:00
memset(&eqTime, 0, sizeof(eqTime));
//Defaults for time
TimeOfDay_Struct start;
start.day = 1;
start.hour = 9;
start.minute = 0;
start.month = 1;
start.year = 3100;
2013-02-16 16:14:39 -08:00
//Set default time zone
timezone = 0;
2013-02-16 16:14:39 -08:00
//Start EQTimer
SetCurrentEQTimeOfDay(start, time(0));
2013-02-16 16:14:39 -08:00
}
EQTime::~EQTime()
{
}
//getEQTimeOfDay - Reads timeConvert and writes the result to eqTimeOfDay
//This function was written by the ShowEQ Project.
//Input: Current Time (as a time_t), a pointer to the TimeOfDay_Struct that will be written to.
//Output: 0=Error, 1=Sucess
int EQTime::GetCurrentEQTimeOfDay(time_t timeConvert, struct TimeOfDay_Struct *eqTimeOfDay)
2013-02-16 16:14:39 -08:00
{
2013-05-09 10:44:08 -04:00
/* check to see if we have a reference time to go by. */
if (eqTime.start_realtime == 0)
2013-05-09 10:44:08 -04:00
return 0;
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
unsigned long diff = timeConvert - eqTime.start_realtime;
/* There are 3 seconds per 1 EQ Minute */
diff /= 3;
/* Start off timezone offset */
int32 ntz = timezone;
/* The minutes range from 0 - 59 */
diff += eqTime.start_eqtime.minute + (ntz % 60);
2013-05-09 10:44:08 -04:00
eqTimeOfDay->minute = diff % 60;
diff /= 60;
ntz /= 60;
// The hours range from 1-24
// 1 = 1am
// 2 = 2am
// ...
// 23 = 11 pm
// 24 = 12 am
//
// Modify it so that it works from
// 0-23 for our calculations
diff += (eqTime.start_eqtime.hour - 1) + (ntz % 24);
eqTimeOfDay->hour = (diff % 24) + 1;
2013-05-09 10:44:08 -04:00
diff /= 24;
ntz /= 24;
// The days range from 1-28
// Modify it so that it works from
// 0-27 for our calculations
diff += (eqTime.start_eqtime.day - 1) + (ntz % 28);
eqTimeOfDay->day = (diff % 28) + 1;
2013-05-09 10:44:08 -04:00
diff /= 28;
ntz /= 28;
// The months range from 1-12
// Modify it so that it works from
// 0-11 for our calculations
diff += (eqTime.start_eqtime.month - 1) + (ntz % 12);
eqTimeOfDay->month = (diff % 12) + 1;
2013-05-09 10:44:08 -04:00
diff /= 12;
ntz /= 12;
eqTimeOfDay->year = eqTime.start_eqtime.year + diff + ntz;
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
return 1;
2013-02-16 16:14:39 -08:00
}
//setEQTimeOfDay
int EQTime::SetCurrentEQTimeOfDay(TimeOfDay_Struct start_eq, time_t start_real)
2013-02-16 16:14:39 -08:00
{
if (start_real == 0)
2013-02-16 16:14:39 -08:00
return 0;
eqTime.start_eqtime = start_eq;
eqTime.start_realtime = start_real;
2013-02-16 16:14:39 -08:00
return 1;
}
bool EQTime::IsTimeBefore(TimeOfDay_Struct *base, TimeOfDay_Struct *test) {
if (base->year > test->year)
2013-02-16 16:14:39 -08:00
return(true);
if (base->year < test->year)
2013-02-16 16:14:39 -08:00
return(false);
//same years
if (base->month > test->month)
2013-02-16 16:14:39 -08:00
return(true);
if (base->month < test->month)
2013-02-16 16:14:39 -08:00
return(false);
//same month
if (base->day > test->day)
2013-02-16 16:14:39 -08:00
return(true);
if (base->day < test->day)
2013-02-16 16:14:39 -08:00
return(false);
//same day
if (base->hour > test->hour)
2013-02-16 16:14:39 -08:00
return(true);
if (base->hour < test->hour)
2013-02-16 16:14:39 -08:00
return(false);
//same hour...
return(base->minute > test->minute);
}
void EQTime::AddMinutes(uint32 minutes, TimeOfDay_Struct *to) {
uint32 cur;
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
//minutes start at 0, everything else starts at 1
cur = to->minute;
cur += minutes;
if (cur < 60) {
2013-02-16 16:14:39 -08:00
to->minute = cur;
return;
}
to->minute = cur % 60;
//carry hours
cur /= 60;
cur += to->hour;
if (cur <= 24) {
2013-02-16 16:14:39 -08:00
to->hour = cur;
return;
}
to->hour = ((cur - 1) % 24) + 1;
2013-02-16 16:14:39 -08:00
//carry days
cur = (cur - 1) / 24;
2013-02-16 16:14:39 -08:00
cur += to->day;
if (cur <= 28) {
2013-02-16 16:14:39 -08:00
to->day = cur;
return;
}
to->day = ((cur - 1) % 28) + 1;
2013-02-16 16:14:39 -08:00
//carry months
cur = (cur - 1) / 28;
2013-02-16 16:14:39 -08:00
cur += to->month;
if (cur <= 12) {
2013-02-16 16:14:39 -08:00
to->month = cur;
return;
}
to->month = ((cur - 1) % 12) + 1;
2013-02-16 16:14:39 -08:00
//carry years
to->year += (cur - 1) / 12;
2013-02-16 16:14:39 -08:00
}
void EQTime::ToString(TimeOfDay_Struct *t, std::string &str) {
2013-02-16 16:14:39 -08:00
char buf[128];
snprintf(buf, 128, "%.2d/%.2d/%.4d %.2d:%.2d",
t->month, t->day, t->year, t->hour, t->minute);
buf[127] = '\0';
str = buf;
}