?
Solved

Date and Time Formatting

Posted on 1998-07-08
2
Medium Priority
?
175 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

770 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