Differences Between time, adafruit_datetime, RTC and NTP

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
osiris
 
Posts: 4
Joined: Wed Nov 28, 2012 2:33 am

Differences Between time, adafruit_datetime, RTC and NTP

Post by osiris »

Hey all. I'm just trying to get a better understanding between these different modules. I'll explain a bit more about my plan on using them after. I'm currently using a Feather ESP32-S2 and CP 7.3.x. I've been looking over the documentation and I guess I'm wondering when to implement each and how they interact with each other.

I do know and understand that

Code: Select all

NTP
is the internet protocol and that I'll need to connect to the internet to get it (which I have done so already). I know

Code: Select all

RTC
is for the Real Time Clock (which I'm familiar with from previous projects using the DS23XX(?) chips). I know this is typically a more precise way to keep time and it can be set and continues to work without providing that there is some power to the clock. Where I start to get iffy is for time and it seems to be a simple time keeping module, used in delays and stuff, but it also does have some date functionality (

Code: Select all

time.localtime()
). Then I'm getting hung up on the

Code: Select all

adafruit_datetime
module as it seems to just be another implementation of RTC. It looks like I can set and retrieve time using either, is

Code: Select all

datetime
just a less precise way of doing things?

The reason I ask is that the project I'm working on I'd like to keep time and have times tamped data logging. I'm working on the code bit-by-bit, breaking it down into smaller projects. I already have working code that connects to wifi and returns UTC time from a NTP server. My next plan is to set the time onboard the MCU for when it disconnects from wifi and gathers data until a determined time when it reconnects to wifi. I've seen the example code to set

Code: Select all

RTC
and

Code: Select all

datetime
but I am wondering (in a nutshell) which to use? Will it matter? Am I misunderstanding their purpose?

Thanks all.

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: Differences Between time, adafruit_datetime, RTC and NTP

Post by bidrohini »

I think adafruit_datetime will be more convenient. RTC will need extra hardware no doubt. Moreover, all RTCs are not always accurate. I think, at first you'd better go ahead with adafruit_datetime. If the result is not accurate, you can try RTC. If you use RTC, choose DS3231 or something similar. Those have built-in crystal oscillator. So, those are more accurate than Ds1307 etc.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Differences Between time, adafruit_datetime, RTC and NTP

Post by adafruit_support_mike »

The 'RTC' module belongs to MicroPython and handles basic 8-field translation from timestamps (milliseconds since 00:00:00 1 Jan 1970) to a years-months-days-etc array. I don't see any official mention that it's based on a physical RTC or any guarantee of accuracy:

https://docs.micropython.org/en/latest/ ... e.RTC.html

The 'time' module is a standard Python module roughly equivalent to, but with more functions than, the 'RTC' module. Again, it just reads values provided by the OS with no guarantee of accuracy:

https://docs.python.org/3.8/library/tim ... odule-time

The 'datetime' module has a much richer set of functions for calculating time and intervals:

https://docs.python.org/3.8/library/datetime.html

The 'adafruit_datetime' module is our fork of 'datetime' scaled down for microcontrollers. Again, it's made for calculation using timestamps rather than timekeeping in its own right.

The 'NTP' module handles communication with a timeserver. As such, it does provide some guarantees of accuracy and syncronization with other devices that also use NTP time.

To get information from a physical RTC like the DS3231 you need to use a module that can talk to the device, like our 'adafruit_ds3231' module:

https://github.com/adafruit/Adafruit_Ci ... hon_DS3231

That module uses timestamps that are compatible with the previous modules. You can set the physical RTC's timestamp by assigning a 'time' module datestamp to the 'adafruit_ds3231' module's 'datetime' attibute:

Code: Select all

    rtc.datetime = time.struct_time((2017,1,9,15,6,0,0,9,-1))
then read the 'datetime' attribute later to get readings from the physical RTC.


So the basic division is between modules that perform calculations on timestamps (RTC, time, datetime, adafruit_datetime) and modules that communicate with timekeeping devices (NTP, adafruit_ds3231).

User avatar
tannewt
 
Posts: 3315
Joined: Thu Oct 06, 2016 8:48 pm

Re: Differences Between time, adafruit_datetime, RTC and NTP

Post by tannewt »

CircuitPython also has an rtc module and rtc.RTC class to manage the on board RTC: https://docs.circuitpython.org/en/lates ... index.html

Its accuracy will depend on whether the board has an external crystal. Usually the onboard RTC doesn't have a battery backup either. Some of the Stemma QT RTC boards do have it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”