467,161 Members | 993 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

using system date calculate last 6 days in c language

i am in big problem
Under unix C Language
i want return date format like MM/DD/YYYY
this function should return above format .

using system date calculate last 6 days mean clearly i am explain

today date is :06/09/2008

the function return 06/03/2008

but when we write program automatic return the last 6 days date means 06/03/2008


please help .............

bye
madhu
Jun 9 '08 #1
  • viewed: 3277
Share:
20 Replies
gpraghuram
Expert 1GB
You should use clock tick to do this.
What code u have written for this?

Raghu
Jun 9 '08 #2
using c language (unix)

return should this format mm/dd/yyyy

calculate the last 6 days mean
using system date - 7 days so we get the last 6 days
there is no input here automatically get the return the function of last 6 days date
please help ,......
i want source code for this

please please


thanks
madhu







You should use clock tick to do this.
What code u have written for this?

Raghu
Jun 9 '08 #3
Expert 8TB
On your unix/linux/whatever system type in 'man ctime' and read all about it.

kind regards,

Jos
Jun 9 '08 #4
gpraghuram
Expert 1GB
We cant write code for you.
The logic u are asking is to some extent simple.
You should use localtime() and time() functions for this

Raghu
Jun 10 '08 #5
i am little bit confusion , code please help,


Regards,
Madhu



We cant write code for you.
The logic u are asking is to some extent simple.
You should use localtime() and time() functions for this

Raghu
Jun 10 '08 #6
i tryied below code.................

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

int main()
{

time_t current,c6;

current = time(NULL);
c6 = current - (6 * 3600 *24);
printf("\n Current Time : [%s]", ctime(&current));
printf("\n 6 Days before : [%s]", ctime(&c6));

return 0;
}

output give the last 6 days date


but using this code write the function and return the last 6 days date..

please help.......
Regards,
Madhu





i am little bit confusion , code please help,


Regards,
Madhu
Jun 10 '08 #7
Expert 8TB
but using this code write the function and return the last 6 days date..

please help.......
I'm sorry I don't understand your question. You have working code that produces
the correct output using the correct logic. What exactly do you want? I'm afraid
this is a little English language problem.

kind regards,

Jos
Jun 10 '08 #8
#include<stdio.h>
#include<time.h>
char *functionName();
int main()
{
printf("\n date - 6 days : [%s]" , functionName());
//Here u will get in full string format then u parce it and use the info what ever required.
}

char *functionName()
{
current = time(NULL);
c6 = current - (29 * 3600 *24);
return ctime(&c6);
}

output is ::::::::::::::
date - 6 days : [Wed Jun 4 17:09:40 2008]


i write the above code it returns the last 6 days date
so output convert into MM/DD/YYYY [06/04/2008]
please help ..............help

Regards,
Madhu





I'm sorry I don't understand your question. You have working code that produces
the correct output using the correct logic. What exactly do you want? I'm afraid
this is a little English language problem.

kind regards,

Jos
Jun 10 '08 #9
Expert 8TB
I'm sorry, I still don't understand what you want. I've asked the other moderators
to have a peek; maybe one of them can help you out.

kind regards,

Jos
Jun 10 '08 #10
please some one help


help................
bye
madhu


I'm sorry, I still don't understand what you want. I've asked the other moderators
to have a peek; maybe one of them can help you out.

kind regards,

Jos
Jun 10 '08 #11
Expert 8TB
please some one help


help................
bye
madhu
Ok, I'll try again; what should the function do? i.e. what are the input parameter(s)
supposed to be, what should the function compute and what should it return?
Please don't show us that same code again, try to write the requirements down
in English.

kind regards,

Jos
Jun 10 '08 #12
NeoPa
Expert Mod 16PB
I'm guessing that the OP is simply asking how to format the date in mm/dd/yyyy format (may well be wrong of course).

I would comment that this does seem very much like a homework question. I would warn against providing code and suggest simply instructing on how this is done.
Jun 10 '08 #13
100+
I'm guessing that the OP is simply asking how to format the date in mm/dd/yyyy format (may well be wrong of course).

I would comment that this does seem very much like a homework question. I would warn against providing code and suggest simply instructing on how this is done.
If all you (OP) need to do is change how the date is formatted you might want to look into the strftime function - it's sort of like a sprintf made just for time formatting. Look at your manpages or ask google for documentation.
Jun 10 '08 #14
MMcCarthy
Expert Mod 8TB
I would guess that there is a problem with the system date formula. (e.g.) if you try to get a date 6 days before 06/09/2008 then the user has a problem because it is returning 31/08/2008 I guess instead of 06/03/2008 which is what the user wants. The system is reading the date in dd/mm/yyyy format instead of mm/dd/yyyy format.

Mary
Jun 11 '08 #15
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    printf("\n Date  6 days : %s\n", functionName());
  4.    //Here u will get in full string format then u parce it and use the info what ever required.
  5. }// function last 6 days
  6. char *functionName()
  7. {
  8. char Date6[11]=NULL;
  9. time_t current,c6;
  10. struct tm      *tm,*now;
  11. current  = time(NULL);
  12. c6 = current - (6 * 3600 *24);
  13. now = localtime(&c6);
  14.  
  15. //printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
  16. //printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
  17. snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
  18. return Date6;
  19. }
i write the above code is working fine and returning also MM/DD/YYYY same format so, here one more issue i get when printf is uncomment date is return if printf is comment date is not returning .

i want comment the printf and return date to the main function.

please help
help........
bye
madhu
Jun 11 '08 #16
NeoPa
Expert Mod 16PB
Please remember always to use the [ CODE ] tags provided when posting code.
Jun 11 '08 #17
Banfa
Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. char *functionName()
  2. {
  3.     char Date6[11]=NULL;
  4.     time_t current,c6;
  5.     struct tm      *tm,*now;
  6.     current  = time(NULL);
  7.     c6 = current - (6 * 3600 *24);
  8.     now = localtime(&c6);
  9.  
  10.     //printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
  11.     //printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
  12.     snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
  13.     return Date6;
  14. }
The problem in this code is that you are returning data that resides on the stack, that is you return Date6 but Date6 is declared as a local variable of functionName. As soon as function name exits Date6 is returned to the stack.

This basically invokes undefined behaviour. Undefined behaviour is bad because once invoked your program will behave in a completely unpredictable manor. You have seen this, the function works with the printf statements in place and doesn't without them even though there is no logical reason why they should make a difference.

Never return data that is located on the stack.

You have 3 choices, the first, simple but somewhat more programming practice, is to make Date6 static (add the static keyword before it's declaration. This will then place Data6 in the global data segment and it will always exist so you wont have a problem returning it. However this method is not re-entrant and wont work on a multi-threaded system.

The second is somewhat more complex, allocate memory from the heap (using malloc) inside functionName and return that data. Because it is allocated from the heap it will continue to exist after the function exits but the calling code will have to remember to free the data once it has finished with it.

The third method is to pass a buffer (and a size of the buffer) into functionName from the calling code via a pointer and for functionName to put it's result directly into that buffer. The calling code then becomes responsible for allocating the memory (on stack, heap or globally) and freeing it if necessary once used.

Personally I would used the third method, the first method doesn't work properly except in very simple circumstances, and I do not like allocating memory in a function at a lower level for release by a function at a higher level in a different part of the code. It tends to lead to memory leaks through thoughtless programming (i.e. forgetting to do the free) so that rules out method 2.


And finally functionName is an extremely poor name for a function, the name of a function should give an idea of what it does.
Jun 11 '08 #18
thanks very much ,i followed first one is working fine getting result into the main function...............................
i want one more issue after result get in the main function how can hold the varibale...
Expand|Select|Wrap|Line Numbers
  1. main() 
  2. char     dateRequired [11]; 
  3. dateRequired= functionName()
  4.  
  5. printf("date is %s" , dateRequired);
  6. }
i try above code but result is not storing in the dateRequired variable....
how can hold result the in the variable in the main function....

please help............
Regards,
Madhu
Jun 11 '08 #19
Banfa
Expert Mod 8TB
i try above code but result is not storing in the dateRequired variable....
how can hold result the in the variable in the main function....
I would be rather surprised if the code you posted compiles (it certainly doesn't for me) since dateRequired is not an l-value (simply put can not appear of the left hand side of an assignment).

If you want the data in the calling function then I suggest you implement option 3 from my previous post that returns the data to the previous function.

However you could try reading this.
Jun 11 '08 #20
NeoPa
Expert Mod 16PB
Please remember always to use the [ CODE ] tags provided when posting code.
Madhu,

This message is for you!

If I have to say this a third time I will make the warning an official (and logged) one.

ADMIN.
Jun 12 '08 #21

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Lynn | last post: by
11 posts views Thread by Connie via AccessMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.