Time Reference for Python "Datetime" module

Can anyone tell me where the “Datetime” module, included in a lot of flavours of Python and Micropython, gets its time and date information from? Common sense tells me that access to a source, such as NTP, should be necessary but all the articles I have read so far focus on the format and extraction of the time/date information, not where it comes from.



I think that depends on your setup, hardware and software wise.
On one Pico setup I have a RV3028 RTC breakout and do the following.

from breakout_rtc import BreakoutRTC
from machine import RTC
RV3028 = BreakoutRTC(i2c)
rtc = BreakoutRTC(i2c)

hours = rtc.get_hours()
minutes = rtc.get_minutes()
month = rtc.get_month()
date = rtc.get_date()    

That gets me reliable time without having to use NTP or having to sync the Pico’s RTC with a host.

If your just using the Pico’s onboard RTC, rtc = machine.RTC() , you need to sync it to the correct time via NTP etc, as it doesn’t have any backup battery.

For those systems that do not have a RTC breakout or able to access outside sources, you will need to set the time manually.

My Tufty2040 when USB connected with Thonny, uses the connected computer’s time. Otherwise… yup.

And also, you’ll start to notice time drift for those manually configured units.

Thank you Gents. I must admit my question was partly academic. I have recently been playing with Plasm2040’s that I have incorporated it into a light/clock which uses a PCF8523 RTC. I have powered this on occasion from a battery and the thought crossed my mind that if the flavour of micropython that the Plsma2040 uses incorporated the Datetime facility (and I don’t know that it does!) then the RTC chip could be superfluous. This led on to “where does the “Datetime” module get its information from”? As I say mainly academic!

No worries!!.. it is good to ask questions. Never know where they might take you…

My Tufty will sync the time from the host PC “if” I run my code from Thonny. Even then it drifts, gaining time a lot quicker than I had expected.

Have PICO get time from host over USB connection? - Support - Pimoroni Buccaneers