ohmigod I forgot to google for time

F

frank

I have an unusual posting style where I tend to waste the first paragraph
of a post. Newsreading requires a news writer, and any writer worth his
salt does not approach his topic too quickly.

So it is that I might ask for the answer before I have the subject or
keywords that might power an internet browser search with some degree of
relevance.

Jacob was talking about ISO changing definitions of time. The only c
version of computer time I had dealt with was difftime(), and the
appropriate call of type time_t that seeds srand().

The background reading in H&S reveals *several* C time facilties, and I
want to work my way through them all at least once for kicks and giggles.

What I want now is a means to date my construction bids. Their form is
to be:
November 7, 2008

How might I best achieve that?
 
N

Nick Keighley

I have an unusual posting style where I tend to waste the first paragraph
of a post.  Newsreading requires a news writer, and any writer worth his
salt does not approach his topic too quickly.

we've got to find something to do with all that Dark Fibre

Jacob was talking about ISO changing definitions of time.

bloody hell I never new ISO had such powers. So is time going to go
faster or slower? Or just hopabout at random?

Jacob, was actually talking about the tightening up of the definition
of a particular function named asctime().
 The only c
version of computer time I had dealt with was difftime(),

This is what I'd call a type error (apparently the officila name is a
Category Error). difftime() is a function it is not a "version" of
time.
and the
appropriate call of type time_t that seeds srand().

time_t is a type or type alias. It cannot be called.

The background reading in H&S reveals *several* C time facilties, and I
want to work my way through them all at least once for kicks and giggles.
ok


What I want now is a means to date my construction bids.  Their form is
to be:
November 7, 2008

How might I best achieve that?

take a look at strftime(). Or even sprintf(). If you can avoid a
format like this. There is less ambiguity and its easier for automatic
processing if you use the international standard ISO-8601 2008-11-07
 
F

frank

bloody hell I never new ISO had such powers. So is time going to go
faster or slower? Or just hopabout at random?

Jacob, was actually talking about the tightening up of the definition of
a particular function named asctime().

Yeah. Somehow I had the errant notion that standard C had only difftime
() to offer. I'll see if I can find that other thread and read more.
This is what I'd call a type error (apparently the officila name is a
Category Error). difftime() is a function it is not a "version" of time.


time_t is a type or type alias. It cannot be called.



ok

Well it's not too hard to get on the scoreboard here:

dan@dan-desktop:~/source$ cat time1.c

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

int main(void)
{
time_t now;
now = time(NULL);
printf ("The current date and time is: %s\n", ctime(&now));
return 0;
}
// gcc -Wall -Wextra time1.c -o out
dan@dan-desktop:~/source$ gcc -Wall -Wextra time1.c -o out
dan@dan-desktop:~/source$ ./out
The current date and time is: Mon Nov 9 00:10:24 2009

dan@dan-desktop:~/source$

I faltered on two further attempts, one using unix extensions and another
using localtime. I can rarely cook from scratch with structs and no help:

cat time3.c

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

int main(void)
{

time_t now;
struct tm *localtime(const time_t *t );
now = time(NULL);
tm my_time;

my_time = localtime(&now);
printf ("The current date and time is: \n" );
return 0;
}

// gcc -Wall -Wextra time3.c -o out




dan@dan-desktop:~/source$ gcc -Wall -Wextra time3.c -o out
time3.c: In function ‘main’:
time3.c:11: error: ‘tm’ undeclared (first use in this function)
time3.c:11: error: (Each undeclared identifier is reported only once
time3.c:11: error: for each function it appears in.)
time3.c:11: error: expected ‘;’ before ‘my_time’
time3.c:13: error: ‘my_time’ undeclared (first use in this function)
dan@dan-desktop:~/source$
take a look at strftime(). Or even sprintf(). If you can avoid a format
like this. There is less ambiguity and its easier for automatic
processing if you use the international standard ISO-8601 2008-11-07

P. 448 has this for the synopsis but the explanation is too much for me :(
size_t strftime(
char * s, size_t maxsize,
const char *format,
const struct tm *timeptr);
 
K

Keith Thompson

frank said:
Yeah. Somehow I had the errant notion that standard C had only difftime
() to offer. I'll see if I can find that other thread and read more.

I don't know what you mean by "had only difftime() to offer".
Standard C has a number of other functions that deal with time. See
the standard or any decent C reference for details.

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

int main(void)
{

time_t now;
struct tm *localtime(const time_t *t );

now = time(NULL);
tm my_time;

The type is called "struct tm", not "tm".
my_time = localtime(&now);

If you declare my_time as "struct tm my_time;", this is illegal;
localtime returns a struct tm*, not a struct tm.

If you change the declaration to "struct tm *my_time;", it should
compile.
printf ("The current date and time is: \n" );

Ok, what are you trying to do here?

And *please* indent your code.
return 0;
}
[...]

P. 448 has this for the synopsis but the explanation is too much for me :(
size_t strftime(
char * s, size_t maxsize,
const char *format,
const struct tm *timeptr);

Perhaps this example will help:

#include <stdio.h>
#include <time.h>
int main(void)
{
const time_t now = time(NULL);
const struct tm *ltime = localtime(&now);
char buf[100];
const size_t result
= strftime(buf, sizeof buf, "%a %Y-%m-%d %H:%M:%S %Z", ltime);
printf("strftime() returned %u\n", (unsigned)result);
printf("It is now %s\n", buf);
return 0;
}
 
N

Nick Keighley

It is indeed a great idea. Extend it some by adding time to date:

   "2009-11-08 11:12:31"

maybe he doesn't have the time of day available. ISO 8601 allows my
version as well. Are construction bids noramlly timed to the second?
Why did you omit the time zone and Daylight Saving Time?
Now you have a string which can be sorted lexically with strcmp().

why couldn't my version be sorted lexically with strcmp()?
 
N

Nick Keighley

On 8 Nov, 03:03, frank <[email protected]> wrote:
Jacob was talking about ISO changing definitions of time.
[...]
Jacob, was actually talking about the tightening up of the definition of
 a particular function named asctime().

Yeah.  Somehow I had the errant notion that standard C had only difftime
() to offer.  I'll see if I can find that other thread and read more.

Standard C has a collection of time related functions. Lookup the
header time.h. This site is not bad:-

www.dinkumware.com/manuals/?manual=compleat&page=time.html


I faltered on two further attempts, one using unix extensions

I'd steer away from these initially

and another
using localtime.  I can rarely cook from scratch with structs and no help:

if you're on unix then try the man pages. That dinkumware link I
posted is ok. Do you have a book, like K&R?
#include <stdio.h>
#include <time.h>

int main(void)
{

time_t now;
struct tm *localtime(const time_t *t );

don't do this. time.h will define localtime(), so you are either
getting the definition wrong or repeating something unnecessarily. Let
the standard headers define function prototypes etc.
now = time(NULL);

you are mixing declarations with statements which isn't allowed in
C89. Move the call to time() so it appears after the decalarations.
tm my_time;

you mean

struct tm *my_time;

C insists on the struct keyword being there (which is why I typedef my
structs) and localtime returns a pointer to a struct tm.

my_time = localtime(&now);
printf ("The current date and time is: \n" );
return 0;

}

//     gcc   -Wall -Wextra time3.c -o out

dan@dan-desktop:~/source$  gcc   -Wall -Wextra time3.c -o out
time3.c: In function ‘main’:
time3.c:11: error: ‘tm’ undeclared (first use in this function)
time3.c:11: error: (Each undeclared identifier is reported only once
time3.c:11: error: for each function it appears in.)
time3.c:11: error: expected ‘;’ before ‘my_time’
time3.c:13: error: ‘my_time’ undeclared (first use in this function)
dan@dan-desktop:~/source$



P. 448

of H&S? I always consideredd that H&S was pretty good on the library.
Still I've given you several other references.
has this for the synopsis but the explanation is too much for me :(
size_t strftime(
  char * s, size_t maxsize,
  const char *format,
  const struct tm *timeptr);

it's a bit like sprintf() you give it a time and it produces a string
representing the time in a format you describe. I don't find the
format options very intuitive so I often use sprintf() instead.
 
F

frank

I don't know what you mean by "had only difftime() to offer". Standard C
has a number of other functions that deal with time. See the standard
or any decent C reference for details.

What's funny is that I came up with this notion reading your comments. I
was pretty wet behind the ears back then. I'm certain the error was in
my partial understanding.
If you change the declaration to "struct tm *my_time;", it should
compile.


Ok, what are you trying to do here?

And *please* indent your code.

I get farther with this on two fronts. The struct declaration is legal,
but I still can't get the syntax right to see some output. *Plus*, it
took me one minute to install indent:

time3.c: In function ‘main’:
time3.c:13: error: request for member ‘tm_day’ in something not a
structure or union
dan@dan-desktop:~/source$ cat time3.c

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

int main(void)
{
time_t now;
struct tm *localtime(const time_t *t );
now = time(NULL);
struct tm *my_time;

my_time = localtime(&now);
printf ("The current day is: %s\n", my_time.tm_day );
return 0;
}

// gcc -Wall -Wextra time3.c -o out

dan@dan-desktop:~/source$ indent time3.c
dan@dan-desktop:~/source$ cat time3.c

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

int
main (void)
{
time_t now;
struct tm *localtime (const time_t * t);
now = time (NULL);
struct tm *my_time;

my_time = localtime (&now);
printf ("The current day is: %s\n", my_time.tm_day);
return 0;
}

// gcc -Wall -Wextra time3.c -o out

Perhaps this example will help:

dan@dan-desktop:~/source$ cat time4.c

#include <stdio.h>
#include <time.h>
int main(void)
{
const time_t now = time(NULL);
const struct tm *ltime = localtime(&now);
char buf[100];
const size_t result
= strftime(buf, sizeof buf, "%a %Y-%m-%d %H:%M:%S %Z", ltime);
printf("strftime() returned %u\n", (unsigned)result);
printf("It is now %s\n", buf);
return 0;
}

// gcc -Wall -Wextra time4.c -o out
dan@dan-desktop:~/source$ gcc -Wall -Wextra time4.c -o out
dan@dan-desktop:~/source$ ./out
strftime() returned 27
It is now Mon 2009-11-09 02:19:21 MST
dan@dan-desktop:~/source$


Thx, Keith. I'll see if I can wrap my head around this.
 
N

Nick Keighley

I get farther with this on two fronts.  The struct declaration is legal,
but I still can't get the syntax right to see some output.  *Plus*, it
took me one minute to install indent:

a program as small as that could have been indented "by hand". It's
really a good idea to get into the habbit of laying it out nicely as
you type it in.

time3.c: In function ‘main’:
time3.c:13: error: request for member ‘tm_day’ in something not a
structure or union

what is the type of my_time? Is it a structure (struct) or union?
[clue: "no"]. What type is tm_day? Is it a C string? It pays to read
the documentaion you have carefully.

struct tm *my_time;
[...]
printf ("The current day is: %s\n", my_time.tm_day );

<snip>
 
F

frank

don't do this. time.h will define localtime(), so you are either getting
the definition wrong or repeating something unnecessarily. Let the
standard headers define function prototypes etc.
ok

you are mixing declarations with statements which isn't allowed in C89.
Move the call to time() so it appears after the decalarations.

I thought it wasn't allowed in C99.
[snip]
I think I have something that looks right now.

dan@dan-desktop:~/source$ gcc -Wall -Wextra time6.c -o out
dan@dan-desktop:~/source$ ./out
November 13, 2009
dan@dan-desktop:~/source$ indent time6.c
dan@dan-desktop:~/source$ cat time6.c
#include <time.h>
#include <stdio.h>

int
main (void)
{
struct tm *ptr;
time_t lt;
char str[80];

lt = time (NULL);
ptr = localtime (&lt);

strftime (str, sizeof (str), "%B %d, %Y", ptr);
printf ("%s\n", str);

return 0;
}


// gcc -Wall -Wextra time6.c -o out
dan@dan-desktop:~/source$

(Code review requested.) It is a singular pleasure to code C on linux.
I'd steer away from these initially.

But that's where I want to go. Doing time on *nix with C sounds like a
great way for me to get my feet wet. I'll x-post to comp.unix.programmer
and set the follow-up to there as well.

P. 444 of H&S V shows the unix version of this material as
#include <sys/types.h>
#include <sys/times.h>
....

times.h wasn't too tough to read. What does types.h have of relevance to
getting the time from the OS?

Thanks for your comment and cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top