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? 18 36283
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
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 :')
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
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
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/
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
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
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 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?
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.
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/
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 |
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
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"
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 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.
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"
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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(...
|
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...
|
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 =...
|
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";
|
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...
|
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...
|
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
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |