How to calculate Night Flight time for a given flight?
This answer will address your question according to US FAA guidelines. Make sure to verify with your country’s aviation agency.
Civil Twlight
The time that you can log as “night” is based on the start or end of “civil twilight.” Civil twilight is defined as the time when the geometric center of the Sun’s disk is at most 6 degrees below the horizon. Civil twilight, as a general rule, is usually the 30 minutes before sunrise or after sunset. However, that can vary quite a bit with your location on the Earth’s surface.
Logged Time
Before (morning) or after (evening) civil twilight and between the two is when you can log night flight. You can take your total time (ie 2.3 Hobbs) and subtract any time that would not count as night and the result is what you are legally allowed to log as night time (at least here in the US).
Application for Development
I understand you're problem as a developer - you will have to use a website like timeanddate.com to grab an API that can find a local civil twlight using a city as an input. You will have to write a function that takes the departure time and determines whether it counts as night time, and then subtract any flight time that is ineligible.
I hope this addressed the mathematical request of your question and let me know if I can clarify at all.