|
| 1 | +/* |
| 2 | + The time conversion of normalized time to the railway is a simple algorithm |
| 3 | + because we know that if the time is in 'AM' value it means they only want |
| 4 | + some changes on hours and minutes and if the time in 'PM' it means the only |
| 5 | + want some changes in hour value. |
| 6 | +
|
| 7 | + Input Formate -> 07:05:45PM |
| 8 | + Output Fromate -> 19:05:45 |
| 9 | +
|
| 10 | + Problem & Explanation Source : https://www.mathsisfun.com/time.html |
| 11 | +*/ |
| 12 | + |
| 13 | +/** |
| 14 | + * RailwayTimeConversion method converts normalized time string to Railway time string. |
| 15 | + * @param {String} timeString Normalized time string. |
| 16 | + * @returns {String} Railway time string. |
| 17 | + */ |
| 18 | +const RailwayTimeConversion = (timeString) => { |
| 19 | + // firstly, check that input is a string or not. |
| 20 | + if (typeof timeString !== 'string') { |
| 21 | + return new TypeError('Argument is not a string.') |
| 22 | + } |
| 23 | + // split the string by ':' character. |
| 24 | + const [hour, minute, scondWithShift] = timeString.split(':') |
| 25 | + // split second and shift value. |
| 26 | + const [second, shift] = [scondWithShift.substr(0, 2), scondWithShift.substr(2)] |
| 27 | + // convert shifted time to not-shift time(Railway time) by using the above explanation. |
| 28 | + if (shift === 'PM') { |
| 29 | + if (parseInt(hour) === 12) { return `${hour}:${minute}:${second}` } else { return `${parseInt(hour) + 12}:${minute}:${second}` } |
| 30 | + } else { |
| 31 | + if (parseInt(hour) === 12) { return `00:${minute}:${second}` } else { return `${hour}:${minute}:${second}` } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +module.exports = RailwayTimeConversion |
0 commit comments