microsoft / PowerToys

Windows system utilities to maximize productivity
MIT License
106.91k stars 6.31k forks source link

[PowerToys Run] Time Calculator #17160

Open xmha97 opened 2 years ago

xmha97 commented 2 years ago

Description of the new feature / enhancement

image image

Scenario when this would be used?

Use this calculator to add or subtract two or more time values in the form of an expression. https://www.calculator.net/time-calculator.html?tcexpression=1d+2h+3m+4s+%2B+4h+5s+-+2030s+%2B+28h&ctype=3&x=57&y=21#expression

Supporting information

No response

xmha97 commented 2 years ago

Try this: (by @ebraminio)

data:text/html;charset=utf8,<textarea id=input style="width: 50%; height: 5em; white-space: pre" oninput="result.innerText = calculate(this.value)"></textarea><div id=result></div><script>window.onload = () => { input.value = '1d 2h 3m 4s + 4h 5s - 2030s + 28h'; input.dispatchEvent(new Event('input')) }; function calculate(input) { const units = Object.entries({ d: 86400, h: 3600, m: 60, s: 1 }); const seconds = eval(input.replace(/[\d\w\s]+/g, x => '(' + units.reduce((acc, unit) => acc.replace(new RegExp('(\\d+)' + unit[0]), '+$1*' + unit[1]), x) + ')')); const result = units.reduce(([result, reminder], unit) => [result + Math.floor(reminder / unit[1]) + unit[0] + ' ', reminder % unit[1]], ['', seconds])[0]; return result + '\n' + units.map(unit => (seconds / unit[1]) + ' ' + unit[0]).join('\n'); }</script>
htcfreek commented 2 years ago

Thos feels for me like an addition to calculator plugin

GhostVaibhav commented 6 days ago

Hi @htcfreek, just a confusion, can we use the TimeSpan struct here?

References -

  1. https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-8.0
htcfreek commented 6 days ago

Hi @htcfreek, just a confusion, can we use the TimeSpan struct here?

References -

  1. https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-8.0

Need to try. But sounds to me, like it make sense to use it.

GhostVaibhav commented 3 days ago

Hi @htcfreek, I looked into this issue and found that this feature can be introduced in Mages, not our repo. Since the whole query string is being given to Mages, it makes much more sense to process it then and there (or add any new functionality). Could you add the External Dependency label here? Also, could you add the same to #31740?