Solved

Date and Time Formatting

Posted on 1998-07-08
2
167 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 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

11 Experts available now in Live!

Get 1:1 Help Now