# Yesterday's date with C



## florins (Aug 18, 2010)

Hello FreeBSD Folks,

I want to get yesterday's date with C. I have this code below:

```
#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:

```
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.


----------



## graudeejs (Aug 18, 2010)

strftime(3)


----------



## florins (Aug 18, 2010)

killasmurf86 said:
			
		

> strftime(3)



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


```
#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;
}
```


----------



## graudeejs (Aug 18, 2010)

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


```
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]


----------



## mickey (Aug 18, 2010)

```
#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;
}
```


----------



## graudeejs (Aug 18, 2010)

mickey said:
			
		

> ```
> #include <stdio.h>
> #include <time.h>
> 
> ...



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


----------



## lme@ (Aug 18, 2010)

mickey said:
			
		

> ```
> time_t yesterday = time(NULL) - 0x15180;
> ```



Great, you could also write

```
time_t fjkefehje=time(NULL)-00250600;
```
so you cannot read it the next time to read your code.


----------



## expl (Aug 18, 2010)

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.


----------



## mickey (Aug 18, 2010)

lme@ said:
			
		

> Great, you could also write
> 
> ```
> time_t fjkefehje=time(NULL)-00250600;
> ...



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

When I see it, I know that it means a day. And I hate nothing more than _computing_ the obvious, like in writing:

```
24 * 60 * 60
```


----------



## graudeejs (Aug 18, 2010)

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
> 
> When I see it, I know that it means a day. And I hate nothing more than _computing_ the obvious, like in writing:
> 
> ...



Normally that should be done by preprocessor. So no slowdowns


----------



## mickey (Aug 18, 2010)

killasmurf86 said:
			
		

> Normally that should be done by preprocessor. So no slowdowns



I know, but in what way is the term

```
24 * 60 * 60
```
more informative than

```
0x15180
```
or

```
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:

```
#define DAY_SECONDS 0x15180
time_t yesterday = time(NULL) - DAY_SECONDS;
```


----------



## wblock@ (Aug 18, 2010)

mickey said:
			
		

> I know, but in what way is the term
> 
> ```
> 24 * 60 * 60
> ...



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...


----------



## mickey (Aug 19, 2010)

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?


----------



## wblock@ (Aug 19, 2010)

mickey said:
			
		

> Silly question... What do you put in your code, if you are in the need of a constant for the speed of light?



02167474112, obviously.


----------



## gordon@ (Aug 19, 2010)

Either mickey is Rainman or this is the best troll I've seen in a long time.


----------



## expl (Aug 19, 2010)

mickey said:
			
		

> And if this is about _readability_, than you would be better off writing the following anyways:
> 
> ```
> #define DAY_SECONDS 0x15180
> ...



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.


----------

