Solved

Date and Time Formatting

Posted on 1998-07-08
2
168 Views
Last Modified: 2010-04-15
Hi, I'm using UNIX C.
I need to get the current date and time printed out in this format :

DD/MMM/YYYY:HH:MM:SS +ZZZZ

e.g. 18/FEB/1998:15:07:43 +0500

I'm new to C and am pretty confused with what to use to achieve this. Please help ? Thanks!!

0
Comment
Question by:totoro030898
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
ID: 1251569
Basically you can use time() to get the current time, localtime() to get the local time, gmtime() to convert the time to a structure, in this structure you will have all the information you need such as month, day, hour etc.  Then it is a matter of using sprintf to get the format you need.  By comparing localtime() with time(), you can get the last field.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1251570
#include <time.h>
char s[30];
time_t t=time(NULL);
strftime(s,sizeof(s),"%d/%b/%Y:%T %Z",localtime(&t));
printf("%s\n",s);
/*or*/
char *mon[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
extern time_t timezone;
time_t t=time(NULL);
struct tm *tm=localtime(&t);
printf("%2d/%s/%d:%02d:%02d:%02d +05d\n",
       tm->tm_mday,mon[tm->tm_mon],tm->tm_year+1900,
      tm->tm_hour,tm->tm_min,tm->tm_sec,timezone/3600*100+timezone/60%60);
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now