Connecting Tech Pros Worldwide Forums | Help | Site Map

How to show Unix time in C?

francescomoi@europe.com
Guest
 
Posts: n/a
#1: Nov 14 '05
I'm trying to show current Unix time in C.

Is it possible?


Jirka Klaue
Guest
 
Posts: n/a
#2: Nov 14 '05

re: How to show Unix time in C?


On Fri, 01 Apr 2005 17:46:00 +0200, <francescomoi@europe.com> wrote:
[color=blue]
> I'm trying to show current Unix time in C.
> Is it possible?[/color]

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
michal.stankoviansky@gmail.com
Guest
 
Posts: n/a
#3: Nov 14 '05

re: How to show Unix time in C?



Jirka Klaue wrote:[color=blue]
> On Fri, 01 Apr 2005 17:46:00 +0200, <francescomoi@europe.com> wrote:
>[color=green]
> > I'm trying to show current Unix time in C.
> > Is it possible?[/color]
>
> It depends on what you mean by "Unix time".[/color]


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

Thomas Cameron
Guest
 
Posts: n/a
#4: Nov 14 '05

re: How to show Unix time in C?


<posted & mailed>

francescomoi@europe.com wrote:
[color=blue]
> I'm trying to show current Unix time in C.
>
> Is it possible?[/color]

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
Keith Thompson
Guest
 
Posts: n/a
#5: Nov 14 '05

re: How to show Unix time in C?


francescomoi@europe.com writes:[color=blue]
> I'm trying to show current Unix time in C.
>
> Is it possible?[/color]

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) kst-u@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.
invincible
Guest
 
Posts: n/a
#6: Nov 14 '05

re: How to show Unix time in C?


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.
<francescomoi@europe.com> wrote in message
news:1112370360.567800.119770@l41g2000cwc.googlegr oups.com...[color=blue]
> I'm trying to show current Unix time in C.
>
> Is it possible?
>[/color]


Closed Thread