473,386 Members | 1,819 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,386 software developers and data experts.

ctime(3) gives incorrect results

Hi group,

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

int main(void)
{
time_t t1, t2;
char *st1, *st2;

t1 = time(NULL);

if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);

st1 = ctime(&t1);
st2 = ctime(&t2);

printf("st1 is %s", st1);
printf("st2 is %s", st2);

return(0);
}

The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007

How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?

Moreover, on a shell console:

$ date -r 1183506197
Wed Jul 4 01:43:17 CEST 2007
$ date -r 1183506198
Wed Jul 4 01:43:18 CEST 2007

Here date gives a correct result (i.e. the first date is one second
before the second date).

I doubt it has something to do with the compiler grouping the two calls
to time(NULL). If it's the case, how can I avoid it?

Thank you.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 3 '07 #1
4 3349
Pietro Cerutti wrote:
Hi group,

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

int main(void)
{
time_t t1, t2;
char *st1, *st2;

t1 = time(NULL);

if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);

st1 = ctime(&t1);
st2 = ctime(&t2);

printf("st1 is %s", st1);
printf("st2 is %s", st2);

return(0);
}

The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007

How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?
Well, you get these results because both st1 and st2 point to the same string.

ctime() returns a pointer to a statically allocated character array in which
the date/time string is stored (by asctime(), under the covers). This array is
reused with each call to ctime(), so your second call to ctime() wiped out the
string generated by the first call to ctime().

What you /should/ have done is something like....

printf("t1 is %s\n",ctime(&t1));
printf("t2 is %s\n",ctime(&t2));

If you needed to defer the printing until after both times were collected, you
should strcpy() the results of the first ctime() call to somewhere safe before
calling ctime() the second time.

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Jul 4 '07 #2
Lew Pitcher wrote:
Pietro Cerutti wrote:
>Hi group,

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

int main(void)
{
time_t t1, t2;
char *st1, *st2;

t1 = time(NULL);

if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);

st1 = ctime(&t1);
st2 = ctime(&t2);

printf("st1 is %s", st1);
printf("st2 is %s", st2);

return(0);
}

The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007

How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?

Well, you get these results because both st1 and st2 point to the same string.

ctime() returns a pointer to a statically allocated character array in which
the date/time string is stored (by asctime(), under the covers). This array is
reused with each call to ctime(), so your second call to ctime() wiped out the
string generated by the first call to ctime().

What you /should/ have done is something like....

printf("t1 is %s\n",ctime(&t1));
printf("t2 is %s\n",ctime(&t2));

If you needed to defer the printing until after both times were collected, you
should strcpy() the results of the first ctime() call to somewhere safe before
calling ctime() the second time.

HTH
Uh, thank you!

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 4 '07 #3
Pietro Cerutti wrote:
Hi group,

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

int main(void)
{
time_t t1, t2;
char *st1, *st2;

t1 = time(NULL);

if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);

st1 = ctime(&t1);
st2 = ctime(&t2);

printf("st1 is %s", st1);
printf("st2 is %s", st2);

return(0);
}

The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007

How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?
/* mha: off-topic non-standard functionality retained here, although
flagged. The problem does not lie with those non-standard
features, and I was too lazy to create a new example. */

#include <stdio.h>
#include <unistd.h /* mha: not a standard C header */
#include <time.h>
#include <stdlib.h /* mha: added so the error return
returns a portably defined value */
#include <string.h /* mha: added for strcpy */

int main(void)
{
time_t t1, t2;
char st1[26], st2[26]; /* mha: replaced pointers with arrays */

t1 = time(NULL);

if (sleep(1) /* mha: not a standard C function */ ) {
printf("Signal received, exiting\n");
return EXIT_FAILURE; /* mha: replaced the (1) return value */
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long) t1);
printf("t2 is %lu\n", (unsigned long) t2);

/* mha: below were two assignments to two pointers of a pointers to
the same static array. I have fixed that. These two lines are not
actually needed, since the calls to ctime() could be done in
the arguments to printf. */
strcpy(st1, ctime(&t1));
strcpy(st2, ctime(&t2));

printf("st1 is %s", st1);
printf("st2 is %s", st2);

return 0;
}
Jul 4 '07 #4

"Pietro Cerutti" <ga**@gahr.chha scritto nel messaggio news:87***************************@news.hispeed.ch ...
Hi group,

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

int main(void)
{
time_t t1, t2;
char *st1, *st2;

t1 = time(NULL);

if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}

t2 = time(NULL);

printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);

st1 = ctime(&t1);
st2 = ctime(&t2);
7.23.3 Time conversion functions
1 Except for the strftime function, these functions each return a pointer to one of two
types of static objects: a broken-down time structure or an array of char. Execution of
any of the functions that return a pointer to one of these object types may overwrite the
information in any object of the same type pointed to by the value returned from any
previous call to any of them.

Here the second call of ctime overwrites the result of the first.
Try declaring st1 and st2 as arrays of chars, and to use strcpy().
char st1[26];
char st2[26];
/* ... */
strcpy(st1, ctime(&t1));
strcpy(st2, ctime(&t2));
printf("st1 is %s", st1);
printf("st2 is %s", st2);

return(0);
}

The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007

How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?

Moreover, on a shell console:

$ date -r 1183506197
Wed Jul 4 01:43:17 CEST 2007
$ date -r 1183506198
Wed Jul 4 01:43:18 CEST 2007

Here date gives a correct result (i.e. the first date is one second
before the second date).

I doubt it has something to do with the compiler grouping the two calls
to time(NULL). If it's the case, how can I avoid it?
If this were the case, you could declare t1 and t2 as volatile. But
it isn't, as t1 < t2.
Jul 4 '07 #5

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

Similar topics

1
by: Tivo Escobar | last post by:
Hi all, anybody here can tell me what exactly is returned by the standard function time() in <ctime> (or time.h). Please dont tell me it is the number of seconds since 1 Jan of 1970 GMT, cos it...
2
by: Oplec | last post by:
Hello, I am learning C++ using The C++ Programming : Special Edition by Bjarne Stroustrup. After reading some of his papers on his site, I came across a source code file that is used to compare...
5
by: berkay | last post by:
long num,num1; char *first; char *last; num=ilk.getTime();//gets time first=ctime(&num); cout.flush(); fflush(stdin); 1) cout<<"first:"<<first;
1
by: JM | last post by:
Hello, A very simple select query (no joins) to a linked ODBC SQL Server 2000 table produces incorrect results. The right number of rows seem to be returned, but some of the columns have...
2
by: Hao Xu | last post by:
Hi everyone! I think that everyone knows ctime() in glibc: The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When ...
8
by: B Williams | last post by:
I have been searching the internet trying to find the definition for the function time() in the standard library header <ctime>. Can someone help me with this? Thanks in advance.
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";
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
0
by: qpwang | last post by:
Dear All, I got a problem and need your help. After I converted my project (Embedded VC++ 3.0 for Pocket PC) to .NET 2005 Framework I can compile it without any error or warning. Then I ran it on...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.