Example #1
0
static void dateofday(int year,int day,int *rtn_year,int *rtn_month,int *rtn_day)
  {
  int x;
  int flg;
  while(day<0)
    {
    --year;
    if(isleapyear(year)) day+=366;
    else day+=365;
    }
  while(flg=isleapyear(year), day>=366 || !flg && day>=365)
    {
    ++year;
    if(flg) day-=366;
    else day-=365;
    }
  if(isleapyear(year))
    {
    for(x=0;dpmleap[x]<=day;++x) day-=dpmleap[x];
    }
  else
    {
    for(x=0;dpmleap[x]<=day;++x) day-=dpm[x];
    }
  *rtn_year = year;
  *rtn_month = x;
  *rtn_day = day;
  }
Example #2
0
/** Compute a timestamp in the UTC timezone.
*/
time_t DataRecordAbstract::UTC_mktime (
	int year,int month,int day,int hour,int min,int sec) 
{
	if (year<1970 || month<1 || month>12 || day<1
			|| hour<0 || min<0 || sec<0)
		return -1;
	time_t r = 0;
	// TODO : optimize (precomputed data)
	for (int y=1970; y<year; y++) {
		r += 365*24*3600;
		if (isleapyear(y))
			r += 24*3600;
	}
	if (month > 1) {
		for (int m=1; m<month; m++) {
			if (m==2) {
				r += 28*24*3600;
				if (isleapyear(year))
					r += 24*3600;
			}
			else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12) {
				r += 31*24*3600;
			}
			else {
				r += 30*24*3600;
			}
		}
	}
	r += (day-1)*24*3600;
	r += hour*3600;
	r += min*60;
	r += sec;
	return r;
}
Example #3
0
static struct tm *ununixtime(unsigned long *pt)
#define localtime(t) ununixtime((unsigned long *)(t))
{
 unsigned long unixtime, timepart;
 int year, month, mdays;
 static struct tm stm;

 unixtime=*pt-TZ_VAR;
 if(_daylight)
  unixtime+=3600;
 timepart=unixtime%86400L; unixtime/=86400L;
 for(year=1970; 365+isleapyear(year)<=unixtime; year++)
  unixtime-=365+isleapyear(year);
 stm.tm_year=year-1900;
 month=0;
 while(1)
 {
  if(month==1)
   mdays=isleapyear(year)?29:28;
  else
   mdays=monthdays[month];
  if(mdays>unixtime||month==11)
   break;
  unixtime-=mdays;
  month++;
 }
 stm.tm_mon=month;
 stm.tm_mday=unixtime+1;
 stm.tm_hour=timepart/3600;
 stm.tm_min=timepart/60%60;
 stm.tm_sec=timepart%60;
 return(&stm);
}
Example #4
0
int Date2Day(int year,int month,int day)
{
	int count=0;
	int mon[12]={0,31,60,91,121,152,182,213,244,274,305,335};
    if(isleapyear(year)||!isleapyear(year)&&month<=2)//not leap year ,month 1 and 2 no need minus 1
			return mon[month-1]+day;
	return mon[month-1]+day-1;
}
Example #5
0
int GetMonthday(int nyear, int nmonth)
{
	int ndays = -1;//?
	if (nmonth >= 1 && nmonth <= 12)
	{
		switch(nmonth)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				ndays = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				ndays = 30;
				break;
			case 2:/* 闰年29 平年28*/
				if (isleapyear(nyear))
				{
					ndays = 29;
				}
				else
				{
					ndays = 28;
				}
		}
	}
	return ndays;
}
Example #6
0
static int dayofyear(int year,int month,int day)
  {
  int d=0;
  int x;
  if(isleapyear(year)) for(x=0;x!=month;++x) d+=dpmleap[x];
  else for(x=0;x!=month;++x) d+=dpm[x];
  d+=day;
  return d;
  }
Example #7
0
// Return the number of days in a given month
int month_length(int n, int year)
{
	static int length[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (n < 1 || n > 12)
		return 0;
	else if (n == 2 && isleapyear(year))
		return length[n-1] + 1;
	else
		return length[n-1];
}
Example #8
0
static int checkdate(int year,int month,int day)
  {
  if(month<0 || month>11) return -1;
  if(day<0) return -1;
  if(isleapyear(year))
    if(day>=dpmleap[month]) return -1;
    else return 0;
  else
    if(day>=dpm[month]) return -1;
    else return 0;
  }
Example #9
0
bool
CMyDate::isvalidday(int nyear, int nmonth, int nday) const
{
    if (nday < 1
        || nday > 31)
    {
        return false;
    }

    if (isleapyear(nyear) && 2 == nmonth && nday != 29
        || !isleapyear(nyear) && 2 == nmonth && nday != 28)
    {
        return false;
    }

    if (nmonth < 8 
        && ((nmonth & 1) != 0) // 1, 3, 5, 7
        && m_nday != 31
        )
    {   
        return false;
    }

    if (nmonth >= 8 
        && (0 == (nmonth & 1)) // 8, 10, 12
        && m_nday != 31
        )
    {   
        return false;
    }

    if ((4 == nmonth || 6 == nmonth || 9 == nmonth || 11 == nmonth)
        && m_nday != 30)
    {
        return false;
    }

    return true;
}
Example #10
0
//1 valid, 0 invalid
int valid_date(unsigned char year, unsigned char month, unsigned char day){
	unsigned short monthlen[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	if (year < 0 || month < 1 || year < 0 || month > 12){
		return 0;
	}
	if (isleapyear(year) && month == 2) {
		monthlen[1]++;
	}
	if (day>monthlen[month-1] || day < 1) {
		return 0;
	}
	return 1;
}
Example #11
0
int date::getdayofyear() const
{
	int sum = 0, dayofyear[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	for (int i = 1; i < month; i++)
	{
		if (i == 2)
			sum += dayofyear[i - 1] + isleapyear();
		else
			sum += dayofyear[i - 1];
	}
	sum += day;

	return sum;
}
Example #12
0
File: cal.c Project: bproctor/utils
static void print_cal (unsigned month, unsigned year)
{
   unsigned i, day;
   printf ("     %s %d\n Su Mo Tu We Th Fr Sa\n", months[month], year);
   
   /* January 1, 1 was a Saturday */
   for (i = 1, day = 6; i < year; ++i)
      day += isleapyear (i) == 0 ? 1 : 2;

   if (year > 1752 && month > 9)
      day -= 11;
      
   day = (day + (isleapyear (year) == 0 ? days1[month] : ldays1[month])) % 7;
   
   if (year == 1752 && month == 9)
   {
      fputs ("        1  2 14 15 16\n"
             " 17 18 19 20 21 22 23\n"
             " 24 25 26 27 28 29 30\n", stdout);
      return;
   }

   for (i = 0; i < day; ++i)
      fputs ("   ", stdout);

   for (i = 1; i <= (isleapyear (year) == 0 ? days[month] : ldays[month]); ++i)
   {
      if (++day > 7)
      {
         putchar ('\n');
         day = 1;
      }
      printf (" %2d", i);
   }
   putchar ('\n');
}
Example #13
0
static int dayofweek(int year,int month,int day)
  {
  int dow=0;
  int x;
  if(year<1899)
    {
    fprintf(stderr,"Sorry, day of week function is broken for year before 1899\n");
    exit(-1);
    }
  /* Jan 1, 1899 is a sunday */
  for(x=1899;x!=year;++x)
    if(isleapyear(x)) dow+=2;
    else dow+=1;
  dow+=dayofyear(year,month,day);
  return dow%7;
  }
Example #14
0
File: 1076.c Project: ikimi/HDUOJ
int main()
{
    int T, start, N, i, n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &start, &N);
        n = 0;
        for (i = start;;i++)
        {
            if(isleapyear(i))
            {
                if (++n == N)
                    break;
            }
        }
        printf("%d\n", i);
    }
    return 0;
}
Example #15
0
int main(){
    int norm[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int leap[] = {31,29,31,30,31,30,31,31,30,31,30,31};
    int res[] = {0,0,0,0,0,0,0};
    int i;
    int offset=readoffset();
    int lastday=6; // the week day of last year
    for(i=0;i<offset;i++){
        struct year temp;
        if(isleapyear(START_YEAR+i)==1){
            //printf("is leap %d\n",START_YEAR+i);
            temp.daynum = leap;
        }
        else
            temp.daynum = norm;
        temp.fisrtday = (lastday+1) % 7;
        //printf("%d\n",lastday);
        lastday = (lastday + daycount(temp)) % 7;
        count13(temp,res);

    }

    FILE *fout;
    char count[10];
    fout = fopen("friday.out","w");
    for(i=0;i<7;i++){
        sprintf(count,"%d",res[(i+12)%7]);
        fputs(count,fout);
        if ((i+12)%7!=4){
            fputs(" ",fout);
        }
        else
            fputs("\n",fout);
    }
    fclose(fout);
    exit(0);
//    printf("Isleap:%d",readoffset());
}
Example #16
0
int date::getnumholidays() const
{
	int years, zera, holidays = 0;

	for (int i = 1; i <= 12; i++)
	{
		zera = getdayofweek(year, i, 1);

		switch (i)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			for (int j = 1; j <= 31; j++)
			{
				if ((i == 1 && j == 1))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 3 && j == 1))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 5 && j == 5))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 5 && j == 15))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 7 && j == 17))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 8 && j == 15))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 10 && j == 3))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 12 && j == 25))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				if (zera == 0 || zera == 6)
					holidays++;
				zera++;
				if (zera > 6)
					zera = 0;
			}
			break;
		case 2:
			for (int j = 1; j <= 28 + isleapyear(); j++)
			{
				if ((i == 2 && (j == 1 || j == 2 || j == 3)))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				if (zera == 0 || zera == 6)
					holidays++;
				zera++;
				if (zera > 6)
					zera = 0;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			for (int j = 1; j <= 30; j++)
			{
				if ((i == 6 && j == 6))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				else if ((i == 9 && (j == 15 || j == 16 || j == 17)))
				{
					if (zera != 0 && zera != 6)
						holidays++;
				}
				if (zera == 0 || zera == 6)
					holidays++;
				zera++;
				if (zera > 6)
					zera = 0;
			}
			break;
		}
	}
	return holidays;
}
Example #17
0
Bool isleapyear_now(void) {
	/* =================================================== */
	/* check current year from struct tm */
	return isleapyear(_yearto4digit_t());
}
Example #18
0
static unsigned long mk_unixtime(int y, int m, int d, int hh, int mm, int ss)
{
 unsigned long u=0;
 int i;
 /* Clash with NetBSD/x86-64 patch: leaving rc as unsigned long still permits
    to escape the year 2038 problem in favor of year 2106 problem, while a
    dedicated time_t structure can be expected as a 64-bit value on relevant
    platforms -- ASR fix 25/01/2004 */
 unsigned long rc;
 time_t tt;
 long tzshift;
 #ifndef TZ_VAR
  long shiftd1, shiftd2;
 #endif
 struct tm *stm;

 if(y>=2001)
 {
  i=y-2001;
  u=11323;
  /* The following piece of code is rather paranoid in 16/32-bit world, where the
     timestamps are limited to year 2108. */
  #if defined(__32BIT__)||defined(TILED)
   if(i>=400)
   {
    u+=1022679L*(i/400);
    i%=400;
   }
  #endif
  if(i>=100)
  {
   u+=36524L*(i/100);
   i%=100;
  }
  u+=1461L*(i/4);
  u+=365L*(i%4);
 }
 else if(y>=1973)
  u=1096+(y-1973)/4*1461L+((y-1973)%4)*365L;
 else
  u=(y-1970)*365L;
 for(i=1; i<m; i++)
 {
  u+=(int)monthdays[i-1];
  if(i==2)
   u+=isleapyear(y);
 }
 rc=86400*(unsigned long)(u+d-1)+(unsigned long)hh*3600+(unsigned long)mm*60+(unsigned long)ss;
 /* If we have to use the timezone variable, do it now */
 #ifdef TZ_VAR
  tzshift=-TZ_VAR;
  if(_daylight)
   tzshift+=3600;
 #else
  tt=(time_t)rc;
  stm=localtime(&tt);
  debug_assert(stm!=NULL);              /* LIBCS.DLL returns NULL for unixtime beyond
                                           0x7FFFFFFF */
  tzshift=(long)stm->tm_hour*3600+(long)stm->tm_min*60;
  shiftd1=stm->tm_mday;
  stm=gmtime(&tt);
  debug_assert(stm!=NULL);
  shiftd2=stm->tm_mday;
  /* Local time overruns GMT, add 24 hours for safety */
  if(shiftd1<shiftd2&&shiftd1==1&&shiftd2>=28)
   tzshift+=86400;
  else if(shiftd1>shiftd2&&shiftd1>=28&&shiftd2==1)
   tzshift-=86400;
  else if(shiftd1>shiftd2)
   tzshift+=86400;
  else if(shiftd1<shiftd2)
   tzshift-=86400;
  tzshift-=(long)stm->tm_hour*3600+(long)stm->tm_min*60;
  tzshift%=86400;
 #endif
 /* Fix the timezone if it does not roll over the zero */
 return((tzshift>0&&rc<tzshift)?rc:rc-tzshift);
}
Example #19
0
END_TEST
#if defined(__clang__)
#pragma clang diagnostic pop
#endif

START_TEST(leapyears_are_known)
{
	ck_assert_int_eq(isleapyear(1995), 0);
	ck_assert_int_eq(isleapyear(1996), 1);
	ck_assert_int_eq(isleapyear(1997), 0);
	ck_assert_int_eq(isleapyear(1998), 0);
	ck_assert_int_eq(isleapyear(1999), 0);
	ck_assert_int_eq(isleapyear(2000), 1);
	ck_assert_int_eq(isleapyear(2001), 0);
	ck_assert_int_eq(isleapyear(2002), 0);
	ck_assert_int_eq(isleapyear(2003), 0);
	ck_assert_int_eq(isleapyear(2004), 1);
	ck_assert_int_eq(isleapyear(2005), 0);
	ck_assert_int_eq(isleapyear(2006), 0);
	ck_assert_int_eq(isleapyear(2007), 0);
	ck_assert_int_eq(isleapyear(2008), 1);
	ck_assert_int_eq(isleapyear(2009), 0);
	ck_assert_int_eq(isleapyear(2010), 0);
	ck_assert_int_eq(isleapyear(2011), 0);
	ck_assert_int_eq(isleapyear(2012), 1);
	ck_assert_int_eq(isleapyear(2013), 0);
	ck_assert_int_eq(isleapyear(2014), 0);
	ck_assert_int_eq(isleapyear(2015), 0);
	ck_assert_int_eq(isleapyear(2016), 1);
	ck_assert_int_eq(isleapyear(2017), 0);
	ck_assert_int_eq(isleapyear(2018), 0);
	ck_assert_int_eq(isleapyear(2019), 0);
	ck_assert_int_eq(isleapyear(2020), 1);
	ck_assert_int_eq(isleapyear(2021), 0);
}
Example #20
0
static Bool _read_hist( TimeInt year) {
/* =================================================== */
/* Read the historical (measured) weather files.
* Format is
* day-of-month, month number, year, doy, mintemp, maxtemp, ppt
*
* I dislike the inclusion of the first three columns
* but this is the old format.  If a new format emerges
* these columns will likely be removed.
*
* temps are in degrees C, ppt is in cm,
*
* 26-Jan-02 - changed format of the input weather files.
*
*/

	SW_WEATHER_HIST *wh = &SW_Weather.hist;
	FILE *f;
	int x, lineno=0, k = 0, i, j;
	RealF tmpmax, tmpmin, ppt, acc = 0.0;
	TimeInt doy;
	
	char fname[MAX_FILENAMESIZE];
	
	
	sprintf(fname, "%s.%4d", SW_Weather.name_prefix, year);

	if ( NULL == (f = fopen(fname, "r")) )
		return FALSE;
	
	_clear_hist_weather();
	
	
	while( GetALine(f, inbuf) ) {
		lineno++;
		x=sscanf( inbuf, "%d %f %f %f", &doy, &tmpmax, &tmpmin, &ppt);
		if (x < 4) {
			LogError(logfp, LOGFATAL, "%s : Incomplete record %d (doy=%d).", fname, lineno, doy);
		}
		if (x > 4) {
			LogError(logfp, LOGFATAL, "%s : Too many values in record %d (doy=%d).", fname, lineno, doy);
		}
		if (doy < 1 || doy > MAX_DAYS) {
			LogError(logfp, LOGFATAL, "%s : Day of year out of range, line %d.", fname, lineno);
		}
	
	
		/* --- Make the assignments ---- */
		doy--;
		wh->temp_max[doy] = tmpmax;
		wh->temp_min[doy] = tmpmin;
		wh->temp_avg[doy] = (tmpmax + tmpmin) / 2.0;
		wh->ppt[doy]      = ppt;
		
		/* Reassign if invalid values are found.  The values are
		* either valid or WTH_MISSING.  If they were not
		* present in the file, we wouldn't get this far because
		* sscanf() would return too few items.
		*/
		if ( missing(tmpmax) ) {
			wh->temp_max[doy] = WTH_MISSING;
			LogError(logfp, LOGWARN, "%s : Missing max temp on doy=%d.", fname, doy+1);
		}
		if ( missing(tmpmin) ) {
			wh->temp_min[doy] = WTH_MISSING;
			LogError(logfp, LOGWARN, "%s : Missing min temp on doy=%d.", fname, doy+1);
		}
		if ( missing(ppt) ) {
			wh->ppt[doy] = 0.;
			LogError(logfp, LOGWARN, "%s : Missing PPT on doy=%d.", fname, doy+1);
		}
		
		if(!missing(tmpmax) && !missing(tmpmin)) {
			k++;
			acc += wh->temp_avg[doy];
		}
	}  /* end of input lines */
	
	wh->temp_year_avg = acc / (k + 0.0);
	
	x = 0;
	for( i=0; i < MAX_MONTHS; i++) {
		k = 31;
		if(i == 8 || i == 3 || i == 5 || i == 10)
			k = 30; // september, april, june, & november all have 30 days...
		else if(i == 1) {
			k = 28; // february has 28 days, except if it's a leap year, in which case it has 29 days...
			if(isleapyear(year))
				k = 29; 
		}
		
		acc = 0.0;
		for( j=0; j < k; j++)
			acc += wh->temp_avg[j + x];
		wh->temp_month_avg[i] = acc / (k + 0.0);
		x += k;
	}
	
	fclose(f);
	
	return TRUE;
}
Example #21
0
void date::printcalendatofmonth() const
{
	int zera;

	cout << year << " " << month << endl;

	zera = getdayofweek(year, month, 1);

	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		for (int j = zera; j > 0; j--)
			cout << 0 << " ";
		for (int j = 1; j <= 31; j++)
		{
			cout << j << " ";
			zera++;
			if (zera > 6 && j != 31)
			{
				cout << endl;
				zera = 0;
			}
		}
		for (; zera < 7; zera++)
			cout << 0 << " ";
		break;
	case 2:
		for (int j = zera; j > 0; j--)
			cout << 0 << " ";
		for (int j = 1; j <= 28 + isleapyear(); j++)
		{
			cout << j << " ";
			zera++;
			if (zera > 6 && j != 28 + isleapyear())
			{
				cout << endl;
				zera = 0;
			}
		}
		for (; zera < 7; zera++)
			cout << 0 << " ";
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		for (int j = zera; j > 0; j--)
			cout << 0 << " ";
		for (int j = 1; j <= 30; j++)
		{
			cout << j << " ";
			zera++;
			if (zera > 6 && j != 30)
			{
				cout << endl;
				zera = 0;
			}
		}
		for (; zera < 7; zera++)
			cout << 0 << " ";
		break;
	}
}
Example #22
0
TimeInt Time_get_lastdoy_y(TimeInt year) {
	return isleapyear(year) ? 366 : 365;
}