LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2019, 08:18 AM   #16
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,071

Original Poster
Rep: Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574

Quote:
Originally Posted by michaelk View Post
It sort of makes some sense, but I really can't completely follow what it means beyond the short descriptions under each picture. And even then, I'm not entirely sure how to interpret it.

Quote:
https://en.wikipedia.org/wiki/System_time

In simple terms the CPU as built in timers and after so many clock cycles an internal CPU register is incremented. One of these registers is the system time. Each bit has a value and typically with modern computers it is nanoseconds or microseconds. When the counter is zero it is assigned midnight 1 January 1970 UTC which is known as the epoch time. The definition of system time is the number of seconds since the epoch. This is how a computer gets the concept of a second.
...
I'm not clear on what's meant by "the number of seconds since the epoch". And other than it has something to do with the number of "ticks"; I don't know what to make of the Wikipedia article, other than 1 January 1970 00:00:00 UT is some kind of "starting point" (?), I don't know. I'm sorry, but I don't know what to make of "epoch". Do "ticks" mean "cycles" ?

Thanks again for your help though.

Quote:
Originally Posted by rtmistler View Post
Recommend you please read the sleep.c code references by michaelk (post #10) and ask more specific questions, the file is under 200 lines of code, much of which are defines.
Well I did try, but beyond the defines, the code on GitHub for nanosleep.c just doesn't make much sense to me. I have no idea what to make of the structures, other than there is some structures there. I can't visualize what they even look like, like what the actual structures are verses the member variables for them. And therefore trying to understand the functions is impossible.

For sleep.c I just don't know what a lot of the functions are even doing, or what "die" means. So for example, bindtextdomain() and textdomain() just to name a few, I have no idea what they are actually doing there. So I can't visualize what that code is actually doing either.

Quote:
You can put printf wherever you wish, what's wrong with it right now?
I meant instead of it printing multiple times "Time is now: xx:xx:xx", it just prints that message once, on only one line, BUT it still updates the values for now.hour, now.minute, now.second member variables.

If I move that printf statement out of the loop, it will only print the time as it was when the loops stopped - and therefore it would update the time as it's being counted. Think of it like, you start the stopwatch on your phone or whatever, it doesn't print multiple lines on the screen, it just changes the numbers for the seconds, etc (on the same line it originally printed them on). That's what I mean.
 
Old 09-21-2019, 08:19 AM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,860

Rep: Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344
Off: A possible improvement:
Code:
          while ( now.second >= 60 ) {
                     now.second -= 60;
                     ++now.minute;
          }
          while ( now.minute >= 60 ) {
                     now.minute -= 60;
                     ++now.hour;
          }
          if ( now.hour >= 3 ) {
              break;
          }
Or maybe:
Code:
          if ( now.second >= 60 ) {
                     now.minute += now.second / 60;
                     now.second %= 60;
          }
          if ( now.minute >= 60 ) {
                     now.hour   += now.minute / 60;
                     now.minute %= 60;
          }
          if ( now.hour >= 3 ) {
              break;
          }
 
2 members found this post helpful.
Old 09-21-2019, 08:47 AM   #18
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,038
Blog Entries: 13

Rep: Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488
In computer science the epoch is a date representing the start of time.

For uSoft this date is 1/1/1980, Edit: or so I thought, I see 1/1/1970 there too, hence why I fairly much look it up at times when it matters.

For Unix/Linux this date is 1/1/1900.

There may be more, I care not and instead I'd test or look it up each time I needed to work with them.

textdomain() and associated functions are library functions which you can look up.

Not saying they're easy, but one can look them up, determine that they're not exactly related to computer time derivations, but instead domain names, and then likely part of NTP operation.

Last edited by rtmistler; 09-21-2019 at 08:53 AM.
 
1 members found this post helpful.
Old 09-21-2019, 09:00 AM   #19
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,038
Blog Entries: 13

Rep: Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488
Quote:
Originally Posted by jsbjsb001 View Post
I meant instead of it printing multiple times "Time is now: xx:xx:xx", it just prints that message once, on only one line, BUT it still updates the values for now.hour, now.minute, now.second member variables.

If I move that printf statement out of the loop, it will only print the time as it was when the loops stopped - and therefore it would update the time as it's being counted. Think of it like, you start the stopwatch on your phone or whatever, it doesn't print multiple lines on the screen, it just changes the numbers for the seconds, etc (on the same line it originally printed them on). That's what I mean.
I don't spend much time trying to be fancy in the terminal, but you can not give it a newline and then each print will overwrite that line, but also you should clean it up to avoid seeing like 1 and 7 showing together.

I think you can go to the end of what you have backspace however far you need, and then print cleanly.

If I really wish to make it all look really appealing, I develop using a graphical library.
 
1 members found this post helpful.
Old 09-22-2019, 02:35 AM   #20
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,071

Original Poster
Rep: Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574
So would it be right to say in a nutshell; a certain amount of "ticks" would mean "a second" to you and me ?

Thank you NevemTeve, while I think I get your first suggested improvement; I'm not quite sure about your second suggested improvement. As my understanding of division and the Modulus operator is still limited. So I'll have to take it on "faith".

Quote:
Originally Posted by rtmistler View Post
...but you can not give it a newline and then each print will overwrite that line, but also you should clean it up to avoid seeing like 1 and 7 showing together.
While I think I get what you mean in the first part of what you said above; I'm not sure what you mean in the last part of what you said, or how exactly I actually do what you've said above.

Quote:
I think you can go to the end of what you have backspace however far you need, and then print cleanly.
Are you talking about the \b (backspace) escape character?

Quote:
If I really wish to make it all look really appealing, I develop using a graphical library.
Well, I do want to write some GUI based programs, but I've been trying to clear up any remaining misunderstandings I have with the "fundamental concepts" like loops, if/switch statements, structures, arrays, pointers, etc. But that said, yes I would like to learn about writing GUI programs once I've got a better understanding of the above.

Thanks again for your help RT.
 
Old 09-22-2019, 07:10 AM   #21
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,038
Blog Entries: 13

Rep: Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488
Quote:
Originally Posted by jsbjsb001 View Post
Are you talking about the \b (backspace) escape character?
It might work.

Print an entry, don't use a newline.

Next entry, if only the last character has changed, print one backspace, and then the new character.

You can go back 2 once you hit double digits, you backspace twice.

I'd try it, but wouldn't go crazy with it.

What I was concerned about was if the backspace doesn't erase. If not, you'll have to write a space to clear, backspace again, and write your new character.

To me, it's valid logic programming if you wish to explore. I'm not in a situation where I can show any example.
Quote:
Originally Posted by jsbjsb001 View Post
Well, I do want to write some GUI based programs, but I've been trying to clear up any remaining misunderstandings I have with the "fundamental concepts" like loops, if/switch statements, structures, arrays, pointers, etc. But that said, yes I would like to learn about writing GUI programs once I've got a better understanding of the above.
I actually recommend learning command line programming first. There's things like multithreading which are best learned prior to that giving this a try.

My point was more, "OK. Make the text look pretty, but not sure I recommend much time and effort into that."

Printing CORRECT time entries has validated your loop. That exercise should be done.
 
1 members found this post helpful.
Old 09-22-2019, 07:46 AM   #22
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
NevemTeve,
Your suggestions are good but since ths OP is not polling or using a interrupt to get a time value I think it over complicates the code a bit.

You can use \r instead of \n to move the cursor to the beginning of the line but it does not erase existing characters. You also need to print the time as two digits 02:09:15 so it always looks right
 
1 members found this post helpful.
Old 09-22-2019, 10:52 AM   #23
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,038
Blog Entries: 13

Rep: Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488
What I'm concerned about is that if one uses CR, leaving something like 02:09:59, when they print 02:10:00 it will appear like 02:08:88 because of the former characters already there.
 
Old 09-24-2019, 10:44 AM   #24
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,071

Original Poster
Rep: Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574
Thanks again guys.

What do you mean by "CR" RT? As I'm not honestly sure.

While I might have a little play around with the escape characters mentioned above; I think you're right RT - so I don't think I'll go too crazy with that. I figure it's probably better to focus on the fundamentals at least for now, rather than get bogged down in the "cosmetics".

I do have some more questions about structures and pointers, as well as a few other questions, but I'll start another thread later on for those questions.

But just before I wrap this thread up; could someone just tell me if what I've quoted below is right/near enough or not? Because I would like to try and clarify my thinking there.

Quote:
Originally Posted by jsbjsb001 View Post
So would it be right to say in a nutshell; a certain amount of "ticks" would mean "a second" to you and me ?
 
Old 09-24-2019, 11:16 AM   #25
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
CR - Carriage Return. This dates back to the manual typewriter days when the carriage physically moved as you typed. There would be a physical lever that you pushed to move it back to the other side of the page. In terminal speak the cursor moves to the left side (at least for most locales).

In addition to moving the carriage back to the other side the typewriter lever would also advance the page up a line which in terminal speak is called a line feed.

In a text editor or word processor pressing the enter key moves the cursor to the beginning of the line and advances it down the page one line...

\n - carriage return+line feed
\r - carriage return only

As posted these are known as escape sequences.

https://en.wikipedia.org/wiki/Escape_sequences_in_C

Yes, in a nutshell a certain number of ticks is a second.

FYI:
https://en.wikipedia.org/wiki/Typewriter

Last edited by michaelk; 09-24-2019 at 11:26 AM.
 
2 members found this post helpful.
Old 09-24-2019, 01:48 PM   #26
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,179
Blog Entries: 18

Rep: Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895
You may find this interesting background reading:
https://linux.die.net/man/8/adjtimex

Before the days of constant internet connections and casual use of ntpd to keep things in sync, we had to use this to fine tune our system clocks to reduce drift.
 
1 members found this post helpful.
Old 09-24-2019, 02:12 PM   #27
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,038
Blog Entries: 13

Rep: Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488Reputation: 3488
Agree with michaelk's reply.

And per your possible experimentation, please do so.

The only note I'll make here is that you can potentially see different terminal attributes where the CR and printing again may not completely erase and overwrite, but instead add on to the information that is already showing. Thus if you had an "8", and wrote a "1", you'd still see "8" because the "8" has filled all the pixels already so you could not see the change.

I've found this to occur on very limited terminals and it probably has to do with the attributes for the terminal. Either case, I've learned to verify, for cases where it matters, or to try and write defensive code for multiple cases.

I think that's too much to worry about there, just a minor note that you may see pollution like I'm describing, or you may not.
 
1 members found this post helpful.
Old 09-25-2019, 02:57 AM   #28
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,071

Original Poster
Rep: Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574Reputation: 1574
Thanks again michaelk and RT! And also thanks again for your help/insights GazL!

While like I said before I'll have a little play with printf() and the escape characters, I won't spend too much time on it.

In any case, and since my questions I think have been answered; I'll mark this thread as [SOLVED].

Thanks again guys!
 
  


Reply

Tags
c programming, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to understand user time, sys time, wait time, idle time of CPU guixingyi Linux - Server 1 08-24-2010 10:10 AM
System time vs Hardware time and Daylight Savings Time Toadman Linux - General 6 03-17-2007 08:12 AM
System time vs Hardware time and Daylight Savings Time Toadman Linux - Networking 6 03-16-2007 07:14 PM
Time, time, time raylhm SUSE / openSUSE 5 11-02-2006 03:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration