Solved

Date and Time Formatting

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

809 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