Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   ohmigod I forgot to google for time (http://www.velocityreviews.com/forums/t704583-ohmigod-i-forgot-to-google-for-time.html)

frank 11-08-2009 03:03 AM

ohmigod I forgot to google for time
 

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?
--
frank

"Rape: is it too much to ask for corporations who bid on federal
contracts to refrain from?"

Nick Keighley 11-08-2009 01:40 PM

Re: ohmigod I forgot to google for time
 
On 8 Nov, 03:03, frank <fr...@example.invalid> wrote:

> 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

<snip>

> 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

frank 11-09-2009 07:24 AM

Re: ohmigod I forgot to google for time
 
On Sun, 08 Nov 2009 05:40:21 -0800, Nick Keighley wrote:

> On 8 Nov, 03:03, frank <fr...@example.invalid> wrote:


>> 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().


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.
>
>> *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


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$

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


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


--
frank

"Guns: yes, they are harmful."

Keith Thompson 11-09-2009 08:17 AM

Re: ohmigod I forgot to google for time
 
frank <frank@example.invalid> writes:
[...]
> 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 );


localtime is declared for you in <time.h>. Redeclaring is either
illegal (if you get it wrong) or useless (if you get it right).

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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Nick Keighley 11-09-2009 08:20 AM

Re: ohmigod I forgot to google for time
 
On 8 Nov, 16:30, Joe Wright <joewwri...@comcast.net> wrote:
> Nick Keighley wrote:
> > On 8 Nov, 03:03, frank <fr...@example.invalid> wrote:


<snip>

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

>
> 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()?

Nick Keighley 11-09-2009 08:40 AM

Re: ohmigod I forgot to google for time
 
On 9 Nov, 07:24, frank <fr...@example.invalid> wrote:
> On Sun, 08 Nov 2009 05:40:21 -0800, Nick Keighley wrote:
> > On 8 Nov, 03:03, frank <fr...@example.invalid> 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:-

http://www.dinkumware.com/manuals/?m...page=time.html

<snip>

> >> 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.


<snip>

> 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$
>
> >> 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

>
> 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.



frank 11-09-2009 09:28 AM

Re: ohmigod I forgot to google for time
 
On Mon, 09 Nov 2009 00:17:18 -0800, Keith Thompson wrote:

> frank <frank@example.invalid> writes: [...]
>> 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.


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.
>
>> printf ("The current date and time is: \n" );

>
> 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.
--
frank

"Guns: yes, they are harmful."

Nick Keighley 11-09-2009 11:06 AM

Re: ohmigod I forgot to google for time
 
On 9 Nov, 09:28, frank <fr...@example.invalid> wrote:
> On Mon, 09 Nov 2009 00:17:18 -0800, Keith Thompson wrote:
> > frank <fr...@example.invalid> writes:


<snip>

> > 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.

>
> 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.

<snip>

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


<snip>


frank 11-14-2009 01:55 AM

Re: ohmigod I forgot to google for time
 
On Mon, 09 Nov 2009 00:40:59 -0800, Nick Keighley wrote:


>> 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.


ok
>
>> 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.


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 faltered on two further attempts, one using unix extensions

>
> 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,
--
frank

"Guns: yes, they are harmful."


All times are GMT. The time now is 05:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.