Reputation: 45
I have a bit of code for calculating when did the previous full moon happen.
int humanreadable(seconds){
time_t now=seconds;
struct tm ts;
char buf[80];
ts = *localtime(&now);
strftime(buf, sizeof(buf), "%Y %m %d", &ts);
printf("%s\n", buf); // Works correctly, returns "2016 12 14", not needed, only used to for a check
int prevY = &ts.tm_year+1900;
int prevM = &ts.tm_mon+1;
int prevD = &ts.tm_mday;
printf("%d %d %d\n", prevY,prevM,prevD); // Does not work, returns random numbers
//later add return prevY/prevM/prevD pointers so I can pass them on
return 0;
}
int prevFullMoon ( int y, int m, int d){ // int * prevY, int * prevM, int * prevD){//temporarely disabled pointers, later need to add them back so assert in main works
if(valid_date(y,m,d)==INVALID_DATE){
return valid_date(y,m,d);
}
int tmp=epoch(y,m,d);
while (isFullMoonEpochCheck(tmp)!=1) {
tmp=tmp-ONE_DAY;
}
humanreadable(tmp);
return 1;
}
int main ( int argc, char * argv [] )
{
prevFullMoon( 2017, 1, 11); //temporary code to check what it is returning
//assert ( prevFullMoon ( 2017, 1, 11, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 ); //this needs to pass
}
I have a problem that my "humanreadable" function does not print the year, month and day i need it to. When I print the "buf" variable, it returns the date correctly but I need the date in three separete variables so I can pass them on from "prevFullMoon" function.
If you read my comments in the code you should get the idea of what is happening and what I am trying to achieve.
I am pretty lost when it comes to structures and pointers so any help is appreciated.
PS. In this thread I am mainly want to know why my time conversion is not working but if you are willing to help me out with the pointers and passing the prevY,prevM and prevD, I would appreciate that.
Thank you
Upvotes: 0
Views: 50
Reputation: 41017
Maybe there are more issues because you didn show the implementation of validDate
, but start fixing those problems adding to an address:
int prevY = &ts.tm_year+1900;
int prevM = &ts.tm_mon+1;
int prevD = &ts.tm_mday;
should be
int prevY = ts.tm_year+1900;
int prevM = ts.tm_mon+1;
int prevD = ts.tm_mday;
Upvotes: 2