Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date and Time Formatting

Posted on 1998-07-08
2
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 400 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

Technology Partners: 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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 how to use strings and some functions related to them in the C programming language.

618 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