473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

abt time functions

SSG
Hi All!
I want to know how to display the system time without
using--system("date").
can anyone give me the idea to handle this operation.

By
S.S.G

Nov 15 '05 #1
21 1460
hi ssg,
this is program using display date and time ..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
// system("date");
time_t rt;
struct tm * ti; // this structure have all int variable of tm
members... like tm_hour
char *p;
time (&rt); // this one return 0:0:0 gst 1979
ti = localtime ( &rt ); // get local time store to tm structure
printf ( "Current date and time are: %s", asctime (ti) );
//converting date structure to string
printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
using that structure
}

By
Sree Krishna
TooMuch Semiconductors Solutions
INDIA

Nov 15 '05 #2
chellappa wrote:
hi ssg,
this is program using display date and time ..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
// system("date");
time_t rt;
struct tm * ti; // this structure have all int variable of tm
members... like tm_hour char *p;
What is this doing here?
time (&rt); // this one return 0:0:0 gst 1979
Huh?
No check for error here.
ti = localtime ( &rt ); // get local time store to tm structure
No check for error.
printf ( "Current date and time are: %s", asctime (ti) );
You need a \n at the end of your format string.
//converting date structure to string
printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
Again, you need a \n at the end of the format string.
Why the double colons?
using that structure
}


Robert Gamble

Nov 15 '05 #3
SSG wrote:
Hi All!
I want to know how to display the system time without
using--system("date").
can anyone give me the idea to handle this operation.


Please get an elementary C textbook. Use the one in the library if you
can't afford one.

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

int main(void)
{
time_t now = time(0);
printf("The time is %s", ctime(&now));
return 0;
}

Nov 15 '05 #4
chellappa wrote on 06/07/05 :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
// system("date");
time_t rt;
struct tm * ti; // this structure have all int variable of tm
members... like tm_hour
char *p;
time (&rt); // this one return 0:0:0 gst 1979
ti = localtime ( &rt ); // get local time store to tm structure
printf ( "Current date and time are: %s", asctime (ti) );
//converting date structure to string
printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
using that structure
}


A little enhancement...

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

int main (void)
{
/* this structure have all int variable of tm members... like
tm_hour */
struct tm ti;
time_t rt;

time (&rt);

/* get local time store to tm structure */
ti = *localtime (&rt);

printf ("Current date and time are: %s", asctime (&ti));

/* converting date structure to string
simply using that structure
*/
printf ("%02d:%02d:%02d\n", ti.tm_hour, ti.tm_min, ti.tm_sec);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 15 '05 #5
Robert Gamble wrote:
chellappa wrote:
printf ( "Current date and time are: %s", asctime (ti) );


You need a \n at the end of your format string.


Only if the output is to be double-spaced. The
string returned by asctime() has a '\n' at the end.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #6
Eric Sosman wrote:
Robert Gamble wrote:
chellappa wrote:
printf ( "Current date and time are: %s", asctime (ti) );


You need a \n at the end of your format string.


Only if the output is to be double-spaced. The
string returned by asctime() has a '\n' at the end.


That's me not engaging my brain ;)

Robert Gamble

Nov 15 '05 #7

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:gM*******************@newsread2.news.atl.eart hlink.net...
SSG wrote:
Hi All!
I want to know how to display the system time without
using--system("date").
can anyone give me the idea to handle this operation.
Please get an elementary C textbook. Use the one in the library if you
can't afford one.


Why must one even travel to a library? Why are not older editions of K&R
available online?

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

int main(void)
{
time_t now = time(0);
printf("The time is %s", ctime(&now));
return 0;
}

Nov 15 '05 #8
"Stephen Mayes" writes:
Please get an elementary C textbook. Use the one in the library if you
can't afford one.


Why must one even travel to a library? Why are not older editions of K&R
available online?


Copyrights can last a long time in the US. Mickey Mouse is still covered by
copyright and he goes back to 1928 or so. Complain to your congressman if
you think copyrights last too long. Otherwise come back in a hundred years.
The current edition of K&R is copyrighted 1988. The version prior to that
is hardly useful to write programs in the current environment. Not that any
of that makes any difference WRT your question.
Nov 15 '05 #9
Stephen Mayes wrote on 06/07/05 :
Please get an elementary C textbook. Use the one in the library if you
can't afford one.


Why must one even travel to a library? Why are not older editions of K&R
available online?

This a legal on-line C-book:

http://publications.gbdirect.co.uk/c_book/

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 15 '05 #10
Stephen Mayes wrote:
Why must one even travel to a library? Why are not older editions of K&R
available online?


In this newsgroup, very few of us would suggest that you engage in
illegal behavior. Don't tell us if you do find and use such an illegal
copy; some people here have been known to report these violations of law.
Nov 15 '05 #11
On Wed, 6 Jul 2005 13:58:33 -0700, in comp.lang.c , "osmium"
<r1********@comcast.net> wrote:
"Stephen Mayes" writes:
Please get an elementary C textbook. Use the one in the library if you
can't afford one.


Why must one even travel to a library? Why are not older editions of K&R
available online?


Copyrights can last a long time in the US.


Not just in the US - the Berne Convention requires fifty years after
the owner's death.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12
Just thought I'd mention a couple things...
time (&rt);
Since time() returns -1 on failure, we need to do some error checking:

time_t rt;

if ((rt = time(NULL)) == -1) {
perror("time()");
exit(EXIT_FAILURE);
}
ti = *localtime (&rt);


Again, some error checking is needed here. Since localtime() returns NULL on
failure, it's possible that your code might dereference a NULL pointer.

A better function should be available, localtime_r(), which allows easier error
checking and is also threadsafe.

Cheers.

--

# /dev/geek
Nov 15 '05 #13
/dev/geek wrote:
Just thought I'd mention a couple things...
time (&rt);

Since time() returns -1 on failure, we need to do some error checking:

time_t rt;

if ((rt = time(NULL)) == -1) {


It will probably never make any difference, but the
pedant-approved way to write this test is

if ((rt = time(NULL)) == (time_t)-1) {

Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type; in C99 I suppose it could be a complex type. (Hawking
has argued on cosmological grounds that time is complex.)

Now, suppose `time_t' is an unsigned integer type that
is narrower than `int' -- `unsigned char' or `unsigned short'
or one of C99's extended types, say. Then if time() fails,
rt will have a positive narrower-than-`int' value (it will
be sometype_MAX), and this value will remain positive after
being promoted for comparison with the `int' value -1. The
comparison will report "unequal" and the failure will go
undetected.

Admittedly, this is all pretty far-fetched. The original
reason time() took an argument was that the "returned" value
was too large for the 16-bit `int' of the era and `long' hadn't
been invented yet, so it's unlikely that you'll ever find a
`time_t' that's narrower than `int'. Indeed, some systems are
beginning to adopt a `time_t' that's wider than `long'! Still,
there's always the remote possibility that your code might need
to run on the DeathStation 9000, where `time_t' occupies three
eleven-bit bytes and an `int' occupies five ...

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #14
> It will probably never make any difference, but the
pedant-approved way to write this test is


Aye, I purposely left that off. :-) My intent was to show a basic error
checking method, not to be pedantic.

Cheers.

--

# /dev/geek
Nov 15 '05 #15


Eric Sosman wrote:
/dev/geek wrote:
Just thought I'd mention a couple things...
time (&rt);

Since time() returns -1 on failure, we need to do some error checking:

time_t rt;

if ((rt = time(NULL)) == -1) {


It will probably never make any difference, but the
pedant-approved way to write this test is

if ((rt = time(NULL)) == (time_t)-1) {

Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type;


In which case, the above pedant-approved line is no longer much
pedant-approved.Ain't it so?
in C99 I suppose it could be a complex type. (Hawking
has argued on cosmological grounds that time is complex.)

[snip]

Nov 15 '05 #16
"Suman" <sk*****@gmail.com> writes:
Eric Sosman wrote:

[...]
It will probably never make any difference, but the
pedant-approved way to write this test is

if ((rt = time(NULL)) == (time_t)-1) {

Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type;


In which case, the above pedant-approved line is no longer much
pedant-approved.Ain't it so?


Why not? time() returns (time_t)-1 on error; if it's floating-point,
that's -1.0. Unless the floating-point subsystem is so badly screwed
up that -1.0 != -1.0, the above line should be safe.

--
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 15 '05 #17


Keith Thompson wrote:
"Suman" <sk*****@gmail.com> writes:
Eric Sosman wrote:

[...]
It will probably never make any difference, but the
pedant-approved way to write this test is

if ((rt = time(NULL)) == (time_t)-1) {

Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type;


In which case, the above pedant-approved line is no longer much
pedant-approved.Ain't it so?


Why not? time() returns (time_t)-1 on error; if it's floating-point,
that's -1.0. Unless the floating-point subsystem is so badly screwed
up that -1.0 != -1.0, the above line should be safe.

Because direct comparison on floating point numbers are almost always
best avoided. Or, that is what I thought was the right thing to do in
general.

Nov 15 '05 #18
"Suman" <sk*****@gmail.com> wrote:
Keith Thompson wrote:
"Suman" <sk*****@gmail.com> writes:
Eric Sosman wrote:
> Here's why: We do not actually know what hides behind
> the `time_t' mask; all we know is that it is an arithmetic
> type capable of representing times. "Arithmetic type" could
> be any signed or unsigned integer type or even a floating-point
> type;

In which case, the above pedant-approved line is no longer much
pedant-approved.Ain't it so?


Why not? time() returns (time_t)-1 on error; if it's floating-point,
that's -1.0. Unless the floating-point subsystem is so badly screwed
up that -1.0 != -1.0, the above line should be safe.

Because direct comparison on floating point numbers are almost always
best avoided. Or, that is what I thought was the right thing to do in
general.


It _is_ true in general. In this specific case, though, the Standard
speaks literally of (time_t)(-1), and -1.0 must be exactly representable
in any kind of float, so this is safe.

Richard
Nov 15 '05 #19


Richard Bos wrote:
"Suman" <sk*****@gmail.com> wrote:
Keith Thompson wrote:
"Suman" <sk*****@gmail.com> writes:
> Eric Sosman wrote:
>> Here's why: We do not actually know what hides behind
>> the `time_t' mask; all we know is that it is an arithmetic
>> type capable of representing times. "Arithmetic type" could
>> be any signed or unsigned integer type or even a floating-point
>> type;
>
> In which case, the above pedant-approved line is no longer much
> pedant-approved.Ain't it so?

Why not? time() returns (time_t)-1 on error; if it's floating-point,
that's -1.0. Unless the floating-point subsystem is so badly screwed
up that -1.0 != -1.0, the above line should be safe. Because direct comparison on floating point numbers are almost always
best avoided. Or, that is what I thought was the right thing to do in
general.


It _is_ true in general. In this specific case, though, the Standard
speaks literally of (time_t)(-1),

I am not too sure I follow you, can you be more specific? and -1.0 must be exactly representable
in any kind of float, so this is safe.

Aye, aye! Sir!

Nov 15 '05 #20
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
It _is_ true in general. In this specific case, though, the Standard
speaks literally of (time_t)(-1),

I am not too sure I follow you, can you be more specific?


The only way I could is by quoting C&V: 7.23.2.4#3, in n869.txt.

Richard
Nov 15 '05 #21


Richard Bos wrote:
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
It _is_ true in general. In this specific case, though, the Standard
speaks literally of (time_t)(-1),

I am not too sure I follow you, can you be more specific?


The only way I could is by quoting C&V: 7.23.2.4#3, in n869.txt.

That's a lot of help. Will check. Thanks.

Nov 15 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rick | last post by:
Hello all. I have an index.php file that has a lot of functions that I wrote. Let's say for the sake of argument that there are 1000 functions of 100 lines each. The index.php file is invoked...
18
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
11
by: Bryant Huang | last post by:
Hello! I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in...
3
by: Luis Pedro | last post by:
Hi there, Can anyone tell me if there is any time and date standard functions for C++ and, if so, were can I find documentation about it? If such functions do not exist, any hint of what library...
17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
5
by: RICHARD BROMBERG | last post by:
I am writing an ASP program that includes a Form. When the Form is submitted I use the Date() and Time() functions to put the date and time into the Body part of the e-mail. The time reported is...
5
by: rs | last post by:
I have a table with a timestamp field which contains the date and time. ie. 9/13/2004 9:10:00 AM. I would like to split this field into 2 fields, one with just the DATE portion ie 9/13/2004 and...
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
0
hqprog
by: hqprog | last post by:
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard. I need to use these two functions in myprog.c (on pc - ...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.