Salem's suggestion of gmtime is great if you need this for work or something. But I'm guessing this is for school/your own learning, in which case I think coding your own would be beneficial. However, I would start with an existing algorithm and simply implement it.
Take another look at that link I sent, and pay attention to the "useful concepts" section, which talks about corresponding months and years, and gives month/year tables. That should give you some clue about whether you increment your weekday counter by one (common year) or two (leap year), depending on which month you're concerned with.
You're on the right track, starting with a known date and calculating from there. However, I wouldn't use a single subtraction of the number of years in between. For example, a gap of 7 years may have 0, 1 or 2 leap years in there. Have a variable called first_tuesday, set to the known first tuesday (4 in your example). Write a loop that iterates through the years, from the known start year to the target year (1788 to 1792 in your example), and decrements first_tuesday by 1 or 2 depending on whether you have to account for a leap year. A simple if statement will handle the "0 means 7 case"
Code:
if first_tuesday < 1 // went before first of month
first_tuesday += 7
So, e.g., Nov 0th was a Tuesday, then so is Nov 7th, the real first Tuesday. Similartly, Nov -1st becomes Nov 6th.