473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
20 3575
gpraghuram
1,275 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
JosAH
11,448 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
1,275 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
JosAH
11,448 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
JosAH
11,448 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
JosAH
11,448 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
32,556 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
mac11
256 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
14,534 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
32,556 Expert Mod 16PB
Please remember always to use the [ CODE ] tags provided when posting code.
Jun 11 '08 #17
Banfa
9,065 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
9,065 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
32,556 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

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

Similar topics

4
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
9
by: mistral | last post by:
Need help to remove list of days from date script. Need format "June 07, 2006" <SCRIPT LANGUAGE="JavaScript"> <!-- Begin // Get today's current date. var now = new Date();
3
by: Stephen | last post by:
Hi, I have a table consists which cosists of batch numbers, and assosiated dates and times..ie the columns are batch, date and time. The data within the table is not in any particular order. ...
0
by: kumarkanth | last post by:
My question is how do I manipulate the procedure to calculate cut-off date with in working days and it should skip weekends and holidays? I have holiday table to determine holidays Table Name :...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
11
by: Connie via AccessMonster.com | last post by:
Hi Access Building Friends, I am building a database for a manufacturer who needs to know the projected End_Date of each job. I know the Start_Date and the total days required to do the job. ...
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.