|
| 1 | +Function ConvertNumberOfSecondsToBreakdownOfYearsDaysHoursMinutesSeconds(ByRef intYears, ByRef intDays, ByRef intHours, ByRef intMinutes, ByRef objOutputSeconds, ByVal objInputTotalSeconds) |
| 2 | + 'region FunctionMetadata #################################################### |
| 3 | + ' Assuming that objInputTotalSeconds is an integer that indicates a total number of |
| 4 | + ' seconds, this function breaks down the seconds into the integer number of years, days, |
| 5 | + ' hours, minutes, and seconds that add up to the specified number of total seconds. |
| 6 | + ' |
| 7 | + ' NOTE: For simplicity, and to account for leap years, this function assumes that there are |
| 8 | + ' 365.25 days in a year |
| 9 | + ' |
| 10 | + ' The function takes six positional arguments: |
| 11 | + ' - The first argument (intYears) is populated with the rounded-down number of years that |
| 12 | + ' comprise the number of seconds specified in the sixth argument |
| 13 | + ' (objInputTotalSeconds). For simplicity, and to account for leap years, this function |
| 14 | + ' assumes that there are 365.25 * 24 * 60 * 60 = 31557600 seconds in a year. For |
| 15 | + ' example, if the sixth argument (objInputTotalSeconds) is 2047483645, then the first |
| 16 | + ' argument (intYears) is 64. That's because 2047483645 / 31557600 = 64.88084, which |
| 17 | + ' is 64 after rounding-down to the nearest whole year. |
| 18 | + ' - The second argument (intDays) is populated with the rounded-down number of days |
| 19 | + ' remaining after subtracting the number of years that comprise the number of seconds |
| 20 | + ' specified in the sixth argument (objInputTotalSeconds). For example, if the sixth |
| 21 | + ' argument (objInputTotalSeconds) is 2047483645, then the first argument (intYears) is |
| 22 | + ' 64 as noted previously and the second argument (intDays) is 321. That's because 64 |
| 23 | + ' years is (64 * 31557600) = 2019686400 seconds. Then, 2047483645 - 2019686400 leaves a |
| 24 | + ' remainder of 27797245 seconds. 27797245 / 60 / 60 / 24 = 321.72737, which is 321 after |
| 25 | + ' rounding down |
| 26 | + ' - The third argument (intHours) is populated with the rounded-down number of hours |
| 27 | + ' remaining after subtracting the number of years that comprise the number of seconds |
| 28 | + ' specified in the sixth argument (objInputTotalSeconds), then subracting the |
| 29 | + ' remaining number of days. For example, if the sixth argument (objInputTotalSeconds) |
| 30 | + ' is 2047483645, then the first argument (intYears) is 64 as noted previously, and the |
| 31 | + ' second argument (intDays) is 321 as noted previously, and the third argument |
| 32 | + ' (intHours) is 17. That's because 64 years and 321 days is (64 * 31557600) + |
| 33 | + ' (321 * 24 * 60 * 60) = 2047420800 seconds. Then, 2047483645 - 2047420800 leaves a |
| 34 | + ' remainder of 62845 seconds. 62845 / 60 / 60 = 17.45694, which is 17 after rounding |
| 35 | + ' down |
| 36 | + ' - The fourth argument (intMinutes) is populated with the rounded-down number of minutes |
| 37 | + ' remaining after subtracting the number of years that comprise the number of seconds |
| 38 | + ' specified in the sixth argument (objInputTotalSeconds), then subracting the remaining |
| 39 | + ' number of days, then subtracting the remaining number of hours. For example, if the |
| 40 | + ' sixth argument (objInputTotalSeconds) is 2047483645, then the first argument |
| 41 | + ' (intYears) is 64 as noted previously, the second argument (intDays) is 321 as noted |
| 42 | + ' previously, the third argument (intHours) is 17 as noted previously, and the fourth |
| 43 | + ' argument (intMinutes) is 27. That's because 64 years, 17 days, and 8 hours is |
| 44 | + ' (64 * 31557600) + (321 * 24 * 60 * 60) + (17 * 60 * 60) = 2047482000 seconds. Then, |
| 45 | + ' 2047483645 - 2047482000 leaves a remainder of 1645 seconds. |
| 46 | + ' 1645 / 60 = 27.41667, which is 27 after rounding down |
| 47 | + ' - The fifth argument (objOutputSeconds) is populated with the number of seconds |
| 48 | + ' remaining after subtracting the number of years that comprise the number of seconds |
| 49 | + ' specified in the sixth argument (objInputTotalSeconds), then subracting the |
| 50 | + ' remaining number of days, then subtracting the remaining number of hours, then |
| 51 | + ' subtracting the remaining number of minutes. For example, if the sixth argument |
| 52 | + ' (objInputTotalSeconds) is 2047483645, then the first argument (intYears) is 64 as |
| 53 | + ' noted previously, the second argument (intDays) is 321 as noted previously, the third |
| 54 | + ' argument (intHours) is 17 as noted previously, the fourth argument (intMinutes) is 27 |
| 55 | + ' as noted previously, and the fifth argument (objOutputSeconds) is 25. That's because |
| 56 | + ' 64 years, 321 days, 17 hours, and 27 minutes is (64 * 31557600) + |
| 57 | + ' (321 * 24 * 60 * 60) + (17 * 60 * 60) + (27 * 60) = 2047483620 seconds. Then, |
| 58 | + ' 2047483645 - 2047483620 leaves a remainder of 25 seconds |
| 59 | + ' - The sixth argument (objInputTotalSeconds) is the number of seconds to convert to a |
| 60 | + ' breakdown of years, days, hours, minutes, and remaining seconds |
| 61 | + ' |
| 62 | + ' The function returns a 0 if the conversion of total seconds to a breakdown of years, |
| 63 | + ' days, hours, minutes, and remaining seconds was successful. The function returns a |
| 64 | + ' negative number if the conversion was not successful. |
| 65 | + ' |
| 66 | + ' Example: |
| 67 | + ' intTotalSecondsToConvert = 2047483645 |
| 68 | + ' intReturnCode = ConvertNumberOfSecondsToBreakdownOfYearsDaysHoursMinutesSeconds(intYears, intDays, intHours, intMinutes, objOutputSeconds, intTotalSecondsToConvert) |
| 69 | + ' If intReturnCode = 0 Then |
| 70 | + ' ' Successfully converted the number of seconds to a breakdown of years, days, |
| 71 | + ' ' hours, minutes and remaining seconds |
| 72 | + ' WScript.Echo(intTotalSecondsToConvert & " is equivalent to " & intYears & " years, " & intDays & " days, " & intHours & " hours, " & intMinutes & " minutes, and " & objOutputSeconds & " seconds.") |
| 73 | + ' ' The script outputs: |
| 74 | + ' ' 2047483645 is equivalent to 64 years, 321 days, 17 hours, 27 minutes, and 25 |
| 75 | + ' ' seconds |
| 76 | + ' End If |
| 77 | + ' |
| 78 | + ' Version: 1.0.20210724.0 |
| 79 | + 'endregion FunctionMetadata #################################################### |
| 80 | + |
| 81 | + 'region License #################################################### |
| 82 | + ' Copyright 2021 Frank Lesniak |
| 83 | + ' |
| 84 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 85 | + ' software and associated documentation files (the "Software"), to deal in the Software |
| 86 | + ' without restriction, including without limitation the rights to use, copy, modify, merge, |
| 87 | + ' publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 88 | + ' persons to whom the Software is furnished to do so, subject to the following conditions: |
| 89 | + ' |
| 90 | + ' The above copyright notice and this permission notice shall be included in all copies or |
| 91 | + ' substantial portions of the Software. |
| 92 | + ' |
| 93 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 94 | + ' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 95 | + ' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 96 | + ' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 97 | + ' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 98 | + ' DEALINGS IN THE SOFTWARE. |
| 99 | + 'endregion License #################################################### |
| 100 | + |
| 101 | + 'region DownloadLocationNotice #################################################### |
| 102 | + ' The most up-to-date version of this script can be found on the author's GitHub repository |
| 103 | + ' at https://github.com/franklesniak/sysadmin-accelerator |
| 104 | + 'endregion DownloadLocationNotice #################################################### |
| 105 | + |
| 106 | + 'region Acknowledgements #################################################### |
| 107 | + ' None! |
| 108 | + 'endregion Acknowledgements #################################################### |
| 109 | + |
| 110 | + 'region DependsOn #################################################### |
| 111 | + ' TestObjectIsAnyTypeOfNumber() |
| 112 | + 'endregion DependsOn #################################################### |
| 113 | + |
| 114 | + Const NUM_SECONDS_IN_YEAR = 31557600 ' = 365.25 * 24 * 60 * 60 |
| 115 | + Const NUM_SECONDS_IN_DAY = 86400 ' = 24 * 60 * 60 |
| 116 | + Const NUM_SECONDS_IN_HOUR = 3600 ' = 60 * 60 |
| 117 | + Const NUM_SECONDS_IN_MINUTE = 60 |
| 118 | + |
| 119 | + Dim intFunctionReturn |
| 120 | + Dim objRemainingSeconds |
| 121 | + Dim intWorkingYears |
| 122 | + Dim intWorkingDays |
| 123 | + Dim intWorkingHours |
| 124 | + Dim intWorkingMinutes |
| 125 | + |
| 126 | + intFunctionReturn = 0 |
| 127 | + |
| 128 | + If TestObjectIsAnyTypeOfNumber(objInputTotalSeconds) <> True Then |
| 129 | + intFunctionReturn = -1 |
| 130 | + Else |
| 131 | + intWorkingYears = Int(objInputTotalSeconds / NUM_SECONDS_IN_YEAR) |
| 132 | + objRemainingSeconds = objInputTotalSeconds - (intWorkingYears * NUM_SECONDS_IN_YEAR) |
| 133 | + intWorkingDays = Int(objRemainingSeconds / NUM_SECONDS_IN_DAY) |
| 134 | + objRemainingSeconds = objRemainingSeconds - (intWorkingDays * NUM_SECONDS_IN_DAY) |
| 135 | + intWorkingHours = Int(objRemainingSeconds / NUM_SECONDS_IN_HOUR) |
| 136 | + objRemainingSeconds = objRemainingSeconds - (intWorkingHours * NUM_SECONDS_IN_HOUR) |
| 137 | + intWorkingMinutes = Int(objRemainingSeconds / NUM_SECONDS_IN_MINUTE) |
| 138 | + objRemainingSeconds = objRemainingSeconds - (intWorkingMinutes * NUM_SECONDS_IN_MINUTE) |
| 139 | + End If |
| 140 | + |
| 141 | + If intFunctionReturn = 0 Then |
| 142 | + intYears = intWorkingYears |
| 143 | + intDays = intWorkingDays |
| 144 | + intHours = intWorkingHours |
| 145 | + intMinutes = intWorkingMinutes |
| 146 | + objOutputSeconds = objRemainingSeconds |
| 147 | + End If |
| 148 | + |
| 149 | + ConvertNumberOfSecondsToBreakdownOfYearsDaysHoursMinutesSeconds = intFunctionReturn |
| 150 | +End Function |
0 commit comments