#include<iostream>
#include<ostream>
#include<string>
#include<cstring>
#include<string.h>
#include "Fecha.h"
#include<ctime>
#include<sstream>
//#include"Punto.h"
using namespace std;
/*
Fecha::Fecha(){
dia=1;
mes=2;
anyo=2000;
}
*/
Fecha::~Fecha(){
dia=-1;
mes=-1;
anyo=-1;
}
Fecha::Fecha(){
//fecha y hora del sistema
time_t ahora=time(0);
tm* localtm=localtime(&ahora);
dia=localtm->tm_mday;
mes=localtm->tm_mon+1;
anyo=localtm->tm_year+1900;
/*
//convertir a struct tm para fecha y hora local
tm* localtm=localtime(&ahora);
cout<<localtm->tm_mday <<"/"
<<localtm->tm_mon+1<<"/"
<<localtm->tm_year+1900<<endl;
*/
}
Fecha::Fecha(const Fecha &f){
dia=f.dia;
mes=f.mes;
anyo=f.anyo;
}
Fecha::Fecha(int d,int m,int a){
//bool diac,mesc,anyoc;
if(!setDia(d)){
dia=-1;
}
else{
dia=d;
}
if(!setMes(m)){
mes=-1;
}
else{
mes=m;
}
if(!setAnyo(a)){
anyo=-1;
}
else{
anyo=a;
}
/*
dia=d;
mes=m;
anyo=a;
dia=setDia(d);
mes=setMes(m);
anyo=setAnyo(a);
*/
}
bool Fecha::setDia(int d){
bool correcto=false;
if ((d>=1)&&(d<=31)){
correcto=true;
dia=d;
}
else {
dia=-1;
}
return correcto;
}
bool Fecha::setMes(int m){
bool correcto=false;
if ((m>=1)&&(m<=12)){
correcto=true;
mes=m;
}
else{
mes=-1;
}
return correcto;
}
bool Fecha::setAnyo(int a){
bool correcto=false;
if (a>0){
correcto=true;
anyo=a;
}
else{
anyo=-1;
}
return correcto;
}
int Fecha::getDia()const{
return dia;
}
int Fecha::getMes()const{
return mes;
}
int Fecha::getAnyo()const{
return anyo;
}
void Fecha::Imprimir(ostream &os)const{
//bool diacorrecto,mescorrecto,anyocorrecto;
//os<<dia<<"/"<<mes<<"/"<<anyo;
//cout<<endl;
//ahora vamos a validar
/*
diacorrecto=f2.setDia(dia);
mescorrecto=f2.setMes(mes);
anyocorrecto=f2.setAnyo(anyo);
if((diacorrecto==false)||(mescorrecto==false)||(anyocorrecto==false)){
os<<"fecha no valida"<<endl;
}
else{
os<<dia<<"/"<<mes<<"/"<<anyo;
}
*/
if( (dia==-1)||(mes==-1)||(anyo==-1) ){
os<<"fecha no valida"<<endl;
}
else{
os<<dia<<"/"<<mes<<"/"<<anyo;
}
}
string Fecha::toString()const {
/*
//opcion 1
string fecha="";
int resto;
fecha=dia/10 +'0';
//cout<<fecha<<endl;
fecha+=dia%10 +'0';
//cout<<fecha<<endl;
fecha+=mes/10+'0';
fecha+=mes%10 + '0';
//convertimos el año a string
fecha+=anyo/1000 +'0';
resto=anyo%1000;
fecha+=resto/100+'0';
resto=resto%100;
fecha+=resto/10+'0';
fecha+=resto%10+'0';
*/
//opcion 2
stringstream sfecha;
sfecha << dia << '/' << mes << '/' << anyo << endl;
// el método str() del objeto stringstream devuelve un string
// con los datos que contiene el flujo de datos.
//cout << sfecha.str() << endl;
return sfecha.str();
}
int Fecha::getDiferenciaAnyos(const Fecha &f)const{
int diferencia;
time_t ahora=time(0);
tm* tm1=localtime(&ahora);
tm* tm2=new tm(*tm1);
tm1->tm_mday=dia;
tm1->tm_mon=mes-1;
tm1->tm_year=anyo-1900;
time_t t1=mktime(tm1);
//time_t t2=mktime(tm2);
diferencia=difftime(time_t t1,time_t t2);
return diferencia;
}
//---------------------------------------------------------------------------------------------------------------------
//Fecha.h
#include<iostream>
#include<ostream>
#include<string>
#include<cstring>
#include<string.h>
#include<ctime>
#include<sstream>
using namespace std;
class Fecha{
private:
int dia,mes,anyo;
public:
Fecha();
Fecha(int d,int m,int a);
Fecha(const Fecha &f);
~Fecha();
bool setDia(int d);
bool setMes(int m);
bool setAnyo(int a);
int getDia()const;
int getMes()const;
int getAnyo()const;
bool isBisiesto()const;
void Imprimir(ostream &os)const;
string toString()const;
int getDiferenciaAnyos(const Fecha &f)const;
};