471,092 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

How to show Unix time in C?

I'm trying to show current Unix time in C.

Is it possible?

Nov 14 '05 #1
5 35203
On Fri, 01 Apr 2005 17:46:00 +0200, <fr**********@europe.com> wrote:
I'm trying to show current Unix time in C.
Is it possible?


It depends on what you mean by "Unix time".

#include <stdio.h>
#include <time.h>

int main()
{
time_t t = time(0);
printf(ctime(&t));
return 0;
}

Jirka
Nov 14 '05 #2

Jirka Klaue wrote:
On Fri, 01 Apr 2005 17:46:00 +0200, <fr**********@europe.com> wrote:
I'm trying to show current Unix time in C.
Is it possible?


It depends on what you mean by "Unix time".

Isn't the Unix Time a number of seconds since January 1st, 1970?

Nov 14 '05 #3
<posted & mailed>

fr**********@europe.com wrote:
I'm trying to show current Unix time in C.

Is it possible?


The following creates the time structure you need, and populates its
elements with the current system values.

struct timeval tv;
gettimeofday(&tv, NULL);

Now, you can printf any elements you choose:
printf("Sec: %d uSec: %d\n", tv.tv_sec, tv.tv_usec)

That should do. Just look up the manpage for any other elements, etc.

--
Tom Cameron
tom<at>drdabbles<dot>us
http://drdabbles.us
Nov 14 '05 #4
fr**********@europe.com writes:
I'm trying to show current Unix time in C.

Is it possible?


If by "Unix time", you mean a raw time_t value, there's not a whole
lot you can do with it portably. The standard only guarantees that
it's an arithmetic type capable of representing times.

The following might do something useful:

#include <stdio.h>
#include <time.h>
int main()
{
printf("%ld\n", (long)time(NULL));
return 0;
}

If time_t is a floating-point type whose value is always between 0.0
and 1.0, you won't get much useful information from this. If it's,
say, an integer type representing the whole number of seconds since
1970-01-01 00:00:00 GMT, the result might be meaningful (as I write
this, it's 1112465716). <OT>The latter happens to be the
representation of time_t on Unix and some other systems, but of course
such implementation details are off-topic here.</OT>

If you want to display a legible and portable representation of the
current time, you can use something like this:

#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
time(&now);
fputs(ctime(&now), stdout);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
hi

this would help u

#include <time.h>
#include <stdio.h>

#define SIZE 256

int
main (void)
{
char buffer[SIZE];
time_t curtime;
struct tm *loctime;

/* Get the current time. */
curtime = time (NULL);

/* Convert it to local time representation. */
loctime = localtime (&curtime);

/* Print out the date and time in the standard format. */
fputs (asctime (loctime), stdout);

/* Print it out in a nice format. */
strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
fputs (buffer, stdout);
strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
fputs (buffer, stdout);

return 0;
}

It produces output like this

Wed Jul 31 13:02:36 1991
Today is Wednesday, July 31.
The time is 01:02 PM.
<fr**********@europe.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I'm trying to show current Unix time in C.

Is it possible?

Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tom | last post: by
2 posts views Thread by Luis P. Mendes | last post: by
37 posts views Thread by David T. Ashley | last post: by
3 posts views Thread by smitanaik | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.