Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #157542 > unrolled thread

I need help understand some c code

Started byT <T@invalid.invalid>
First post2020-12-20 18:32 -0800
Last post2020-12-21 20:03 +0000
Articles 20 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  I need help understand some c code T <T@invalid.invalid> - 2020-12-20 18:32 -0800
    Re: I need help understand some c code Siri Cruise <chine.bleu@yahoo.com> - 2020-12-20 19:03 -0800
    Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 19:56 -0800
      Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 20:02 -0800
        Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 20:43 -0800
          Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 21:06 -0800
            Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 21:56 -0800
    Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 20:33 -0800
      Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 20:51 -0800
        Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 21:11 -0800
          Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 22:10 -0800
            Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 22:15 -0800
        Re: I need help understand some c code Bart <bc@freeuk.com> - 2020-12-21 13:11 +0000
          Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-21 13:47 -0800
            Re: I need help understand some c code scott@slp53.sl.home (Scott Lurndal) - 2020-12-23 17:18 +0000
              Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-23 12:05 -0800
          Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-21 14:25 -0800
    Re: I need help understand some c code Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-21 05:45 +0100
    Re: I need help understand some c code Bart <bc@freeuk.com> - 2020-12-21 12:16 +0000
    Re: I need help understand some c code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-21 20:03 +0000

#157542 — I need help understand some c code

FromT <T@invalid.invalid>
Date2020-12-20 18:32 -0800
SubjectI need help understand some c code
Message-ID<rrp1gb$kle$1@dont-email.me>
Hi All,

I am trying to use Raku's native call to read the
time in Linux.


I found this C code:

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

void main()
{
     time_t t;
     time(&t);
     printf("\n current time is : %s",ctime(&t));
}


What is "time_t t"  and why the space?

What is "time($t)"?

I also need to know the length in bit.

Many thanks,
-T

[toc] | [next] | [standalone]


#157544

FromSiri Cruise <chine.bleu@yahoo.com>
Date2020-12-20 19:03 -0800
Message-ID<chine.bleu-32C5FC.19032620122020@reader.eternal-september.org>
In reply to#157542
In article <space-20201221034818@ram.dialup.fu-berlin.de>,
 ram@zedat.fu-berlin.de (Stefan Ram) wrote:

> T <T@invalid.invalid>:
> >What is "time_t t"  and why the space?
> 
>   It's the start of a declaration still missing the semicolon ";", 
>   consisting of the type-specifier "time_t" and the declarator "t". 
>   "time_t" is a real type capable of representing times.

#include<time.h>

And this is where time_t, time(), and ctime() are declared.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.    @
'I desire mercy, not sacrifice.'                                    /|\
Discordia: not just a religion but also a parody. This post         / \
I am an Andrea Doria sockpuppet.                  insults Islam.  Mohammed

[toc] | [prev] | [next] | [standalone]


#157545

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-20 19:56 -0800
Message-ID<87lfdrbymy.fsf@nosuchdomain.example.com>
In reply to#157542
T <T@invalid.invalid> writes:
> I am trying to use Raku's native call to read the
> time in Linux.
>
> I found this C code:
>
> #include<stdio.h>
> #include<time.h>
>
> void main()

This should be "int main(void)".  See questions 11.12a and following in
the comp.lang.c FAQ, <http://www.c-faq.com/>.

> {
>     time_t t;
>     time(&t);

Could also be written as:

    t = time(NULL);

If the time() function were being designed today, it would
undoubtedly take no arguments and return a time_t value.  Instead,
for obscure historical reasons I won't go into, it takes a
pointer-to-time_t argument and returns a time_t result, both of
which are updated with the result.  Passing a null pointer means you can
just use the returned result.

>     printf("\n current time is : %s",ctime(&t));

Output should normally *end* with a newline:

    printf(" current time is : %s\n", ctime(&t));

But, for bizarre historical reasons, ctime() returns a pointer to a
string that already ends with a newline (I had mercifully forgotten
that until just now), so you could drop the final \n.  "man 3 ctime"

> }
>
>
> What is "time_t t"  and why the space?

time_t is a type defined in <time.h>.

This:
    time_t t;
defines an object (variable) "t" of type "time_t".  Similarly, this:
    int n;
would define an object "n" of type "int".

> What is "time($t)"?

A syntax error, but "time(&t)" calls the time function, passing the
address of t as its argument.  The time function requires an argument of
type time_t* (pointer to time_t).  "man 2 time" is likely to show you
the manual for the time function if you're on a Unix-like system.

> I also need to know the length in bit.

Normally you wouldn't need to know that, but if you're interfacing from
another language you might.  The size of time_t is
implementation-defined.  Its size in bits for a given implementation is
    sizeof (time_t) * CHAR_BIT
where CHAR_BIT is a macro defined in <limits.h>.  CHAR_BIT is almost
certainly 8, and that "almost" can likely be dropped for any system that
supports Raku (formerly known as Perl 6), but it's good style to use it
anyway.  It's probably 64 bits if you're using a reasonably modern
system, but assuming that is a good way to shoot yourself in the foot.

Of course Raku has its own built-in time function, but I presume the
point is to learn how to use Raku's native call functionality, and a
simple example is a good starting point even if a simpler solution would
be better for that example.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157546

FromT <T@invalid.invalid>
Date2020-12-20 20:02 -0800
Message-ID<rrp6o0$ip9$1@dont-email.me>
In reply to#157545
On 12/20/20 7:56 PM, Keith Thompson wrote:
> t = time(NULL);

I found this on stack overflow:

int main (void)
{
     //print time in seconds from 1 Jan 1970 using c
     float n = time(NULL);
     printf("%.2f\n" , n);
}

"seconds from 1 Jan 1970" ????

I am just after something like 21:04:55

Is there a better call for this?

[toc] | [prev] | [next] | [standalone]


#157548

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-20 20:43 -0800
Message-ID<87h7ofbwh5.fsf@nosuchdomain.example.com>
In reply to#157546
T <T@invalid.invalid> writes:
> On 12/20/20 7:56 PM, Keith Thompson wrote:
>> t = time(NULL);
>
> I found this on stack overflow:
>
> int main (void)
> {
>     //print time in seconds from 1 Jan 1970 using c
>     float n = time(NULL);
>     printf("%.2f\n" , n);
> }

This converts time_t to float.  There's no good reason to do that.

> "seconds from 1 Jan 1970" ????

Yes, that's what the time() function returns in most implementions,
though it's implementation-defined.

> I am just after something like 21:04:55
> 
> Is there a better call for this?

time() gives you a value that you can pass to other functions to
interpret its meaning.  <time.h> is documented in section 7.27 of N1570:
    http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf
For more user-friendly documentation, see
    https://en.cppreference.com/w/c/chrono
or any decent C reference or tutorial.

gmtime() or localtime() converts a time_t to a struct tm, which
represents a time broken down into its components.

If you're going to be working with C as much as your questions suggest,
you would do well to learn the basics of the language.  You can't do so
by posting questions about one feature at a time.  (I mentioned this to
you a couple of years ago.)

[I just noticed that I had flagged you in my newsreader early last
year, for repeatedly posting off-topic replies.  Please refrain
from doing so again.]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157551

FromT <T@invalid.invalid>
Date2020-12-20 21:06 -0800
Message-ID<rrpagm$3e1$1@dont-email.me>
In reply to#157548
On 12/20/20 8:43 PM, Keith Thompson wrote:
> If you're going to be working with C as much as your questions suggest,
> you would do well to learn the basics of the language.  You can't do so
> by posting questions about one feature at a time.  (I mentioned this to
> you a couple of years ago.)
> 
> [I just noticed that I had flagged you in my newsreader early last
> year, for repeatedly posting off-topic replies.  Please refrain
> from doing so again.]

Keith,

I CLEARLY state I am programming in Raku.  The
questions I am posting here are to understand
how to convert C structures into Raku's NativeCall.
To do that I need to know what I am looking
at: type of variable, purpose, bits.

My questions are not off topic.  They are the
most basic of C programming.

Please refrain from misinterpreting me.

-T

[toc] | [prev] | [next] | [standalone]


#157553

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-20 21:56 -0800
Message-ID<878s9rbt2j.fsf@nosuchdomain.example.com>
In reply to#157551
T <T@invalid.invalid> writes:
> On 12/20/20 8:43 PM, Keith Thompson wrote:
>> If you're going to be working with C as much as your questions suggest,
>> you would do well to learn the basics of the language.  You can't do so
>> by posting questions about one feature at a time.  (I mentioned this to
>> you a couple of years ago.)
>>
>> [I just noticed that I had flagged you in my newsreader early last
>> year, for repeatedly posting off-topic replies.  Please refrain
>> from doing so again.]
>
> Keith,
>
> I CLEARLY state I am programming in Raku.  The
> questions I am posting here are to understand
> how to convert C structures into Raku's NativeCall.
> To do that I need to know what I am looking
> at: type of variable, purpose, bits.
>
> My questions are not off topic.  They are the
> most basic of C programming.
>
> Please refrain from misinterpreting me.

I didn't say that your current posts are off-topic.  I was
referring to your posts here some time ago, which at the time led
to me flagging you in my newsreader.  No point in going into the
details now.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157547

FromT <T@invalid.invalid>
Date2020-12-20 20:33 -0800
Message-ID<rrp8ij$r14$1@dont-email.me>
In reply to#157542
On 12/20/20 6:32 PM, T wrote:
> Hi All,
> 
> I am trying to use Raku's native call to read the
> time in Linux.
> 
> 
> I found this C code:
> 
> #include<stdio.h>
> #include<time.h>
> 
> void main()
> {
>      time_t t;
>      time(&t);
>      printf("\n current time is : %s",ctime(&t));
> }
> 
> 
> What is "time_t t"  and why the space?
> 
> What is "time($t)"?
> 
> I also need to know the length in bit.
> 
> Many thanks,
> -T


I need to cancel this question.  I am making the
mistake of calling a library in C (time.h) and what
I really need is to call a function inside a
/usr/lib64/libxxx.so.x.  And I do not know what
that is.  (Google keeps pointing me back to time.h.).

And since I am only look to create examples for a
paper I am writing, I do not really care what
example I use.

[toc] | [prev] | [next] | [standalone]


#157550

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-20 20:51 -0800
Message-ID<87czz3bw39.fsf@nosuchdomain.example.com>
In reply to#157547
T <T@invalid.invalid> writes:
[...]
> I need to cancel this question.  I am making the
> mistake of calling a library in C (time.h) and what
> I really need is to call a function inside a
> /usr/lib64/libxxx.so.x.  And I do not know what
> that is.  (Google keeps pointing me back to time.h.).
>
> And since I am only look to create examples for a
> paper I am writing, I do not really care what
> example I use.

This shows a serious lack of understanding about how C works, and
possibly compiled languages in general.  I don't say this to be
insulting, but to suggest that you don't know enough to know what
questions you need to ask.

time.h is a header, not a library.  It provides an interface to
library code (in this case, the C standard library) by providing
type and function declarations.  The code that implements those
functions may well be in /usr/lib64/libxxx.so.x on one system, or
in some completely different place on another.  As a C programmer,
you very probably don't need to know.  The compiler and/or linker
handles it all for you.  If you know to write "#include <time.h>" at
the top of your C source file, that's probably all you need to know.

If I write a simple program, say one that prints "hello world",
I create a source file, then I compile, link, and run it.  On my
system:

    gcc hello.c -o hello
    ./hello

gcc, and the tools it invokes, take care of all the details for me.
99% of the time I literally do not care where time.h or the library
that implements the time function exist on the system.

Find yourself a decent C tutorial and follow it.  There are
undoubtedly some decent ones online.  Perhaps others can suggest one.
(There are a lot of really bad ones, and you don't yet know enough
to tell the difference.)

Since you're apparently working on interfacing between C and another
language, you're likely to need more knowledge than you'd need just to
write and run C programs, but I suggest starting with that as a minimum.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157552

FromT <T@invalid.invalid>
Date2020-12-20 21:11 -0800
Message-ID<rrpapu$4mj$1@dont-email.me>
In reply to#157550
On 12/20/20 8:51 PM, Keith Thompson wrote:
> T <T@invalid.invalid> writes:
> [...]
>> I need to cancel this question.  I am making the
>> mistake of calling a library in C (time.h) and what
>> I really need is to call a function inside a
>> /usr/lib64/libxxx.so.x.  And I do not know what
>> that is.  (Google keeps pointing me back to time.h.).
>>
>> And since I am only look to create examples for a
>> paper I am writing, I do not really care what
>> example I use.
> 
> This shows a serious lack of understanding about how C works, and
> possibly compiled languages in general.  I don't say this to be
> insulting, but to suggest that you don't know enough to know what
> questions you need to ask.
> 
> time.h is a header, not a library.  It provides an interface to
> library code (in this case, the C standard library) by providing
> type and function declarations.  The code that implements those
> functions may well be in /usr/lib64/libxxx.so.x on one system, or
> in some completely different place on another.  As a C programmer,
> you very probably don't need to know.  The compiler and/or linker
> handles it all for you.  If you know to write "#include <time.h>" at
> the top of your C source file, that's probably all you need to know.
> 
> If I write a simple program, say one that prints "hello world",
> I create a source file, then I compile, link, and run it.  On my
> system:
> 
>      gcc hello.c -o hello
>      ./hello
> 
> gcc, and the tools it invokes, take care of all the details for me.
> 99% of the time I literally do not care where time.h or the library
> that implements the time function exist on the system.
> 
> Find yourself a decent C tutorial and follow it.  There are
> undoubtedly some decent ones online.  Perhaps others can suggest one.
> (There are a lot of really bad ones, and you don't yet know enough
> to tell the difference.)
> 
> Since you're apparently working on interfacing between C and another
> language, you're likely to need more knowledge than you'd need just to
> write and run C programs, but I suggest starting with that as a minimum.
> 

Keith,

You misunderstand.  NativeCall is calling a system
interrupt. Native call does not calls to C libraries.
That is why I need to call a function inside a .so.

Maybe if I root around inside the c code that goes
with time.h, I would be able to find that, but it
is way over my head.

-T

[toc] | [prev] | [next] | [standalone]


#157554

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-20 22:10 -0800
Message-ID<874kkfbsfu.fsf@nosuchdomain.example.com>
In reply to#157552
T <T@invalid.invalid> writes:
> On 12/20/20 8:51 PM, Keith Thompson wrote:
>> T <T@invalid.invalid> writes:
>> [...]
>>> I need to cancel this question.  I am making the
>>> mistake of calling a library in C (time.h) and what
>>> I really need is to call a function inside a
>>> /usr/lib64/libxxx.so.x.  And I do not know what
>>> that is.  (Google keeps pointing me back to time.h.).
>>>
>>> And since I am only look to create examples for a
>>> paper I am writing, I do not really care what
>>> example I use.
>>
>> This shows a serious lack of understanding about how C works, and
>> possibly compiled languages in general.  I don't say this to be
>> insulting, but to suggest that you don't know enough to know what
>> questions you need to ask.
>>
>> time.h is a header, not a library.  It provides an interface to
>> library code (in this case, the C standard library) by providing
>> type and function declarations.  The code that implements those
>> functions may well be in /usr/lib64/libxxx.so.x on one system, or
>> in some completely different place on another.  As a C programmer,
>> you very probably don't need to know.  The compiler and/or linker
>> handles it all for you.  If you know to write "#include <time.h>" at
>> the top of your C source file, that's probably all you need to know.
>>
>> If I write a simple program, say one that prints "hello world",
>> I create a source file, then I compile, link, and run it.  On my
>> system:
>>
>>      gcc hello.c -o hello
>>      ./hello
>>
>> gcc, and the tools it invokes, take care of all the details for me.
>> 99% of the time I literally do not care where time.h or the library
>> that implements the time function exist on the system.
>>
>> Find yourself a decent C tutorial and follow it.  There are
>> undoubtedly some decent ones online.  Perhaps others can suggest one.
>> (There are a lot of really bad ones, and you don't yet know enough
>> to tell the difference.)
>>
>> Since you're apparently working on interfacing between C and another
>> language, you're likely to need more knowledge than you'd need just to
>> write and run C programs, but I suggest starting with that as a minimum.
>
> You misunderstand.  NativeCall is calling a system
> interrupt. Native call does not calls to C libraries.
> That is why I need to call a function inside a .so.

I don't think "system interrupt" is an accurate representation of what
Raku's NativeCall does.

https://docs.raku.org/language/nativecall

Apparently it's used to call a function in a native library, with the
code implemented in *.dll on Windows or *.so on a Unix-like system.

Those library files will likely contain the implementation of the C
standard library, or of some other library.  For example, on my system a
simple C "hello world" executable will load libc.so.6, which it will find
at /lib/x86_64-linux-gnu/libc.so.6, which is a symbolic link to
/lib/x86_64-linux-gnu/libc-2.31.so.  All that is likely to be very
different in different implementations.

I fail to see what interrupts have to do with this.

> Maybe if I root around inside the c code that goes
> with time.h, I would be able to find that, but it
> is way over my head.

Reading the C code that implements the functions declared in <time.h>,
even if it didn't go over your head, is unlikely to help you with what
you're doing.  The source code won't tell you how the code generated
from it is packaged into a library file.

Though you're interfacing to C functions, your questions are really
about linking (which is specific to an OS, not to a language) and Raku's
NativeCall interface.  You're likely to get much better help in a forum
that deals with Raku.  https://www.raku.org/community/ looks like a good
place to start.  I think if you mention on the #raku IRC channel that
you're looking for help with NativeCall they can give you some good
pointers.  (And anyone who's familiar with Raku and NativeCall is
likely to know about C as well.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157555

FromT <T@invalid.invalid>
Date2020-12-20 22:15 -0800
Message-ID<rrpehg$kei$1@dont-email.me>
In reply to#157554
On 12/20/20 10:10 PM, Keith Thompson wrote:
> I don't think "system interrupt" is an accurate representation of what
> Raku's NativeCall does.

You are correct.  It is talking to .so's and .dll's

[toc] | [prev] | [next] | [standalone]


#157575

FromBart <bc@freeuk.com>
Date2020-12-21 13:11 +0000
Message-ID<2a1EH.305965$YVH9.53781@fx06.ams4>
In reply to#157550
On 21/12/2020 04:51, Keith Thompson wrote:
> T <T@invalid.invalid> writes:
> [...]
>> I need to cancel this question.  I am making the
>> mistake of calling a library in C (time.h) and what
>> I really need is to call a function inside a
>> /usr/lib64/libxxx.so.x.  And I do not know what
>> that is.  (Google keeps pointing me back to time.h.).

> If I write a simple program, say one that prints "hello world",
> I create a source file, then I compile, link, and run it.  On my
> system:
> 
>      gcc hello.c -o hello
>      ./hello
> 
> gcc, and the tools it invokes, take care of all the details for me.
> 99% of the time I literally do not care where time.h or the library
> that implements the time function exist on the system.
> 
> Find yourself a decent C tutorial and follow it.  There are
> undoubtedly some decent ones online.  Perhaps others can suggest one.
> (There are a lot of really bad ones, and you don't yet know enough
> to tell the difference.)
> 
> Since you're apparently working on interfacing between C and another
> language,

So, you know this, but still felt the need to explain how to compile 
Hello World!

Have you ever had to create bindings for C APIs from a language that 
hasn't provided them?

Especially from a dynamic language where the concept of static linking 
is meaningless, and C macros and typedefs don't exist. You might not 
even have a C compiler. Nor any actual headers for the things you want 
to use.

I can't help the OP because I'm on Windows. There is a 'time_t', but it 
may be very different from Linux.

Also I use WinAPI for the time. Here's an example of calling Windows' 
GetSystemTime() function from my interpreted, dynamic language:

-----------------------------------------
global type ws_systemtime = struct
     word16  year
     word16  month
     word16  dayofweek
     word16  day
     word16  hour
     word16  minute
     word16  second
     word16  milliseconds
end

importdll kernel32=
     windows proc "GetSystemTime"(ref byte)
end

proc start=

     tm:=new(ws_systemtime)
     getsystemtime(&tm)

     println tm.day, tm.month, tm.year
end
-----------------------------------------

This prints: 21 12 2020

Here, the dll name is important, as the interpreter will only look 
inside that specific library (not search across a set of DLLs as is 
common with linkers).

GetSystemTime (shown as a string because it is case-sensensitive inside 
the library, but my source is case-insensitive) is inside kernel32.dll 
according to the docs.

The 'linking' is done at runtime, and on demand, the first time 
GetSytsemTime is called.

For calls to the C library, this language declare those functions inside 
a block like this:

    importdll msvcrt =
        clang function printf(string, ...)int32
        ....
    end

But the language also runs on Linux, and in that case, the interpreter 
knows to map "msvcrt.dll" to "libc.so.6". Fixups are done via 
dlopen/dlsym instead of LoadLibrary/GetProcAddress.

It doesn't need the full path, as dlopen etc will find it.

[toc] | [prev] | [next] | [standalone]


#157621

FromT <T@invalid.invalid>
Date2020-12-21 13:47 -0800
Message-ID<rrr55m$kj0$1@dont-email.me>
In reply to#157575
On 12/21/20 5:11 AM, Bart wrote:
> time_t'

What I was after was what type of variable that was.  How
many bits etc..  But I don't need it anymore as I was
mistaken that I was not addressing a .so or .dll.

> Also I use WinAPI for the time. Here's an example of calling Windows' 
> GetSystemTime()

Now that I can also use as an example of how to get
the time on both Windows and Linux.

Thank you!

[toc] | [prev] | [next] | [standalone]


#157648

Fromscott@slp53.sl.home (Scott Lurndal)
Date2020-12-23 17:18 +0000
Message-ID<vZKEH.27916$Vj2.269@fx16.iad>
In reply to#157621
T <T@invalid.invalid> writes:
>On 12/21/20 5:11 AM, Bart wrote:
>> time_t'
>
>What I was after was what type of variable that was.  How
>many bits etc..  But I don't need it anymore as I was
>mistaken that I was not addressing a .so or .dll.
>
>> Also I use WinAPI for the time. Here's an example of calling Windows' 
>> GetSystemTime()
>
>Now that I can also use as an example of how to get
>the time on both Windows and Linux.

Although the time(2) system call has been replaced by
the gettimeofday(2) system call which offers microsecond
resolution instead of second resolution.

   https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html

Both system calls have been augmented by the POSIX
clock functions (clock_getres, clock_gettime, clock_settime)
which support multiple clock sources and have nanosecond resolution.

   https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html

The 'type' of time_t (insofar as any portable application is concerned):

   https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html

There you'll find these statements about the characteristics of 'time_t':

   "All of the types shall be defined as arithmetic types
    of an appropriate length"

   "clock_t shall be an integer or real-floating type.  time_t shall be an integer type."

Many platforms typedef 'time_t' as long.

[toc] | [prev] | [next] | [standalone]


#157662

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-23 12:05 -0800
Message-ID<87ft3w9tkp.fsf@nosuchdomain.example.com>
In reply to#157648
scott@slp53.sl.home (Scott Lurndal) writes:
> T <T@invalid.invalid> writes:
>>On 12/21/20 5:11 AM, Bart wrote:
>>> time_t'
>>
>>What I was after was what type of variable that was.  How
>>many bits etc..  But I don't need it anymore as I was
>>mistaken that I was not addressing a .so or .dll.
>>
>>> Also I use WinAPI for the time. Here's an example of calling Windows' 
>>> GetSystemTime()
>>
>>Now that I can also use as an example of how to get
>>the time on both Windows and Linux.
>
> Although the time(2) system call has been replaced by
> the gettimeofday(2) system call which offers microsecond
> resolution instead of second resolution.
>
>    https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html

It hasn't been "replaced".  time() is defined by both ISO C and POSIX.
gettimeofday() is defined by POSIX -- and has been marked obsolescent
as of POSIX Issue 7.

C17 adds timespec_get, which gives you the current time with nanosecond
precision (but not necessarily nanosecond accuracy).

> Both system calls have been augmented by the POSIX
> clock functions (clock_getres, clock_gettime, clock_settime)
> which support multiple clock sources and have nanosecond resolution.
>
>    https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
>
> The 'type' of time_t (insofar as any portable application is concerned):
>
>    https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html
>
> There you'll find these statements about the characteristics of 'time_t':
>
>    "All of the types shall be defined as arithmetic types
>     of an appropriate length"
>
>    "clock_t shall be an integer or real-floating type.  time_t shall
>    be an integer type."
>
> Many platforms typedef 'time_t' as long.

POSIX places stricter requirements on time_t than ISO C does.
C only requires time_t to be a real (integer or floating) type
"capable of representing times".  It doesn't even require that
increasing values represent increasing times.

Most platforms typedef time_t as a signed integer type of 32 or, if
possible, 64 bits, representing seconds since the epoch (1970-01-01
00:00:00 UTC).  (Windows has 32-bit long, so I believe it defines
time_t as long long.)  A conforming implementation could define time_t
as a floating-point type, but I'm not aware of any that do so.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#157624

FromT <T@invalid.invalid>
Date2020-12-21 14:25 -0800
Message-ID<rrr7d5$5l1$1@dont-email.me>
In reply to#157575
On 12/21/20 5:11 AM, Bart wrote:
> global type ws_systemtime = struct
>      word16  year
>      word16  month
>      word16  dayofweek
>      word16  day
>      word16  hour
>      word16  minute
>      word16  second
>      word16  milliseconds
> end
> 
> importdll kernel32=
>      windows proc "GetSystemTime"(ref byte)
> end
> 
> proc start=
> 
>      tm:=new(ws_systemtime)
>      getsystemtime(&tm)
> 
>      println tm.day, tm.month, tm.year
> end

Hi Bart,

Beautiful explanation!  And one I can use as an
example to demonstrate Object Orientated (OO)
"class", "object" and "method".   (Raku's explanations
of OO implementation in Raku is miserable to non-existent,
so I am writing my own.)

Thank you!!!

-T

[toc] | [prev] | [next] | [standalone]


#157549

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-12-21 05:45 +0100
Message-ID<rrp9a6$sna$1@dont-email.me>
In reply to#157542
Sorry, Stefan, but you're an idiot. Described in such a
formalized way, no beginner will understand what you mean.

[toc] | [prev] | [next] | [standalone]


#157572

FromBart <bc@freeuk.com>
Date2020-12-21 12:16 +0000
Message-ID<Vm0EH.937700$LMma.271339@fx47.ams4>
In reply to#157542
On 21/12/2020 02:32, T wrote:
> Hi All,
> 
> I am trying to use Raku's native call to read the
> time in Linux.
> 
> 
> I found this C code:
> 
> #include<stdio.h>
> #include<time.h>
> 
> void main()
> {
>      time_t t;
>      time(&t);
>      printf("\n current time is : %s",ctime(&t));
> }
> 
> 
> What is "time_t t"  and why the space?
> 
> What is "time($t)"?
> 
> I also need to know the length in bit.

Docs for these functions may be useful to look at, as they include lots 
of extra info you won't see in C code.

Try googling for 'man time' (probably you need man time(2) as 'time' is 
also a shell command). Or 'msdn time'.

The byte-sizes of these often depend on platform. Then you might need to 
compile and run the C code, for example:

     printf("%d\n",(int)sizeof(time(&t)));

For the bit-size, multiply by 8.

[toc] | [prev] | [next] | [standalone]


#157612

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-12-21 20:03 +0000
Message-ID<87a6u7udt2.fsf@bsb.me.uk>
In reply to#157542
T <T@invalid.invalid> writes:

> I am trying to use Raku's native call to read the
> time in Linux.
>
> I found this C code:
>
> #include<stdio.h>
> #include<time.h>
>
> void main()
> {
>     time_t t;
>     time(&t);
>     printf("\n current time is : %s",ctime(&t));
> }
>
>
> What is "time_t t"  and why the space?

time_t a type (most likely a 64-bit integer type).

> What is "time($t)"?

It's &t.  &t is the address of t -- a pointer to the t object.  The time
function is odd for historical reasons.  It returns the time and also
allows the time to be placed into an object whose address is passed to
the function.

> I also need to know the length in bit.

You don't really need to know anything except this last one.  It is
likely that int64 is the correct Raku type to use.  All the rest is
about Raku's NativeCall, not C:

$ cat time.p6
use NativeCall;

sub time(int64 is rw) returns int64 is native { * }
sub ctime(int64 is rw) returns Str is native { * }
time(my int64 $t);
print ctime($t);
$ perl6 time.p6 
Mon Dec 21 20:02:18 2020

-- 
Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web