You may remove the loop and setup the tm$ yourself to verify values. It gets coverted to UNIX time, and then the outcome is converted back.
Gregorian calendar was introduced in the year 1582, and Julian calendar was introduced by Julius Caesar in 46 BC. Julian date 0 is in the year -4712.... There has been a lot of hassling with the calendars during the ages. This Julian calculation with a Gregorian sauce on top calculates the current leap years (UNIX to timestamp routine). You might discuss if that is accurate befoure introducing it before 1582, I did not research that.
Wikipedia and other sites have a lot of information.