Depending what relay device you decide to use (I use a MIMOLite w/the built in open/close sensor), ST’s own Smart Lighting app can be used to automatically close the door. I do that, but instead of using an open/close sensor I wait for motion to stop for 20 minutes, and then I close the door. There are also options I can use to only do that during a certain time, like what you describe in situation A.
For situation B, you’ll be able to use a Community developed SmartApp called CoRE to build that rule. Technically CoRE can do both A and B if you want to get really creative.