Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Date conversion in C

Posted on 2005-03-15
10
Medium Priority
?
217 Views
Last Modified: 2010-04-15
Dear Experts
I would like to have a code which would convert a dateformat from yyyymmdd to mmddyyyy
i.e 20050315 to 03152005

Regards
Ronan
0
Comment
Question by:ronan_40060
  • 5
  • 5
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13546189
Hi ronan_40060,

Here's some example code:

char OldTimeFormat[] = "20050315";
char NewTimeFormat[32];

strcpy(NewTimeFormat, OldTimeFormat+4);
strncat(NewTimeFormat, OldTimeFormat, 4);


That's it.  NewTimeFormat variable should have the new format.

You'll of course want to add test in above code for length of the source string.

David Maisonave :-)
Cheers!
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546424
Hello Axter
sorry  I did not get what you meant by to add test in above code for length of the source string

pls can u explain that
thank you very much
Ronan
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546458
You want to add extra logic to make sure the source string is the minimum length.
Example:
if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
}
else
{
   //Report error, or handle error here!!!
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 13546498
If OldTimeFormat is an empty string, or if it's smaller then 5 characters, then copying pass it's length will result in undefined behavior.

If your source string is a pointer, then you'll also want to make sure the pointer is not NULL.
Example:

bool ConvertTime(const char*OldTimeFormat, char*NewTimeFormat)
{
     if (OldTimeFormat && strlen(OldTimeFormat) > 4) //Could also add NULL test for NewTimeFormat
     {
        strcpy(NewTimeFormat, OldTimeFormat+4);
        strncat(NewTimeFormat, OldTimeFormat, 4);
     }
     else
     {
        //Report error, or handle error here!!!
        return false;
     }
     return true;
}
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546595
Axter here is the code . just one help
in printf statement what has to be entered ?


int main ()

{

char OldTimeFormat[] = "20050315";
char NewTimeFormat[32];

//strcpy(NewTimeFormat, OldTimeFormat+4);
//strncat(NewTimeFormat, OldTimeFormat, 4);

if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
   printf("NewTimeFormat\n");      
}
else
{
   printf("Error reported !!!");
}

}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546627
Example:

printf("%s\n", NewTimeFormat);    
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546888
Axter Thanks a lot
regards
Ronan
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546936
Axter a bit explanation needed
char NewTimeFormat[32];
why 32
and also
in the following
if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
   printf("NewTimeFormat\n");    

why used 4
thank you
Ronan
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546981
>>why 32

Just a common number I used.
You should use the largest size you expect your buffer to be, plus 1.
In your case that should be 9
char NewTimeFormat[9];

I myself always like to throw extra padding for good measure.

>>why used 4
Because you're copying after the forth position in your source string.

01234567  (zero based index into your string)
20050315
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13547193
Great Axter
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question