Jano
Jano

Reputation: 45

Converting seconds (unix time) to human readable (to different variables)

I need to convert seconds (unix time) to human readable time.

I use this code:

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);
return 0;
}

It works pretty well but I have two problems with this.

First, I don't really understand how it works. This is a homework for school and we have not yet covered how structures work so it is pretty weird that I am using it. Now, this is not really a problem because out code gets checked by a computer, so as long as our code outputs the right thing, it does not matter how we get it.

Second (my actual problem). This functions outputs (prints) one string which contains the year, month and day (formated) but I need the y,m,d in three separate variables because I need to use them later for a different function. How can I achieve that.

My second function where I need to use the three variables:

int prevFullMoon ( int y, int m, int d, int * prevY, int * prevM, int * prevD)
{
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;
}
return tmp;
}

This function gets y,m,d from input (the main function uses assert to evaluate the function (that's how it gets checked by the computer)) and needs to output when the previous full moon happened (it need to output prevY, prevM, prevD). Currently it outputs the time but in seconds (unix time). Otherwise it works how it should.

PS. I am kinda on a time constraint here (only got a few days left) so at this point I just kinda need it to work, but any explanations (or pointers to explanations/teaching materials) are welcomed.

Thank you for your help


My new code that returns random numbers everytime I run the program:

int humanreadable(int seconds){
  int prevY,prevD,prevM;
  struct tm ts;
    time_t now=seconds;
    ts = *localtime(&now);
    prevY = &ts.tm_year+1900;
    prevM = &ts.tm_mon+1;
    prevD = &ts.tm_mday;
    printf("%d %d %d\n",prevY,prevM,prevD); //only temporary to find out what thea variables return //later need to pass it to prevFullMoon as pointers
    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);
}

int main ( int argc, char * argv [] )
{
  prevFullMoon( 2017, 1, 11);
  //assert ( prevFullMoon ( 2017, 1, 11, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 );
}

I have also added some comments that should explain what I need to achieve


Yeah, figured it out.

 prevY = &ts.tm_year+1900

Should have been

 prevY = ts.tm_year+1900

Thanks everybody

Upvotes: 0

Views: 132

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25276

1) how it works

The number of seconds is converted to a structure that contains elements for year, month, day etc. Next this structure is printed to a string buffer in a standard format. Then you print this string buffer.

2) how to get to year, month, day etc.

Well, that is this struct tm. localtime returns a pointer to a struct tm and you copy the data of that structure to your own ts. Now you need to access the fields. You do need to know about this, but I'll help you:

struct tm {    // definition of struct tm
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };

To pass the proper fields to your function prevFullMoon ( int y, int m, int d... do:

prevFullMoon ( ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday, ...

Upvotes: 1

Related Questions