46 years as a software developer (over half doing C/C++) and this is the most succinct and complete example of the UNIX/LINUX time code without the pages and pages of boilerplate. You are a true craftsman!
I've been searching a video to print current time of my system from past 2 days. Then, suddenly your video got recommended today and I must say that this video is an upgrade of what I wanted in first place. Best tutorial on this topic.❤ Thanks a lot.
The "strftime()" at @16:50 comes in very handy for embedded designers because they can specifically tailor the output based on various features requirements. I will definitely consider using it in my next project. In embedded design we usually use a chip called Real-Time-Clock (RTC) and follow instructions on data sheet to how to format the output. This makes it very powerful to interface the RTC of the PC with the Embedded device for real embedded action.
Thank you for the great video I was playing around with this for hours and learnt so much. With the strftime() function all of the format specifiers are identical to those given in the man page for the date command. The specifiers for its default output are "%a %d %b %Y %X %Z" if anyone is interested. Hope this helps
@20:24 This is very useful to measure events or signal parameters, we call it Timer Capture and Compare in Embedded design, where the MCU counts the number of clocks elapsed between the start of measuring time and the end of measurement, or counting the number of clocks over the incident latency. We use the Capture and Compare to also create certain wave forms with specific parameters and accuracy of near microsecond.
@@cd-stephen Maybe one day I could do an introductory video on making drivers, but that's not my specialty so that's about as far as I could go with it I think. 🙂
I just put sleep(1) between the start and end clocks expecting the total after to around 1.0s but I got much smaller values. What am I missing. Also if I execute the program multiple times the value for total varies even though sleep(1) should a constant time. Are the clock ticks not constant? Or maybe the waiting that the sleep function executes does not prevent the next clock to be executed somehow? Ah, so if sleep pauses the execution of a program would that mean that it stops somehow counting the clock ticks? so the value for the end clock is then not the one for the amount of ticks that would have been executed in the 1 sec equivalent?
I actually didn't know the answer to this myself, so I had to Google it, and this Stackoverflow thread offers a couple solutions... a possible function use and a POSIX library function that can be used: stackoverflow.com/questions/1952290/how-can-i-get-utctime-in-millisecond-since-january-1-1970-in-c-language. So I would try our these methods. I think maybe I'll make a video on this someday. :-)
sleep(2) does not work for me. It creates an endless loop... Edit: Ok, the problem was that I didn't put a at the end of a printf statement, and C somehow cant write into the same line again, when there was a sleep in between.
We seed the random number generator to ensure that it gives us different random numbers each time we run the program. We typically seed it with a value that will be unique each time we run our program, like for example the current time. This video on random number generation explains more: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Mp3eGLX-OpY.html. :-)
Hi I tried the code from this video in the Arduino IDE. I want to show the current time and date. However, the output is showing the time and date from January 1970. Any ideas on how I can fix this code? void loop() { time_t now = time(NULL); char *string_now = ctime(&now); Serial.println(string_now); }
Great question Gurmukh! 🙂 I'm not sure exactly how the time.h library will work with Arduino and the Ardunio IDE, or whether it will work at all. There is this other time library for Arduion that people seem to mention online, maybe this would be better to use instead of time.h: github.com/PaulStoffregen/Time.
@@PortfolioCourses Thank you so much for the quick response. The time libraries from this link that you have provided look useful I will check them out. Thank you so much you for all your help! 🙂
Hahahaha I hear you… C is sort of the “grandpa” of programming languages, it’s old and does things in old ways and needs some extra help to get around. :-)
I do not have a discord and I am not able to provide tutoring right now due to time constraints, but I do try to answer questions that are posted as comments on videos. :-)