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

current time with ctime


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";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970
Jul 24 '06 #1
4 9330
Gary Wessle wrote:
>
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";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970
Why did you expect the current time from this? ctime gives you a string
describing the point in time you give it as argument. This argument must
contain the number of seconds elapsed since 00:00:00 UTC on January 1,
1970. Since your 'rawtime' is zero, that's the time you get.
If you want to get the current time, you first have to initialize your
time_t value to that.

Jul 24 '06 #2
Rolf Magnus <ra******@t-online.dewrites:
Gary Wessle wrote:

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";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970

Why did you expect the current time from this?
because off
************************************************** **************
http://www.cplusplus.com/ref/ctime/ctime.html
Example.

/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );

return 0;
}
************************************************** **************
>ctime gives you a string
describing the point in time you give it as argument. This argument must
contain the number of seconds elapsed since 00:00:00 UTC on January 1,
1970. Since your 'rawtime' is zero, that's the time you get.
If you want to get the current time, you first have to initialize your
time_t value to that.
is there a good link out there?
Jul 24 '06 #3
Gary Wessle wrote:
>Why did you expect the current time from this?
because off
time_t rawtime;

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );
Note the call to "time()".
Jul 24 '06 #4
Rolf Magnus wrote:

Why did you expect the current time from this? ctime gives you a
string describing the point in time you give it as argument. This
argument must contain the number of seconds elapsed since 00:00:00
UTC on January 1, 1970.

That's not accurate. The form of the return from time() is not
specified, nor indeed is the type of time_t. A common implementation is
as you describe.


Brian
Jul 24 '06 #5

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

Similar topics

5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
2
by: zolaris | last post by:
I am trying to print the current system time to a file. I know only a little bit about Python. I have gotten the very simple: Print time.time() to work properly. From what I gather the line...
18
by: Tim Quon | last post by:
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...
2
by: Suresh | last post by:
hello, i am new to c and how do i use time function to get time. thanks, Sean
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
3
by: rrs.matrix | last post by:
i have a problem i have a file which contains the time and is generated on sun machine. i have to decode the time on linux machine. the problem is that the sun machine shows different time and the...
1
by: Allen Maki | last post by:
Hi everybody, I need help. I want to print the current time on the screen. After research I managed to run the codes below in regular C++. But I could not make it to run in .NET
5
by: Allen Maki | last post by:
Hi everybody, I need help. I want to print the current time on the screen. After research I managed to run the codes below in regular C++. But I could not make it to run in .NET
1
by: Josef Dalcolmo | last post by:
Hello, I have tried this only on Windows XP. in Python 2.4 os.path.getmtime() used to return an integer representing the local time. in Python 2.5 os.path.getmtime() reports a float...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.