#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEAP(y) ((y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? 1 : 0)
static void ha_ltona0(int idx, int length, char *answer);
static int ha_antol(char *request, int length);
static void cb1_compute_date (char *odate, char *idate, int addval);
static int getRemainDay(char *odate, char *idate);
int main(int argc, char *argv[])
{
int rc,mm,dd; char begin_date[10] = "20190101";
char end_date [10];
for(mm=1; mm<=12; mm++)
{
for(dd=1; dd<=31; dd++)
{
memset(end_date,0x00,sizeof(end_date));
sprintf(end_date, "2019%.2d%.2d", mm, dd);
rc = getRemainDay(end_date,begin_date);
if(rc<0) printf("(%s)(%s):(%s)\n", begin_date, end_date, " is not valid day!! error");
else printf("(%s)(%s):(%d)\n", begin_date, end_date, getRemainDay(end_date,begin_date));
}
}
for(mm=1; mm<=12; mm++) { for(dd=1; dd<=31; dd++)
{
memset(end_date,0x00,sizeof(end_date));
sprintf(end_date, "2019%.2d%.2d", mm, dd);
rc = getRemainDay(end_date,begin_date);
if(rc<0) printf("(%s)(%s):(%s)\n", begin_date, end_date, " is not valid day!! error");
}
}
return 0;
}
/*- ------------------------------------------------ -*
*- ha_ltona0
*- ------------------------------------------------ -*/
void ha_ltona0(int idx, int length, char *answer)
{
if(length==1)
{
sprintf(answer,"%.1d",idx);
}
else if(length==2)
{
sprintf(answer,"%.2d",idx);
}
else if(length==3)
{
sprintf(answer,"%.3d",idx);
}
else if(length==4)
{
sprintf(answer,"%.4d",idx);
}
}
/*- ------------------------------------------------ -*
*- ha_antol
*- ------------------------------------------------ -*/
int ha_antol(char *request, int length)
{
char answer[100];
memset(answer,0x00,sizeof(answer));
memcpy(answer,request,length);
return(atoi(answer));
}
/*- ------------------------------------------------ -*
*- cb1_compute_date
*- ------------------------------------------------ -*/
void cb1_compute_date (odate, idate, addval)
char *odate;
char *idate;
int addval;
{
int yy;
int mm;
int dd;
int leap;
static int mmt[2] [13] =
{ {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
yy = ha_antol(idate, 4);
mm = ha_antol(idate+4, 2);
dd = ha_antol(idate+6, 2) + addval;
leap = LEAP(yy);
if (addval == 0) {
sprintf(odate, "%.8s", idate);
return;
}
else
if (addval > 0) {
for ( ; dd > mmt[leap][mm]; mm++)
{
dd -= mmt[leap] [mm];
if (mm >= 12)
{
mm = 0;
yy++;
leap = LEAP(yy);
}
}
}
else {
mm--;
if (mm == 0)
{
mm = 12;
yy--;
leap = LEAP(yy);
}
for ( ; dd < 1; mm--)
{
dd += mmt [leap] [mm];
if (mm <= 1)
{
mm = 13;
yy--;
leap = LEAP(yy);
}
}
mm++;
if (mm > 12)
{
mm = 1;
yy++;
}
}
ha_ltona0(yy, 4, odate);
ha_ltona0(mm, 2, odate+4);
ha_ltona0(dd, 2, odate+6);
}
/*- ------------------------------------------------ -*
*- getRemainDay
*- ------------------------------------------------ -*/
int getRemainDay(char *odate, char *idate)
{
int k;
char begin_date[10];
if(strcmp(odate,idate)<0) return(-1);
if(memcmp(odate,idate,strlen("YYYYMMDD"))==0) return 1;
for(k=1; k<9999; k++)
{
memset(begin_date,0x00,sizeof(begin_date));
cb1_compute_date(begin_date,idate,k);
if(memcmp(begin_date,odate,strlen("YYYYMMDD"))==0) break;
}
if(k==9999) return(-1);
return k+1;
}
/*RESULT
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Downloads>10
,,
,,
,,
,,
(20190101)(20190818):(230)
(20190101)(20190819):(231)
(20190101)(20190820):(232)
(20190101)(20190821):(233)
(20190101)(20190822):(234)
(20190101)(20190823):(235)
(20190101)(20190824):(236)
(20190101)(20190825):(237)
(20190101)(20190826):(238)
(20190101)(20190827):(239)
(20190101)(20190828):(240)
(20190101)(20190829):(241)
(20190101)(20190830):(242)
(20190101)(20190831):(243)
(20190101)(20190901):(244)
(20190101)(20190902):(245)
(20190101)(20190903):(246)
(20190101)(20190904):(247)
(20190101)(20190905):(248)
(20190101)(20190906):(249)
(20190101)(20190907):(250)
(20190101)(20190908):(251)
(20190101)(20190909):(252)
(20190101)(20190910):(253)
(20190101)(20190911):(254)
(20190101)(20190912):(255)
(20190101)(20190913):(256)
(20190101)(20190914):(257)
(20190101)(20190915):(258)
(20190101)(20190916):(259)
(20190101)(20190917):(260)
(20190101)(20190918):(261)
(20190101)(20190919):(262)
(20190101)(20190920):(263)
(20190101)(20190921):(264)
(20190101)(20190922):(265)
(20190101)(20190923):(266)
(20190101)(20190924):(267)
(20190101)(20190925):(268)
(20190101)(20190926):(269)
(20190101)(20190927):(270)
(20190101)(20190928):(271)
(20190101)(20190929):(272)
(20190101)(20190930):(273)
(20190101)(20190931):( is not valid day!! error)
(20190101)(20191001):(274)
(20190101)(20191002):(275)
(20190101)(20191003):(276)
(20190101)(20191004):(277)
(20190101)(20191005):(278)
(20190101)(20191006):(279)
(20190101)(20191007):(280)
(20190101)(20191008):(281)
(20190101)(20191009):(282)
(20190101)(20191010):(283)
(20190101)(20191011):(284)
(20190101)(20191012):(285)
(20190101)(20191013):(286)
(20190101)(20191014):(287)
(20190101)(20191015):(288)
(20190101)(20191016):(289)
(20190101)(20191017):(290)
(20190101)(20191018):(291)
(20190101)(20191019):(292)
(20190101)(20191020):(293)
(20190101)(20191021):(294)
(20190101)(20191022):(295)
(20190101)(20191023):(296)
(20190101)(20191024):(297)
(20190101)(20191025):(298)
(20190101)(20191026):(299)
(20190101)(20191027):(300)
(20190101)(20191028):(301)
(20190101)(20191029):(302)
(20190101)(20191030):(303)
(20190101)(20191031):(304)
(20190101)(20191101):(305)
(20190101)(20191102):(306)
(20190101)(20191103):(307)
(20190101)(20191104):(308)
(20190101)(20191105):(309)
(20190101)(20191106):(310)
(20190101)(20191107):(311)
(20190101)(20191108):(312)
(20190101)(20191109):(313)
(20190101)(20191110):(314)
(20190101)(20191111):(315)
(20190101)(20191112):(316)
(20190101)(20191113):(317)
(20190101)(20191114):(318)
(20190101)(20191115):(319)
(20190101)(20191116):(320)
(20190101)(20191117):(321)
(20190101)(20191118):(322)
(20190101)(20191119):(323)
(20190101)(20191120):(324)
(20190101)(20191121):(325)
(20190101)(20191122):(326)
(20190101)(20191123):(327)
(20190101)(20191124):(328)
(20190101)(20191125):(329)
(20190101)(20191126):(330)
(20190101)(20191127):(331)
(20190101)(20191128):(332)
(20190101)(20191129):(333)
(20190101)(20191130):(334)
(20190101)(20191131):( is not valid day!! error)
(20190101)(20191201):(335)
(20190101)(20191202):(336)
(20190101)(20191203):(337)
(20190101)(20191204):(338)
(20190101)(20191205):(339)
(20190101)(20191206):(340)
(20190101)(20191207):(341)
(20190101)(20191208):(342)
(20190101)(20191209):(343)
(20190101)(20191210):(344)
(20190101)(20191211):(345)
(20190101)(20191212):(346)
(20190101)(20191213):(347)
(20190101)(20191214):(348)
(20190101)(20191215):(349)
(20190101)(20191216):(350)
(20190101)(20191217):(351)
(20190101)(20191218):(352)
(20190101)(20191219):(353)
(20190101)(20191220):(354)
(20190101)(20191221):(355)
(20190101)(20191222):(356)
(20190101)(20191223):(357)
(20190101)(20191224):(358)
(20190101)(20191225):(359)
(20190101)(20191226):(360)
(20190101)(20191227):(361)
(20190101)(20191228):(362)
(20190101)(20191229):(363)
(20190101)(20191230):(364)
(20190101)(20191231):(365)
(20190101)(20190229):( is not valid day!! error)
(20190101)(20190230):( is not valid day!! error)
(20190101)(20190231):( is not valid day!! error)
(20190101)(20190431):( is not valid day!! error)
(20190101)(20190631):( is not valid day!! error)
(20190101)(20190931):( is not valid day!! error)
(20190101)(20191131):( is not valid day!! error)
*/