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
20 3575
You should use clock tick to do this.
What code u have written for this?
Raghu
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
On your unix/linux/whatever system type in 'man ctime' and read all about it.
kind regards,
Jos
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
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
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(¤t));
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
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
#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
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
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
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
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.
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.
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
- int main()
-
{
-
printf("\n Date 6 days : %s\n", functionName());
-
//Here u will get in full string format then u parce it and use the info what ever required.
-
}// function last 6 days
-
char *functionName()
-
{
-
char Date6[11]=NULL;
-
time_t current,c6;
-
struct tm *tm,*now;
-
current = time(NULL);
-
c6 = current - (6 * 3600 *24);
-
now = localtime(&c6);
-
-
//printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
-
//printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
-
snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
-
return Date6;
-
}
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
NeoPa 32,556
Expert Mod 16PB
Please remember always to use the [ CODE ] tags provided when posting code.
Banfa 9,065
Expert Mod 8TB -
char *functionName()
-
{
-
char Date6[11]=NULL;
-
time_t current,c6;
-
struct tm *tm,*now;
-
current = time(NULL);
-
c6 = current - (6 * 3600 *24);
-
now = localtime(&c6);
-
-
//printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
-
//printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
-
snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
-
return Date6;
-
}
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.
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... - main()
-
{
-
char dateRequired [11];
-
dateRequired= functionName()
-
-
printf("date is %s" , dateRequired);
-
}
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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();
|
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.
...
|
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 :...
|
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...
|
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.
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |