The criteria for the times (start and end) of each prayer is defined by the following hadith in Sahih Muslim:
وعَنْ عَبْدِ اللَّهِ بْنِ عَمْرٍو، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم قَالَ: وَقْتُ الظُّهْرِ إِذَا زَالَتِ الشَّمْسُ وَكَانَ ظِلُّ الرَّجُلِ كَطُولِهِ مَا لَمْ يَحْضُرِ الْعَصْرُ وَوَقْتُ الْعَصْرِ مَا لَمْ تَصْفَرَّ الشَّمْسُ وَوَقْتُ صَلاَةِ الْمَغْرِبِ مَا لَمْ يَغِبِ الشَّفَقُ وَوَقْتُ صَلاَةِ الْعِشَاءِ إِلَى نِصْفِ اللَّيْلِ الأَوْسَطِ وَوَقْتُ صَلاَةِ الصُّبْحِ مِنْ طُلُوعِ الْفَجْرِ مَا لَمْ تَطْلُعِ الشَّمْسُ فَإِذَا طَلَعَتِ الشَّمْسُ فَأَمْسِكْ عَنِ الصَّلاَةِ فَإِنَّهَا تَطْلُعُ بَيْنَ قَرْنَىْ شَيْطَانٍ
'Abdullah ibn 'Amr reported: The Messenger of Allah (ﷺ) said: "The time of the noon prayer is when the sun passes the meridian and a man's shadow is the same (length) as his height, (and it lasts) as long as the time for the afternoon prayer has not come; the time for the afternoon prayer is as long as the sun has not become pale; the time of the evening prayer is as long as the twilight has not ended; the time of the night prayer is up to the middle of the average night and the time of the morning prayer is from the appearance of dawn, as long as the sun has not risen; but when the sun rises, refrain from prayer for it rises between the horns of the devil."
— Sahih Muslim, Book 5, Hadith 222
As you can see, not all prayers end when the prayer that follows starts. This is the case only for dhuhr prayer.
As for the fajr and 'asr prayer in specific, there is waqt ikhtiyār (preferred time of choice) and waqt darūra (time of necessity). For example, the time of necessity goes from the end of the 'asr prayer time as per the hadith above (the time the sun turns yellow) and extends till the time of maghrib prayers based on the hadith in Sahih Muslim:
عَنْ أَبِي هُرَيْرَةَ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم قَالَ: مَنْ أَدْرَكَ رَكْعَةً مِنَ الصُّبْحِ قَبْلَ أَنْ تَطْلُعَ الشَّمْسُ فَقَدْ أَدْرَكَ الصُّبْحَ وَمَنْ أَدْرَكَ رَكْعَةً مِنَ الْعَصْرِ قَبْلَ أَنْ تَغْرُبَ الشَّمْسُ فَقَدْ أَدْرَكَ الْعَصْرَ
Abu Huraira reported: The Messenger of Allah (ﷺ) said: "He who finds one rak'ah at dawn before the rising of the sun, he, in fact, finds the dawn prayer. and he who finds one rak'ah of the afternoon prayer before sunset, he, in fact, finds the afternoon prayer."
— Sahih Muslim, Book 5, Hadith 209
It is worth noting that the Hanafi school is different from the Maliki, Shafi'i, and Hanbali school in the calculation method of the start of 'asr and 'ishā' prayer:
- 'Asr prayer time:
- Maliki, Shafi'i, Hanbali, and some Hanafis: start of 'asr prayer time is the end of dhuhr time until the sun turns yellow
- Rest of Hanafis: 'asr prayer time starts when a man's shadow is twice the length as his height until sunset or start of maghrib prayer time.
- 'Ishā' prayer time:
- Maliki, Shafi'i, Hanbali, and some Hanafis: start of 'ishā' prayer time is the setting of the red twilight.
- Rest of Hanafis: start of 'ishā' prayer time is the setting of the white twilight (which is a few minutes later).
There are libraries of code available online (e.g., PrayersTime Code) that can assist you in your programming endeavor.