Aggiunta la possibilita' di trattare le date in formato ANSI (YYYYMMDD).
git-svn-id: svn://10.65.10.50/trunk@45 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
22611ac7b5
commit
441344d49c
578
include/date.cpp
578
include/date.cpp
@ -1,276 +1,302 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define __DATE_CPP
|
#define __DATE_CPP
|
||||||
#include <date.h>
|
#include <date.h>
|
||||||
|
|
||||||
#include <extcdecl.h>
|
#include <extcdecl.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <utility.h>
|
#include <utility.h>
|
||||||
|
|
||||||
#define DAYBIAS 36525L
|
#define DAYBIAS 36525L
|
||||||
#define NULLDATE -99999L
|
#define NULLDATE -99999L
|
||||||
|
|
||||||
TDate nodate, botime, eotime(31,12,2050);
|
TDate nodate, botime, eotime(31,12,2050);
|
||||||
HIDDEN TDate __tmp_date;
|
HIDDEN TDate __tmp_date;
|
||||||
|
HIDDEN char __date_tmp_string[20];
|
||||||
TDate::TDate(const TDate &d) : _val(d._val) {}
|
|
||||||
|
TDate::TDate(const TDate &d) : _val(d._val) {}
|
||||||
|
|
||||||
TDate::TDate(long l) : _val(l)
|
|
||||||
|
TDate::TDate(long l) : _val(l)
|
||||||
{
|
|
||||||
if (_val == TODAY)
|
{
|
||||||
{
|
if (_val == TODAY)
|
||||||
char s[10];
|
{
|
||||||
cgetdata(&_val, s);
|
char s[10];
|
||||||
}
|
cgetdata(&_val, s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TDate::TDate(const char* s)
|
|
||||||
|
TDate::TDate(const char* s)
|
||||||
{
|
|
||||||
_val = NULLDATE;
|
{
|
||||||
if (!isdate(s)) return;
|
_val = NULLDATE;
|
||||||
|
if (!isdate(s)) return;
|
||||||
if (strlen(s) == 10)
|
|
||||||
{
|
if (strlen(s) == 10)
|
||||||
int day = atoi(s),
|
{
|
||||||
month = atoi(&s[3]),
|
int day = atoi(s), month = atoi(&s[3]), year = atoi(&s[6]);
|
||||||
year = atoi(&s[6]);
|
long off = 0L;
|
||||||
long off = 0L;
|
|
||||||
|
if (year > 1000)
|
||||||
if (year > 1000)
|
{
|
||||||
{
|
if (year < 1951) off -= DAYBIAS;
|
||||||
if (year < 1951) off -= DAYBIAS;
|
if (year < 1851) off -= DAYBIAS;
|
||||||
if (year < 1851) off -= DAYBIAS;
|
if (year < 1751) off -= DAYBIAS;
|
||||||
if (year < 1751) off -= DAYBIAS;
|
year %= 100; // modify
|
||||||
year %= 100; // modify
|
}
|
||||||
}
|
_val = makedata(day, month, year) + off;
|
||||||
_val = makedata(day, month, year) + off;
|
}
|
||||||
}
|
else
|
||||||
else _val = cpackdata((char*)s);
|
if (strchr(s, '-') == NULL)
|
||||||
}
|
{
|
||||||
|
strcpy(__date_tmp_string, s);
|
||||||
|
|
||||||
TDate::TDate(int day, int month, int year)
|
int day = atoi(__date_tmp_string + 6); __date_tmp_string[6] = '\0';
|
||||||
|
int month = atoi(__date_tmp_string + 4); __date_tmp_string[4] = '\0';
|
||||||
{
|
int year = atoi(__date_tmp_string);
|
||||||
if (day == TODAY)
|
long off = 0L;
|
||||||
{
|
|
||||||
char s[10];
|
if (year > 1000)
|
||||||
cgetdata(&_val, s);
|
{
|
||||||
}
|
if (year < 1951) off -= DAYBIAS;
|
||||||
else
|
if (year < 1851) off -= DAYBIAS;
|
||||||
if ((day == 0) || (month == 0) || (year == 0)) _val = NULLDATE;
|
if (year < 1751) off -= DAYBIAS;
|
||||||
else
|
year %= 100; // modify
|
||||||
{
|
}
|
||||||
long off = 0L;
|
_val = makedata(day, month, year) + off;
|
||||||
|
}
|
||||||
if (year > 1000)
|
else _val = cpackdata((char*)s);
|
||||||
{
|
}
|
||||||
if (year < 1951) off -= DAYBIAS;
|
|
||||||
if (year < 1851) off -= DAYBIAS;
|
|
||||||
if (year < 1751) off -= DAYBIAS;
|
TDate::TDate(int day, int month, int year)
|
||||||
year %= 100; // modify
|
|
||||||
}
|
{
|
||||||
_val = makedata(day, month, year) + off;
|
if (day == TODAY)
|
||||||
if (!ok()) _val = NULLDATE;
|
{
|
||||||
}
|
char s[10];
|
||||||
}
|
cgetdata(&_val, s);
|
||||||
|
}
|
||||||
|
else
|
||||||
TDate::operator const char*() const
|
if ((day == 0) || (month == 0) || (year == 0)) _val = NULLDATE;
|
||||||
{
|
else
|
||||||
return string(4);
|
{
|
||||||
}
|
long off = 0L;
|
||||||
|
|
||||||
|
if (year > 1000)
|
||||||
TDate& TDate::operator =(const char* s)
|
{
|
||||||
{
|
if (year < 1951) off -= DAYBIAS;
|
||||||
return *this = TDate(s);
|
if (year < 1851) off -= DAYBIAS;
|
||||||
}
|
if (year < 1751) off -= DAYBIAS;
|
||||||
|
year %= 100; // modify
|
||||||
|
}
|
||||||
void TDate::print_on(ostream& out) const
|
_val = makedata(day, month, year) + off;
|
||||||
{ out << string(); }
|
if (!ok()) _val = NULLDATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
void TDate::read_from(istream& in)
|
|
||||||
{
|
|
||||||
in >> __tmp_string;
|
TDate::operator const char*() const
|
||||||
if (isdate(__tmp_string)) _val = cpackdata(__tmp_string);
|
{
|
||||||
else _val = NULLDATE;
|
return string(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char* TDate::string(int yeardgts, char sep) const
|
TDate& TDate::operator =(const char* s)
|
||||||
|
{
|
||||||
{
|
return *this = TDate(s);
|
||||||
if (!ok()) return "";
|
}
|
||||||
long wv = _val;
|
|
||||||
// int cnt = int(wv / DAYBIAS - (wv < 0 ? 1 : 0));
|
|
||||||
int cnt = wv >= DAYBIAS ? 2000 : 1900;
|
void TDate::print_on(ostream& out) const
|
||||||
// int cnt = 0;
|
{
|
||||||
// bool over2000 = wv >= DAYBIAS;
|
out << string();
|
||||||
|
}
|
||||||
while (wv < 0)
|
|
||||||
{
|
|
||||||
cnt -= 100;
|
void TDate::read_from(istream& in)
|
||||||
wv += DAYBIAS;
|
{
|
||||||
}
|
char s[256];
|
||||||
ceditdata(wv, __tmp_string);
|
|
||||||
if (strcmp(__tmp_string, " - - ") == 0) return "";
|
in >> s;
|
||||||
if (sep != '-')
|
if (isdate(s)) _val = cpackdata(s);
|
||||||
for (char* s = __tmp_string; *s; s++)
|
else _val = NULLDATE;
|
||||||
if (*s == '-') *s = sep;
|
}
|
||||||
if (yeardgts > 2)
|
|
||||||
{
|
|
||||||
char s[8];
|
char* TDate::string(int yeardgts, char sep) const
|
||||||
// int year = ::year(wv) - cnt * 100;
|
|
||||||
// if (year > 2000 && !over2000) year -= 100;
|
{
|
||||||
// int year = 1900 + cnt * 100 + atoi(__tmp_string + 6);
|
if (!ok()) return "";
|
||||||
int year = cnt + atoi(__tmp_string + 6);
|
long wv = _val;
|
||||||
|
int cnt = wv >= DAYBIAS ? 2000 : 1900;
|
||||||
if (yeardgts == 3) sprintf(s, "%03d", year % 1000);
|
|
||||||
else sprintf(s, "%04d", year);
|
while (wv < 0)
|
||||||
__tmp_string[6] = '\0';
|
{
|
||||||
strcat(__tmp_string, s);
|
cnt -= 100;
|
||||||
}
|
wv += DAYBIAS;
|
||||||
return __tmp_string;
|
}
|
||||||
}
|
ceditdata(wv, __date_tmp_string);
|
||||||
|
if (strcmp(__date_tmp_string, " - - ") == 0) return "";
|
||||||
|
if (sep != '-')
|
||||||
int TDate::day() const
|
for (char* s = __date_tmp_string; *s; s++)
|
||||||
|
if (*s == '-') *s = sep;
|
||||||
{
|
if (yeardgts > 2)
|
||||||
return ::day(_val);
|
{
|
||||||
}
|
char s[8];
|
||||||
|
int year = cnt + atoi(__date_tmp_string + 6);
|
||||||
|
|
||||||
int TDate::month() const
|
if (yeardgts == 3) sprintf(s, "%03d", year % 1000);
|
||||||
|
else sprintf(s, "%04d", year);
|
||||||
{
|
__date_tmp_string[6] = '\0';
|
||||||
return ::month(_val);
|
strcat(__date_tmp_string, s);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
if (yeardgts == ANSI)
|
||||||
int TDate::year() const
|
{
|
||||||
|
char* s = __date_tmp_string; s[2] = '\0'; s[5] = '\0';
|
||||||
{
|
const int day = atoi(s);
|
||||||
return ::year(_val);
|
const int month = atoi(s + 3);
|
||||||
}
|
const int year = atoi(s + 6);
|
||||||
|
|
||||||
|
sprintf(__date_tmp_string, "%04d%02d%02d", year, month, day);
|
||||||
void TDate::addmonth(int nmonth)
|
}
|
||||||
|
return __date_tmp_string;
|
||||||
{
|
}
|
||||||
_val = ::addmonth(_val, nmonth);
|
|
||||||
}
|
|
||||||
|
int TDate::day() const
|
||||||
|
|
||||||
void TDate::addyear(int nyear)
|
{
|
||||||
|
return ::day(_val);
|
||||||
{
|
}
|
||||||
_val = ::addyear(_val, nyear);
|
|
||||||
}
|
|
||||||
|
int TDate::month() const
|
||||||
|
|
||||||
bool TDate::isdate(const char* s)
|
{
|
||||||
{
|
return ::month(_val);
|
||||||
if (!*s) return FALSE;
|
}
|
||||||
if (strlen(s) == 10)
|
|
||||||
{
|
|
||||||
int day = atoi(s),
|
int TDate::year() const
|
||||||
month = atoi(&s[3]),
|
|
||||||
year = atoi(&s[6]);
|
{
|
||||||
if (day < 1 || day > 31 ||
|
return ::year(_val);
|
||||||
month < 1 || month > 12 ||
|
}
|
||||||
year < 1700 || year > 2100)
|
|
||||||
return FALSE;
|
|
||||||
|
void TDate::addmonth(int nmonth)
|
||||||
char s1[16];
|
|
||||||
sprintf(s1, "%02d-%02d-%02d", day, month, year%100);
|
{
|
||||||
return (bool)cverdata(s1);
|
_val = ::addmonth(_val, nmonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (bool)cverdata((char*)s);
|
|
||||||
}
|
void TDate::addyear(int nyear)
|
||||||
|
|
||||||
|
{
|
||||||
bool TDate::ok() const
|
_val = ::addyear(_val, nyear);
|
||||||
{
|
}
|
||||||
return _val <= 2 * DAYBIAS && _val >= -2 * DAYBIAS;
|
|
||||||
}
|
|
||||||
|
bool TDate::isdate(const char* s)
|
||||||
|
{
|
||||||
TDate& operator +(const TDate& a, long nday)
|
if (!*s) return FALSE;
|
||||||
|
if (strlen(s) == 10)
|
||||||
{
|
{
|
||||||
__tmp_date = long(a) + nday;
|
int day = atoi(s),
|
||||||
return __tmp_date;
|
month = atoi(&s[3]),
|
||||||
}
|
year = atoi(&s[6]);
|
||||||
|
if (day < 1 || day > 31 ||
|
||||||
|
month < 1 || month > 12 ||
|
||||||
TDate& operator +(const long nday, const TDate& b)
|
year < 1700 || year > 2100)
|
||||||
|
return FALSE;
|
||||||
{
|
|
||||||
__tmp_date = long(b) + nday;
|
char s1[16];
|
||||||
return __tmp_date;
|
sprintf(s1, "%02d-%02d-%02d", day, month, year%100);
|
||||||
}
|
return (bool)cverdata(s1);
|
||||||
|
}
|
||||||
|
|
||||||
TDate& operator -(const TDate& a, long nday)
|
return (bool)cverdata((char*)s);
|
||||||
|
}
|
||||||
{
|
|
||||||
__tmp_date = long(a)- nday;
|
|
||||||
return __tmp_date;
|
bool TDate::ok() const
|
||||||
}
|
{
|
||||||
|
return _val <= 2 * DAYBIAS && _val >= -2 * DAYBIAS;
|
||||||
|
}
|
||||||
void swap(TDate& a, TDate& b)
|
|
||||||
|
|
||||||
{
|
TDate& operator +(const TDate& a, long nday)
|
||||||
__tmp_date = b;
|
|
||||||
b = a;
|
{
|
||||||
a = __tmp_date;
|
__tmp_date = long(a) + nday;
|
||||||
}
|
return __tmp_date;
|
||||||
|
}
|
||||||
|
|
||||||
const TDate& fnc_min(const TDate& a, const TDate& b)
|
|
||||||
|
TDate& operator +(const long nday, const TDate& b)
|
||||||
{
|
|
||||||
if (a < b) return a;
|
{
|
||||||
else return b;
|
__tmp_date = long(b) + nday;
|
||||||
}
|
return __tmp_date;
|
||||||
|
}
|
||||||
|
|
||||||
const TDate& fnc_max(const TDate& a, const TDate& b)
|
|
||||||
|
TDate& operator -(const TDate& a, long nday)
|
||||||
{
|
|
||||||
if (a > b) return a;
|
{
|
||||||
else return b;
|
__tmp_date = long(a)- nday;
|
||||||
}
|
return __tmp_date;
|
||||||
|
}
|
||||||
|
|
||||||
// Converte un numero da 1 a 12 nel corrispondente mese
|
|
||||||
const char* itom(byte m)
|
void swap(TDate& a, TDate& b)
|
||||||
{
|
|
||||||
const char* nomi[12] =
|
{
|
||||||
{
|
__tmp_date = b;
|
||||||
"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
|
b = a;
|
||||||
"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"
|
a = __tmp_date;
|
||||||
};
|
}
|
||||||
|
|
||||||
return nomi[(m-1) % 12];
|
|
||||||
}
|
const TDate& fnc_min(const TDate& a, const TDate& b)
|
||||||
|
|
||||||
const char* itow(byte d)
|
{
|
||||||
{
|
if (a < b) return a;
|
||||||
const char* nomi[7] =
|
else return b;
|
||||||
{ "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi",
|
}
|
||||||
"Sabato", "Domenica" };
|
|
||||||
return nomi[(d-1) % 7];
|
|
||||||
}
|
const TDate& fnc_max(const TDate& a, const TDate& b)
|
||||||
|
|
||||||
|
{
|
||||||
|
if (a > b) return a;
|
||||||
|
else return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Converte un numero da 1 a 12 nel corrispondente mese
|
||||||
|
const char* itom(byte m)
|
||||||
|
{
|
||||||
|
const char* nomi[12] =
|
||||||
|
{
|
||||||
|
"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
|
||||||
|
"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"
|
||||||
|
};
|
||||||
|
|
||||||
|
return nomi[(m-1) % 12];
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* itow(byte d)
|
||||||
|
{
|
||||||
|
const char* nomi[7] =
|
||||||
|
{ "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi",
|
||||||
|
"Sabato", "Domenica" };
|
||||||
|
return nomi[(d-1) % 7];
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user