-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfechas.c
More file actions
199 lines (185 loc) · 3.44 KB
/
fechas.c
File metadata and controls
199 lines (185 loc) · 3.44 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Proyecto fin de carrera*/
/* Optimizacion del stock de farmacos en hospitales*/
/* Fecha: 2014-2015*/
/*Apellidos: Hoyos Martín
Nombre: César*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "typedef.h"
#include <time.h>
#include "fechas.h"
#include "matrices.h"
#define LUNES 0
#define MARTES 1
#define MIERCOLES 2
#define JUEVES 3
#define VIERNES 4
#define SABADO 5
#define DOMINGO 6
#define TAM_BUF 100
void compruebaFecha(){
}
int bisiesto(int year){
int bis;
bis=(year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
return bis;
}
void obtieneFechasPedidos(int*v, int tam, int ** FechasPedido){
int x;
int j=0;
for(x=0; x<tam; x++){
if(v[x]!=0){
fechaPedido(x, FechasPedido[j]);
printf("%d/%d/%d\n", FechasPedido[j][0], FechasPedido[j][1], FechasPedido[j][2]);
printf("%d\n", v[x]);
j++;
}
}
}
/*
Funcion que recibe un vector y devuelve los campos del mismo
rellenos con {día, mes, año} en formato entero.
*/
void fechaHoy(int * fechaActual){
time_t t;
struct tm *tm;
char fechayhora[100];
t=time(NULL);
tm=localtime(&t);
strftime(fechayhora, 100, "%d/%m/%Y", tm);
int i;
int j=0;
int k=0;
char auxFecha[5];
for(i=0;fechayhora[i]!='\0';i++){
auxFecha[k]=fechayhora[i];
k++;
if(fechayhora[i]=='/'){
auxFecha[k]='\0';
fechaActual[j]=atoi(auxFecha);
j++;
k=0;
}
}
auxFecha[k]='\0';
fechaActual[j]=atoi(auxFecha);
}
void fechaPedido(int dia, int* fecha){
int diasMes;
int *fechaActual;
inicializaVector(3, &fechaActual);
int i;
//Obtenemos hoy
fechaHoy(fechaActual);
//Al día de hoy le añadimos los días en los que se pide
for(i=0; i<3; i++){
if(i==0){
fecha[i]=fechaActual[i]+dia;
}else{
fecha[i]=fechaActual[i];
}
}
liberaVector(fechaActual);
//Procedemos a comprobar que la fecha obtenida es correcta en funcion del número
//de días que tiene cada mes para ver las correcciones a realizar.
switch (fecha[1]){
case 1:
diasMes = 31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 2:
if(bisiesto(fecha[2])==1){
diasMes=29;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
}
else{
diasMes=28;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
}
break;
case 3:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 4:
diasMes=30;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 5:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 6:
diasMes=30;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 7:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 8:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 9:
diasMes=30;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 10:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 11:
diasMes=30;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
break;
case 12:
diasMes=31;
if(fecha[0]>diasMes){
fecha[0]=fecha[0]-diasMes;
fecha[1]=1;
fecha[2]++;
}
break;
default:
fecha[0]=fecha[0]-diasMes;
fecha[1]++;
}
}