File: calendar.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (154 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*-----------------------------------------------------------------------------------*/
/* INRIA 2005 */
/* Allan CORNET */
/*-----------------------------------------------------------------------------------*/ 
#include "calendar.h"
#ifdef WIN32
#include "../os_specific/win_mem_alloc.h" /* MALLOC */
#else
#include "../os_specific/sci_mem_alloc.h" /* MALLOC */
#endif
/*-----------------------------------------------------------------------------------*/
int IsAScalar(int RhsNumber);
static int isBissextile (unsigned annee);
static unsigned months_to_days (unsigned mois);
static long years_to_days (unsigned annee);
static long ymd_to_scalar (unsigned annee, unsigned mois, unsigned jour);
int *InversionMatrixInt(int W,int L,int *Matrix);
/*-----------------------------------------------------------------------------------*/
#define NBRDAY 7
#define NBRWEEK 6
/*-----------------------------------------------------------------------------------*/

int days[12]    = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*-----------------------------------------------------------------------------------*/
int C2F(intcalendar) _PARAMS((char *fname))
{
	static int l1,n1,m1;

	int month=0;
	int year=0;
	int day, day_1, numdays, i;
    int a=0;

	int *CALMONTH=NULL;
	int *tmpMatrix=NULL;

	Rhs = Max(0, Rhs);
	CheckRhs(2,2);
	CheckLhs(1,1);
	
	if ( IsAScalar(Rhs-1) && IsAScalar(Rhs) )
	{
		GetRhsVar(1,"i",&m1,&n1,&l1);
		month=*istk(l1);

		GetRhsVar(2,"i",&m1,&n1,&l1);
		year=*istk(l1);

		if ( (year<1800) || (year>3000) )
		{
			Scierror(999,"Error : year (1800 - 3000).\n");
			return 0;
		}
				
		if ( (month<1) || (month>12) )
		{
			Scierror(999,"Error : month (1 - 12).\n");
			return 0;
		}
				
	}
	else
	{
		Scierror(999,"Invalid argument(s) type.\n");
		return 0;
	}
	CALMONTH=(int *)MALLOC( (NBRDAY*NBRWEEK)*sizeof(int) );
	for (i=0;i<NBRDAY*NBRWEEK;i++) CALMONTH[i]=0;

	/* verification si le mois de fevrier est a 28 jours ou 29 jours */
    numdays = days[month - 1];
    if (2 == month && isBissextile(year)) ++numdays;

	/* Cela sert a faire demarrer le calendrier par le Lundi */
    day_1 = (int)((ymd_to_scalar(year, month, 1) - (long)1) % 7L);

	for (day = 0; day < day_1; ++day) a++;

    /* ici on remplit les jours tous simplement */
    for (day = 1; day <= numdays; ++day, ++day_1, day_1 %= 7)
    {
        CALMONTH[a]= day;
		a++;
    }
	m1=NBRWEEK;
	n1=NBRDAY;
	tmpMatrix=CALMONTH;

	CALMONTH=InversionMatrixInt(NBRDAY,NBRWEEK,CALMONTH);
	if(tmpMatrix) {FREE(tmpMatrix);tmpMatrix=NULL;}
	
	CreateVarFromPtr(Rhs+1, "i", &m1, &n1 ,&CALMONTH);
	LhsVar(1)=Rhs+1;

	C2F(putlhsvar)();

	if (CALMONTH) {FREE(CALMONTH);CALMONTH=NULL;}
	return 0;
}
/*-----------------------------------------------------------------------------------*/
int IsAScalar(int RhsNumber)
{
	int bOK=0;

	if ( GetType(RhsNumber) == sci_matrix )
	{
		static int l1,n1,m1;
		GetRhsVar(RhsNumber,"d",&m1,&n1,&l1);
		if ( (m1 == 1) && (n1 == 1) )
		{
			bOK=1;
		}
	}
	return bOK;
}
/*-----------------------------------------------------------------------------------*/
static int isBissextile (unsigned annee)
{
    return annee % 400 == 0 || (annee % 4 == 0 && annee % 100 != 0);
}
/*-----------------------------------------------------------------------------------*/
static unsigned months_to_days (unsigned mois)
{
    return (mois * 3057 - 3007) / 100;
}
/*-----------------------------------------------------------------------------------*/
static long years_to_days (unsigned annee)
{
    return annee * 365L + annee / 4 - annee / 100 + annee / 400;
}
/*-----------------------------------------------------------------------------------*/
static long ymd_to_scalar (unsigned annee, unsigned mois, unsigned jour)
{
    long scalaire;
    scalaire = jour + months_to_days(mois);
    if ( mois > 2 )                      
    scalaire -= isBissextile(annee) ? 1 : 2;
    annee--;
    scalaire += years_to_days(annee);
    return scalaire;
}
/*-----------------------------------------------------------------------------------*/
int *InversionMatrixInt(int W,int L,int *Matrix)
{
	int *buffer = (int *)MALLOC( (W*L)*sizeof(int) );

	int i=0;
	int j=0;

	for (i=0; i<W; i++) for (j=0; j<L; j++) buffer[i*L+j] = Matrix[j*W+i];

	return buffer;
}
/*-----------------------------------------------------------------------------------*/