Solved

Need to get system date/time

Posted on 1998-05-15
3
767 Views
Last Modified: 2013-12-26
I need to get the system date and time in 'C' (ansi).  

In Windows, I use the following code to do so:
_strtime(time_string);
time_string[2] = '\0';
time_string[5] = '\0'
hours = atoi(&time_string[0]);
minutes = atoi(&time_string[3]);

_strdate(date_string);
date_string[2] = '\0';
date_string[5] = '\0';
date_string[8] = '\0';
month = atoi(&date_string[0]);
day = atoi(&date_string[3]);
year = atoi(&date_string[6]);

This code will not compile in redHat linux 5.0.  I believe this is faily primitive code
though. What I would actually like, is to get the time in a string like so: "9:45 AM",
and the date like so: "January 25, 1998"  Is there a way to easily do this in Linux/Unix??

What about calling the Tzset() function -  Is this necessary??  when I do a `date` in the
unix environment, it seems correct.


0
Comment
Question by:cwilliso
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1293433
#include <time.h>
time_t t[1];
char *date;
*t = time(0);
date=asctime(localtime(t));

0
 
LVL 2

Accepted Solution

by:
António Sargento earned 100 total points
ID: 1293434
You have the function time() that return the system time, i.e., the number of seconds from January 1, 1970.
You could use this function like this:

long t=time((time_t*)0);

Do not forget to include the time.h

Then with this value you could convert it to any format that you want.
Exist several functions and I will enumerate a few:

char *ctime(long /* the return value of time() */);
output: Fri May 15 23:52:00 1998

You could use also the functions gmttime, localtime asctime. These functions return
a struct (struct tm) that has all the information associated to a date (week day, month, etc).

You could check the time.h and see the manual pages of these functions for further details.



0
 
LVL 84

Expert Comment

by:ozo
ID: 1293435
#include <time.h>
time_t t[1];
struct tm *tm;
int hours,minutes,month,day,year,dst;
*t = time(0);
tm=localtime(t);
hours = tm->tm_hour;
minutes = tm->tm_min;
month = tm->tm_mon+1;
day = tm->tm_mday;
year = tm->tm_year+1900;
dst = tm->tm_isdst;

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 114
repeatEnd java challenge 42 85
Add content to output file 4 67
firstChar challenge 13 121
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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