473,396 Members | 2,039 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,396 software developers and data experts.

current time

Hi

I need to print the current time.

I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);

But this doesn't work and prints some ugly numbers.

How is this done correctly?
Nov 13 '05 #1
18 36336
Tim Quon wrote:
I need to print the current time.

I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);


At least 6 errors in these 3 lines!

time_t mytime = time(0);
printf("time: %s\n", asctime(localtime(&mytime)));

Jirka

Nov 13 '05 #2
On Mon, 15 Sep 2003 13:04:40 +0200, Jirka Klaue
<jk****@ee.tu-berlin.de> wrote:
Tim Quon wrote:
I need to print the current time.

I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);


At least 6 errors in these 3 lines!

time_t mytime = time(0);
printf("time: %s\n", asctime(localtime(&mytime)));

Jirka


Thanks a lot.
Never trust examples from the web :')

Nov 13 '05 #3
Tim Quon <ti***@freesurf.ch> wrote:
I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);

But this doesn't work and prints some ugly numbers.

How is this done correctly?


No kiddin'. What is time()'s prototype? What is asctime()'s? (And note,
ascitime() doesn't exist...) In particular, what kind of parameters do
these functions take?

Richard
Nov 13 '05 #4
Tim Quon wrote:
Hi

I need to print the current time.

I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);

But this doesn't work and prints some ugly numbers.

How is this done correctly?


asctime() [note spelling] requires a pointer to a struct tm.
You could get such a pointer from localtime(), among others.
ctime() is probably the function you want, which takes a pointer to time_t
as its argument.
#include <time.h>
#include <stdio.h>

void shortime(void)
{
time_t mytime = time(0); /* not 'long' */
printf("time: %s", ctime(&mytime));
}


--
Martin Ambuhl

Nov 13 '05 #5


Tim Quon wrote:
Hi

I need to print the current time.

I found an example where this is done as followed:
long *mytime;
time(mytime);
printf("time: %s", ascitime(mytime);

But this doesn't work and prints some ugly numbers.

How is this done correctly?


No, not even close to being correct.
time_t mytime;
time(&mytime);
or
mytime = time(NULL);
And to print the date-time use function ctime.

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

int main(void)
{
time_t now;

if((now = time(NULL)) == (time_t)-1)
puts("Failure in getting time");
else
printf("The date and time is: %s",ctime(&now));
return 0;
}

--
--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #6
SW1
Well i think what you saw is the C time which counts the seconds since
January the first, 1970. There is a function in time.h which is called
localtime, it works like that:

time_t now; //time_t should be declared in time.h as long
struct tm *current; //pointer to array holding the current time

now = time(0); //current time in C representation
current = localtime(&now); //IMPORTANT you have to use a pointer to
time_t
printf("%i\n",current->tm_min); //Lets print the current minute

So now struct tm has money members:

struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};

I hope that helped
Nov 13 '05 #7
SW1 wrote:
Well i think what you saw is the C time which counts the seconds since
January the first, 1970. There is a function in time.h which is called
localtime, it works like that:
Not necessarily. For at least these reasons:
time_t (and clock_t) are arithmetic types capable of representing times.
There is no requirement about which arithmetic type, what the encoding is
(so linearity is not required), what the granularity is (so "seconds" are
not required), or what the base epoch is (so no particular date -- and
certainly not 1/1/1970 -- is specified).
time_t now; //time_t should be declared in time.h as long
Wrong. See above.

[ ... ]
I hope that helped


Only if he ignores your errors.

--
Martin Ambuhl

Nov 13 '05 #8
SW1
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<W5*******************@newsread1.news.atl.ear thlink.net>...
SW1 wrote:
Well i think what you saw is the C time which counts the seconds since
January the first, 1970. There is a function in time.h which is called
localtime, it works like that:


Not necessarily. For at least these reasons:
time_t (and clock_t) are arithmetic types capable of representing times.
There is no requirement about which arithmetic type, what the encoding is
(so linearity is not required), what the granularity is (so "seconds" are
not required), or what the base epoch is (so no particular date -- and
certainly not 1/1/1970 -- is specified).


Hmm, You might want to risk a look into time.h it says there:
typedef long time_t;
and if you call the time() function you get the SECONDS since 1/1/1970
passed to it.

SOO

now anyway the program would still run and print the current time,
even if I was wrong about time() and time_t.

THNX
Nov 13 '05 #9
sc**********@yahoo.de (SW1) wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<W5*******************@newsread1.news.atl.ear thlink.net>...
SW1 wrote:
> Well i think what you saw is the C time which counts the seconds since
> January the first, 1970. There is a function in time.h which is called
> localtime, it works like that:


Not necessarily. For at least these reasons:
time_t (and clock_t) are arithmetic types capable of representing times.
There is no requirement about which arithmetic type, what the encoding is
(so linearity is not required), what the granularity is (so "seconds" are
not required), or what the base epoch is (so no particular date -- and
certainly not 1/1/1970 -- is specified).


Hmm, You might want to risk a look into time.h it says there:
typedef long time_t;
and if you call the time() function you get the SECONDS since 1/1/1970
passed to it.

Not necessarily so, it actually depends on the implementation - lookup
the standard. Quote from n843:

7.23.2.5 The time function

[...]

Returns

[#3] The time function returns the implementation's best
approximation to the current calendar time. The value
(time_t)-1 is returned if the calendar time is not
available. If timer is not a null pointer, the return value
is also assigned to the object it points to.

<SNIP>

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #10
SW1 <sc**********@yahoo.de> wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<W5*******************@newsread1.news.atl.ear thlink.net>...
SW1 wrote:
> Well i think what you saw is the C time which counts the seconds since
> January the first, 1970. There is a function in time.h which is called
> localtime, it works like that:


Not necessarily. For at least these reasons:
time_t (and clock_t) are arithmetic types capable of representing times.
There is no requirement about which arithmetic type, what the encoding is
(so linearity is not required), what the granularity is (so "seconds" are
not required), or what the base epoch is (so no particular date -- and
certainly not 1/1/1970 -- is specified).


Hmm, You might want to risk a look into time.h it says there:


There is no one true time.h.

- Kevin.

Nov 13 '05 #11


SW1 wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<W5*******************@newsread1.news.atl.ear thlink.net>...
SW1 wrote:

Well i think what you saw is the C time which counts the seconds since
January the first, 1970. There is a function in time.h which is called
localtime, it works like that:
Not necessarily. For at least these reasons:
time_t (and clock_t) are arithmetic types capable of representing times.
There is no requirement about which arithmetic type, what the encoding is
(so linearity is not required), what the granularity is (so "seconds" are
not required), or what the base epoch is (so no particular date -- and
certainly not 1/1/1970 -- is specified).

Hmm, You might want to risk a look into time.h it says there:
typedef long time_t;
and if you call the time() function you get the SECONDS since 1/1/1970
passed to it.


You should be aware that your implementations time.h and compiler
does not define the language. The language is defined by ISO/IEC 9899.
This standard says in regards to time_t, that is is an arithmetic
type capable of representing times. The range and precision of
time_t is implementation defined. Thus the range, long on your
implementation, may be different on other implementations. And,
your precision, seconds, may be different on other implementations.
SOO

now anyway the program would still run and print the current time,
even if I was wrong about time() and time_t.


Soo, you should avoid asserting that time_t should be type long and
the precision is seconds since some date in 1970. These may be true
for your implementation of the language but there are many in this
newsgroup who have implementations of the language that do not use
your asserted range and/or precision.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #12
Kevin Easton <kevin@-nospam-pcug.org.au> spoke thus:
There is no one true time.h.


The time.h that can be known is not the true time.h? ;)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #13
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
Kevin Easton <kevin@-nospam-pcug.org.au> spoke thus:
There is no one true time.h.
The time.h that can be known is not the true time.h? ;)


And so Christopher was enlightened.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 13 '05 #14
Al Bowers <xa*@abowers.combase.com> writes:
[...]
Soo, you should avoid asserting that time_t should be type long and
the precision is seconds since some date in 1970. These may be true
for your implementation of the language but there are many in this
newsgroup who have implementations of the language that do not use
your asserted range and/or precision.


Quite correct.

Note that the convention of making time_t an integer type with
one-second granularity, with 0 representing midnight 1970-01-01, goes
back to the early days of Unix and C (before the ANSI/ISO C standard).
This convention spread to many other implementations. I've seen
implementations where time_t is a 64-bit type, and ones where it's
unsigned rather than signed, but I don't believe I've ever used one
with a granularity other than 1 second or an epoch other than
1970-01-01.

That's not, of course, to say that such implementations don't exist,
just that it's all to easy to assume that they don't. Writing
portable code isn't just about making it work on every implementation
you've seen; you have to understand the standard and avoid making any
unjustified assumptions about implementations you haven't seen.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #15
Keith Thompson <ks*@cts.com> wrote:

I've seen
implementations where time_t is a 64-bit type, and ones where it's
unsigned rather than signed, but I don't believe I've ever used one
with a granularity other than 1 second or an epoch other than
1970-01-01.


I have. SAS/C for the IBM mainframe uses double for time_t; the units
are seconds from the epoch, but the floating type allows subsecond
resolution. When I used it, the epoch was 1900-01-01, but recent
releases default to 1970-01-01 to conform to POSIX (it's a global
variable so you can set it to whatever you want).

-Larry Jones

I never get to do anything fun. -- Calvin
Nov 13 '05 #16
la************@eds.com wrote:
Keith Thompson <ks*@cts.com> wrote:
I've seen
implementations where time_t is a 64-bit type, and ones where it's
unsigned rather than signed, but I don't believe I've ever used one
with a granularity other than 1 second or an epoch other than
1970-01-01.

I have. SAS/C for the IBM mainframe uses double for time_t; the units
are seconds from the epoch, but the floating type allows subsecond
resolution. When I used it, the epoch was 1900-01-01,


which would be consistant with the 'native' epoch of the mainframe. MVS and
VSE both use 00:00:00 1900-01-01 as the beginning of their epoch. I don't know
about zOS or OS390, but both MVS and VSE used a 32bit unsigned binary number
(an S/390 "fullword") that was incremented about (IIRC) 300 times per second.
but recent
releases default to 1970-01-01 to conform to POSIX (it's a global
variable so you can set it to whatever you want).


Which would be consistant with IBM's ongoing evolution of the mainframe support.
--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

Nov 13 '05 #17
Lew Pitcher <lp******@sympatico.ca> writes:
[...]
which would be consistant with the 'native' epoch of the
mainframe. MVS and VSE both use 00:00:00 1900-01-01 as the beginning
of their epoch. I don't know about zOS or OS390, but both MVS and VSE
used a 32bit unsigned binary number (an S/390 "fullword") that was
incremented about (IIRC) 300 times per second.


That would wrap around in less than 6 months. Are you sure it's not a
64-bit number?

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #18
Keith Thompson wrote:
Lew Pitcher <lp******@sympatico.ca> writes:
[...]
which would be consistant with the 'native' epoch of the
mainframe. MVS and VSE both use 00:00:00 1900-01-01 as the beginning
of their epoch. I don't know about zOS or OS390, but both MVS and VSE
used a 32bit unsigned binary number (an S/390 "fullword") that was
incremented about (IIRC) 300 times per second.

That would wrap around in less than 6 months. Are you sure it's not a
64-bit number?


You are, of course, correct. It wasn't a "Word"[1] value (32bits), it was a
"DoubleWord"[2] (64bit) value.
[1] "Word" here is IBM's term for a 32bit binary value. When stored, the high
8bits must be stored aligned on a two-octet (even address) boundary.
[2] "DoubleWord" here is IBM's term for a 64bit binary value. When stored,
the high 8bits must be stored aligned on a four-octet (even address) boundary.

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

Nov 13 '05 #19

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

Similar topics

8
by: Chris | last post by:
Sorry, This should be simple, but brain is hurting... How do I convert a Current Time to a Decimal 6,0 (HMS)? There must be a cleaner way then this: Insert into Table Values Dec(...
2
by: MarthaR | last post by:
I am trying to add the current time to a field when the user clicks on the toggle button next to the field. I am getting a time of 12:00:00 AM each time I click the button. How do I get a...
3
by: | last post by:
I have a datetime field in table. I want to insert the current time of database server to the table thr ASP.NET (C#). I have dataset to do the insert: DataSet1.TableNameRow rowNew =...
4
by: Gary Wessle | last post by:
Hi I am not getting current time with this program, what am I doing wrong? #include <ctime> #include <iostream> using namespace std; #define P(x) cout << #x " = " << (x) << "\n";
0
by: Saloni | last post by:
I have windows service which is going to run on several machines which are in different time zones in USA. I want to find out the Eastern Zone current time from these different machines. It...
3
by: Mark Ingram | last post by:
Hi, I'd like to know the best way of checking the current time during a demonstration product. At the minute i store the first run date, then compare that to the system time, but obviously a user...
4
by: iwdu15 | last post by:
Hi, in VB i could use the keyword Now to get the current DateTime, but C# doesnt have that. How can i get the current time? thanks -- -iwdu15
4
by: JuAn2226 | last post by:
Hi, can anyone help me . when my program starts there is numbers and time will be display in the form of visual basic. it will run continuesly .My problem here is i dont know how to display the data...
1
oranoos3000
by: oranoos3000 | last post by:
hi I want to get current time from server only once while user open page from site and repace this time with current time client and with each second that is added this time is added how can...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.