I want to get the current date/time using time(), then use ctime() to
display it.
What type is time_t? I've tried looking it up in time.h and elsewhere.
"grep" shows: typedef __time_t time_t;
which, in turn, greps as: __time_t tv_sec;
Here's my latest attempt:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
main (void)
{
time_t eot, *tod;
eot = time(tod);
printf("*tod\t = %ul\n", *tod);
printf("Today's date is: %s\n", ctime(tod));
return EXIT_SUCCESS;
}
What am I doing wrong?
Also, how can I determine the max value time_t can hold?
jim
--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ Everything should be made as simple as possible, but not simpler.
_/ -- Albert Einstein
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 8 2282
Jim Showalter wrote on 08/08/04 : I want to get the current date/time using time(), then use ctime() to display it.
What type is time_t?
It's time_t. You don't need to know more except that it could be a
floating point.
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main (void) { time_t eot, *tod;
eot = time(tod);
Wrong. You are passing an undefined value to a function. My compiler
chokes on that. Yours should if it has been correctly configured.
eot = time (NULL);
or
time (&eot);
get rid of this tod. It's useless.
printf("*tod\t = %ul\n", *tod); printf("Today's date is: %s\n", ctime(tod));
printf("*tod\t = %ul\n", (unsigned long) eot);
printf("Today's date is: %s\n", ctime(eot));
return EXIT_SUCCESS; }
Note that ctime() returns the adress of a static string. It may behave
strangely if you don't make a copy of the pointed string. Better to use
strftime() (and it's more fun, actually).
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
Emmanuel Delahaye wrote: Jim Showalter wrote on 08/08/04 : I want to get the current date/time using time(), then use ctime() to display it.
What type is time_t?
It's time_t. You don't need to know more except that it could be a floating point.
Note that ctime() returns the adress of a static string. It may behave strangely if you don't make a copy of the pointed string. Better to use strftime() (and it's more fun, actually).
Ok, I got it working - thanks Emmanuel! But you missed my other
question at the end, which was: How can I determine the max value
time_t can hold?
jim
--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ Everything should be made as simple as possible, but not simpler.
_/ -- Albert Einstein
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
# Ok, I got it working - thanks Emmanuel! But you missed my other
# question at the end, which was: How can I determine the max value
# time_t can hold?
System dependent. On current unices it's good to about 2039. Mac Classic
is until 2104 I think, perhaps. Windows clocks are good until 2003. Or
8:03 PM if you prefer that notation. If you have a choice, don't store
time_t in a file, but use something like ISO calendar values. Hopefully
the time_t value will change to 64 bits before 2039.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
The little stoner's got a point.
SM Ryan wrote: # Ok, I got it working - thanks Emmanuel! But you missed my other # question at the end, which was: How can I determine the max value # time_t can hold?
System dependent. On current unices it's good to about 2039. Mac Classic is until 2104 I think, perhaps. Windows clocks are good until 2003. Or 8:03 PM if you prefer that notation. If you have a choice, don't store time_t in a file, but use something like ISO calendar values. Hopefully the time_t value will change to 64 bits before 2039.
-- SM Ryan http://www.rawbw.com/~wyrmwif/ The little stoner's got a point.
2003? Glad I'm not programming on Windows! :)
Seriously, that's about what I figured - but not my concern. I'm just
trying to complete the first "Programming Challenge" in Peter van der
Linden's book, "Expert C Programming", and I'm already stumped!
Surely there is a method using C to determine the greatest value that
any type can hold?
jim
--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ Everything should be made as simple as possible, but not simpler.
_/ -- Albert Einstein
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
# Surely there is a method using C to determine the greatest value that
# any type can hold?
sizeof(T)*CHAR_BIT will usually be the number of bits, but doesn't say
anything about how the value is encoded in the bits.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Jim Showalter wrote:
.... snip ... Surely there is a method using C to determine the greatest value that any type can hold?
#include <limits.h>
#include <float.h>
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Emmanuel Delahaye wrote: Jim Showalter wrote on 08/08/04 :
I want to get the current date/time using time(), then use ctime() to display it.
What type is time_t?
It's time_t. You don't need to know more except that it could be a floating point.
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main (void) { time_t eot, *tod;
eot = time(tod);
Wrong. You are passing an undefined value to a function. My compiler chokes on that. Yours should if it has been correctly configured.
eot = time (NULL);
or
time (&eot);
get rid of this tod. It's useless.
printf("*tod\t = %ul\n", *tod); printf("Today's date is: %s\n", ctime(tod));
printf("*tod\t = %ul\n", (unsigned long) eot); printf("Today's date is: %s\n", ctime(eot));
return EXIT_SUCCESS; }
Note that ctime() returns the adress of a static string. It may behave strangely if you don't make a copy of the pointed string. Better to use strftime() (and it's more fun, actually).
time.h on my system prototypes ...
char * ctime(const time_t *_cal);
.... indicating that ctime() wants a pointer, ctime(&eot) in your
example. No?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Joe Wright wrote on 08/08/04 : printf("Today's date is: %s\n", ctime(eot));
char * ctime(const time_t *_cal);
... indicating that ctime() wants a pointer, ctime(&eot) in your example. No?
Yes, I meant:
printf("Today's date is: %s\n", ctime(&eot));
thanks for the correction.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Anand CS |
last post by:
Hi All
I have question regarding time data structures...
I have 64 bit unsigned microsecond resolution
(unsigned __int64 for windows/and unsigned long long for others)
variable. It stores the...
|
by: j0mbolar |
last post by:
i've read recently that time_t doesn't have to be in second resolution
but if it isn't, then what resolution could it be in?
microseconds, milliseconds, are all valid?
also, does anyone know...
|
by: Ian Pilcher |
last post by:
When adding two values of type time_t, how can I check for overflow?
Maybe I'm just brain-cramped today, but I can't figure out how to do it.
Thanks!
--...
|
by: Zwyatt |
last post by:
I have the following code (simplified here):
#include <time.h>
class A
{
public:
char *aString;
int aNum;
time_t aTime;
}
|
by: Zwyatt |
last post by:
having a really weird little bug w/ time_t...check it out:
I have the following code (simplified here):
#include <time.h>
class A
{
public:
char *aString;
int aNum;
|
by: Henrik Goldman |
last post by:
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.
So far I've written the following code:
time_t...
|
by: loudking |
last post by:
Hello, all
I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
RETURN VALUE
On success, the value of time in seconds since the Epoch is
retu
rned.
On error,...
|
by: Nick Keighley |
last post by:
Hi,
this is probably quite easy...
How do I convert a UTC string into a time_t?
eg. "2007-12-18 13:37:26" - a time_t.
Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t...
|
by: Abubakar |
last post by:
Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a...
|
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...
|
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,...
|
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: 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: 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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |