📄 xdate.cpp
字号:
/*************************************************************************** xdate.cpp - description ------------------- begin : Thu Jan 17 2002 copyright : (C) 2002 by email : ***************************************************************************//*************************************************************************** * * * 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; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//* $Id: xdate.cpp,v 1.7 2000/11/10 19:04:17 dbryson Exp $ Xbase project source code These functions are used for processing dates. All functions assume a standard date format of CCYYMMDD for Century,Year,Month and Day Copyright (C) 1997 StarTech, Gary A. Kunkel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Mail: Technology Associates, Inc. XBase Project 1455 Deming Way #11 Sparks, NV 89434 USA Email: xbase@techass.com See our website at: xdb.sourceforge.net V 1.0 10/10/97 - Initial release of software V 1.2 11/30/97 - Updated leap-year processing logic V 1.5 1/2/98 - Added memo field support V 1.6a 4/1/98 - Added expression support V 1.6b 4/8/98 - Numeric index keys V 1.7.1 5/25/98 - Enhancements/bug fixes from eljorgo@fontun.com V 1.8.0.a 1/27/99 - Added default date format processing V 1.9 6/29/99 - CPP ified, minor bug fixes*/#ifdef __GNUG__ #pragma implementation "xdate.h"#endif#include <ctype.h>#include <stdio.h>#include <stdlib.h> #include <string>using namespace std;#include <time.h>#ifdef __WIN32__#include "xbconfigw32.h"#else#include "xbconfig.h"#endif#include "xbase.h"#include "xdate.h"#include "retcodes.h"/*! \file xdate.cpp*/int xbDate::DaysInMonths[2][13];int xbDate::AggregatedDaysInMonths[2][13];const xbString *xbDate::Days[7];const xbString *xbDate::Months[12];#define EPOCH_MIN 100#define EPOCH_MAX 3000#define DAYS_AD(year) ((year) *365L + (year) / 4 - (year) / 100 + (year) / 400)/***************************************************************///! Short description./*! \param Date8*/xbDate::xbDate( const xbString & Date8 ) { if( DateIsValid( Date8 )) cDate8 = Date8; else Sysdate(); SetDateTables();}/***************************************************************///! Short description./*! \param Date8*/xbDate::xbDate( const char * Date8 ) { if( DateIsValid( Date8 )) cDate8 = Date8; else Sysdate(); /* if invalid date, set class to sysdate */ SetDateTables();}/***************************************************************///! Short description./*!*/xbDate::xbDate() { Sysdate(); SetDateTables();}/***************************************************************///! Short description./*!*/void xbDate::SetDateTables() { if( AggregatedDaysInMonths[1][12] != 366 ){ /* first time called ? */ AggregatedDaysInMonths[0][0] = 0; AggregatedDaysInMonths[0][1] = 31; AggregatedDaysInMonths[0][2] = 59; AggregatedDaysInMonths[0][3] = 90; AggregatedDaysInMonths[0][4] = 120; AggregatedDaysInMonths[0][5] = 151; AggregatedDaysInMonths[0][6] = 181; AggregatedDaysInMonths[0][7] = 212; AggregatedDaysInMonths[0][8] = 243; AggregatedDaysInMonths[0][9] = 273; AggregatedDaysInMonths[0][10] = 304; AggregatedDaysInMonths[0][11] = 334; AggregatedDaysInMonths[0][12] = 365; AggregatedDaysInMonths[1][0] = 0; AggregatedDaysInMonths[1][1] = 31; AggregatedDaysInMonths[1][2] = 60; AggregatedDaysInMonths[1][3] = 91; AggregatedDaysInMonths[1][4] = 121; AggregatedDaysInMonths[1][5] = 152; AggregatedDaysInMonths[1][6] = 182; AggregatedDaysInMonths[1][7] = 213; AggregatedDaysInMonths[1][8] = 244; AggregatedDaysInMonths[1][9] = 274; AggregatedDaysInMonths[1][10] = 305; AggregatedDaysInMonths[1][11] = 335; AggregatedDaysInMonths[1][12] = 366; DaysInMonths[0][0] = 0; DaysInMonths[0][1] = 31; DaysInMonths[0][2] = 28; DaysInMonths[0][3] = 31; DaysInMonths[0][4] = 30; DaysInMonths[0][5] = 31; DaysInMonths[0][6] = 30; DaysInMonths[0][7] = 31; DaysInMonths[0][8] = 31; DaysInMonths[0][9] = 30; DaysInMonths[0][10] = 31; DaysInMonths[0][11] = 30; DaysInMonths[0][12] = 31; DaysInMonths[1][0] = 0; DaysInMonths[1][1] = 31; DaysInMonths[1][2] = 29; DaysInMonths[1][3] = 31; DaysInMonths[1][4] = 30; DaysInMonths[1][5] = 31; DaysInMonths[1][6] = 30; DaysInMonths[1][7] = 31; DaysInMonths[1][8] = 31; DaysInMonths[1][9] = 30; DaysInMonths[1][10] = 31; DaysInMonths[1][11] = 30; DaysInMonths[1][12] = 31;/* per Rene de Zwart <renez@lightcon.xs4all.nl>The following assignments of Days[] and Months[] should be accessableby getting the locale and with it all the language dependencies like monthsdays and abbreviations, printing the date should lookup the locale.However, I didn't know how to do this and didn't (yet) find any documentationon how to do this...This should work for unices, ms dos/win, os390 (is this unix?) and VAX.those are the platforms that xbase is being used on.If you know how, please let me know how - or make the changes to this codeand send it to me..Gary - gkunkelstartech.keller.tx.us*//*#ifdef XB_CASTELLANO Days[0] = new const xbString( "Domingo" ); Days[1] = new const xbString( "Lunes" ); Days[2] = new const xbString( "Martes" ); Days[3] = new const xbString( "Miercoles" ); Days[4] = new const xbString( "Jueves" ); Days[5] = new const xbString( "Viernes" ); Days[6] = new const xbStirng( "Sabado" ); Months[0] = new const xbString( "Enero" ); Months[1] = new const xbString( "Febrero" ); Months[2] = new const xbString( "Marzo" ); Months[3] = new const xbString( "Abril" ); Months[4] = new const xbString( "Mayo" ); Months[5] = new const xbString( "Junio" ); Months[6] = new const xbString( "Julio" ); Months[7] = new const xbString( "Agosto" ); Months[8] = new const xbString( "Septiembre" ); Months[9] = new const xbString( "Octubre" ); Months[10] = new const xbString( "Noviembre" ); Months[11] = new const xbString( "Diciembre" );#else Days[0] = new const xbString( "Sunday" ); Days[1] = new const xbString( "Monday" ); Days[2] = new const xbString( "Tuesday" ); Days[3] = new const xbString( "Wednesday" ); Days[4] = new const xbString( "Thursday" ); Days[5] = new const xbString( "Friday" ); Days[6] = new const xbString( "Saturday" ); Months[0] = new const xbString( "January" ); Months[1] = new const xbString( "February" ); Months[2] = new const xbString( "March" ); Months[3] = new const xbString( "April" ); Months[4] = new const xbString( "May" ); Months[5] = new const xbString( "June" ); Months[6] = new const xbString( "July" ); Months[7] = new const xbString( "August" ); Months[8] = new const xbString( "September" ); Months[9] = new const xbString( "October" ); Months[10] = new const xbString( "November" ); Months[11] = new const xbString( "December" );#endif */ }}/***************************************************************///! Short description./*! \param Date8*//* this function returns century and year from a CCYYMMDD date */int xbDate::YearOf( const char * Date8 ) const{ char year[5]; year[0] = Date8[0]; year[1] = Date8[1]; year[2] = Date8[2]; year[3] = Date8[3]; year[4] = 0x00; return( atoi( year ));}/***************************************************************///! Short description./*! \param Date8*//* this function returns the month from a CCYYMMDD date */int xbDate::MonthOf( const char * Date8 ) const{ char month[3]; month[0] = Date8[4]; month[1] = Date8[5]; month[2] = 0x00; return( atoi( month ));}/***************************************************************///! Short description./*! \param Date8*//* this function returns TRUE if a CCYYMMDD date is a leap year*/int xbDate::IsLeapYear( const char * Date8 ) const{ int year; year = YearOf( Date8 ); if(( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) return 1; else return 0;}/***************************************************************///! Short description./*! \param Format \param Date8*//* this function returns the "day of" from a CCYYMMDD date *//* format = XB_FMT_WEEK Number of day in WEEK 0-6 ( Sun - Sat ) format = XB_FMT_MONTH Number of day in MONTH 1-31 format = XB_FMT_YEAR Number of day in YEAR 1-366*/int xbDate::DayOf( int Format, const char * Date8 ) const{ char day[3]; int iday, imonth, iyear, iday2; /* check for valid format switch */ if( Format!=XB_FMT_WEEK && Format!=XB_FMT_MONTH && Format!=XB_FMT_YEAR ) return XB_INVALID_OPTION; if( Format == XB_FMT_WEEK ) { iday = DayOf( XB_FMT_MONTH, Date8 ); imonth = MonthOf( Date8 ); iyear = YearOf ( Date8 ); /* The following formula uses Zeller's Congruence to determine the day of the week */ if( imonth > 2 ) /* init to February */ imonth -= 2; else { imonth += 10; iyear--; } iday2 = ((13 * imonth - 1) / 5) +iday + ( iyear % 100 ) + (( iyear % 100 ) / 4) + ((iyear /100 ) / 4 ) - 2 * ( iyear / 100 ) + 77 ; return( iday2 - 7 * ( iday2 / 7 )); } else if( Format == XB_FMT_MONTH ) { day[0] = Date8[6]; day[1] = Date8[7]; day[2] = 0x00; return( atoi( day )); } else return( AggregatedDaysInMonths[IsLeapYear(Date8)][MonthOf(Date8)-1]+ DayOf(XB_FMT_MONTH, Date8));}/**********************************************************************///! Short description./*!*//* this method sets the class date & returns a pointer to system date */xbString& xbDate::Sysdate(){ char dt[9]; time_t timer; struct tm *tblock; timer = time( NULL ); tblock = localtime( &timer ); tblock->tm_year += 1900; tblock->tm_mon++; sprintf( dt,"%4d%02d%02d",tblock->tm_year,tblock->tm_mon,tblock->tm_mday ); dt[8] = 0x00; cDate8 = dt; return cDate8;}/***************************************************************///! Short description./*! \param Date8*//* this function checks a date for validity - returns 1 if OK */int xbDate::DateIsValid( const char * Date8 ) const{ int year, month, day; if(!isdigit( Date8[0] ) || !isdigit( Date8[1] ) || !isdigit( Date8[2] ) || !isdigit( Date8[3] ) || !isdigit( Date8[4] ) || !isdigit( Date8[5] ) || !isdigit( Date8[6] ) || !isdigit( Date8[7] ) ) return 0; year = YearOf ( Date8 ); month = MonthOf( Date8 ); day = DayOf ( XB_FMT_MONTH, Date8 ); /* check the basics */ if( year == 0 || month < 1 || month > 12 || day < 1 || day > 31 ) return 0; /* April, June, September and November have 30 days */ if(( month==4 || month==6 || month==9 || month==11 )&& day > 30 ) return 0; /* check for February with leap year */ if( month == 2 ) if( IsLeapYear( Date8 )) { if( day > 29 ) return 0; } else { if( day > 28 ) return 0; } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -