Yesterday's date with C

Hello FreeBSD Folks,

I want to get yesterday's date with C. I have this code below:
Code:
#include <stdio.h>
#include <time.h>

int main()
{
  time_t today, yesterday;
  today = time(NULL);
  printf("Today's date is : %s", ctime(&today));
  yesterday = today - 24*60*60;
  printf("Yesterday's date was : %s", ctime(&yesterday));
}

Output of this code is:
Code:
Today's date is : Wed Aug 18 06:31:21 2010
Yesterday's date was : Tue Aug 17 06:31:21 2010
My question is how i can get the date format as YearMonthDay (sample; 20100818)
Thanks for your help.
 
killasmurf86 said:

Thanks. I have this code below but still dont know how to get back one day? Can anyone help me specially?

Code:
#include <stdio.h>
#include <time.h>

int main()
{
  time_t now;
  struct tm * timeinfo;
  char today [10];

  time( &now );
  timeinfo = localtime ( &now );

  strftime (today, 10, "%Y%m%d", timeinfo);
  printf("%s", today);

return 0;
}
 
modify tm, subtract 1 from tm->tm_day
note, that you should then check month, and year
so it is getting ugly

as much simpler and flexible would be to

Code:
char today [10];
time_t tt = time(NULL)-3600*24;
struct tm *stm = localtime(&tt);
strftime (today, 10, "%Y%m%d", stm);

something like this [haven't tested, no time]
 
Code:
#include <stdio.h>
#include <time.h>

int main(int argc, char *argv[])
{
        time_t yesterday = time(NULL) - 0x15180;
        char dt[16];

        strftime(dt, sizeof(dt), "%Y%m%d", localtime(&yesterday));
        printf("Yesterday was: %s\n", dt);

        return 0;
}
 
mickey said:
Code:
#include <stdio.h>
#include <time.h>

int main(int argc, char *argv[])
{
        time_t yesterday = time(NULL) - 0x15180;
        char dt[16];

        strftime(dt, sizeof(dt), "%Y%m%d", localtime(&yesterday));
        printf("Yesterday was: %s\n", dt);

        return 0;
}

Obviously I haven't been writing C code for some time :D
Won't sizeof(dt) return size of pointer (bytes necessary to store memory address) :)
because dt is pointer to array of chars
 
mickey said:
Code:
time_t yesterday = time(NULL) - 0x15180;

Great, you could also write
Code:
time_t fjkefehje=time(NULL)-00250600;
so you cannot read it the next time to read your code. :)
 
killasmurf86 said:
Obviously I haven't been writing C code for some time
Won't sizeof(dt) return size of pointer (bytes necessary to store memory address)
because dt is pointer to array of chars

When sizeof is applied to an array, the result is the size in bytes of the array in memory.
 
lme@ said:
Great, you could also write
Code:
time_t fjkefehje=time(NULL)-00250600;
so you cannot read it the next time to read your code. :)

I guess that depends on how deeply burnt into your memory hex constants like the one above got over the time using them :p

When I see it, I know that it means a day. And I hate nothing more than computing the obvious, like in writing:
Code:
24 * 60 * 60
 
mickey said:
I guess that depends on how deeply burnt into your memory hex constants like the one above got over the time using them :p

When I see it, I know that it means a day. And I hate nothing more than computing the obvious, like in writing:
Code:
24 * 60 * 60

Normally that should be done by preprocessor. So no slowdowns
 
killasmurf86 said:
Normally that should be done by preprocessor. So no slowdowns

I know, but in what way is the term
Code:
24 * 60 * 60
more informative than
Code:
0x15180
or
Code:
86400
given the fact, that I know a day has 24 hours of 60 minutes of 60 seconds, which equals 86400 seconds or $15180 expressed in hex?

And if this is about readability, than you would be better off writing the following anyways:
Code:
#define DAY_SECONDS 0x15180
time_t yesterday = time(NULL) - DAY_SECONDS;
 
mickey said:
I know, but in what way is the term
Code:
24 * 60 * 60
more informative than
Code:
0x15180
or
Code:
86400
given the fact, that I know a day has 24 hours of 60 minutes of 60 seconds, which equals 86400 seconds or $15180 expressed in hex?

Because 0x15180 is a magic constant that looks--to the poor guy who inherits your code--like a bad pointer with a random memory address. Even single-celled organisms that are too small to wear watches can tell that 24 * 60 * 60 is some sort of time value. 86400 is weaker, but at least a recognizable value that's often used in code.

If you must use magic constants, at least put the calculation in a comment. Of course, then the magic constant changes but the comment doesn't, which is always fun. And that leads back to putting the calculation in the code...
 
wblock said:
Because 0x15180 is a magic constant that looks--to the poor guy who inherits your code--like a bad pointer with a random memory address.

The only magic thing about it is that it is expressed in hex, but I guess nowadays this is considered something like ... bad practice ?

Anyhow, I guess different programmers have a different view on things like this. In my personal view, this is a constant, which I do not calculate within the code, neither by preprocessor nor otherwise. For the readability, one may use named constants, which should make clear what the value represents.

Silly question... What do you put in your code, if you are in the need of a constant for the speed of light? ;)
 
mickey said:
And if this is about readability, than you would be better off writing the following anyways:
Code:
#define DAY_SECONDS 0x15180
time_t yesterday = time(NULL) - DAY_SECONDS;

If you want to make code more readable use simple thing like comments. Definitions are there to help you out in special cases.

By the way this thread is terrible.
 
Back
Top