20
2
Challenge
Given a time and a timezone as input, output the time in that timezone.
Time
The time will be given in 24 hour format like so:
hh:mm
Where hh is the two digit hour and mm is the two digit minute. Note that the hour and the minute will always be padded with zeroes like so:
06:09
All the times given are at UTC+00:00.
The hours in your output do not have to be padded wih zeroes but your time it must be in 24 hour format
Timezone
The timezone will be given in the following format:
UTC±hh:mm
Where ± is either going to be a + or a - and hh, is the two digit hour and mm is the two digit minute (again, these will be padded with zeroes).
To find the time in that timezone, you either add (if the symbol is +) or subtract (if the symbol is -) the time after the UTC± from the inputted time.
For example, if the input was 24:56
and UTC-02:50
, you would subtract 2 hours and 50 minutes from 24:56:
24:56
02:50 -
-----
22:06
The output would be 22:06
.
Examples
Chicago
Input: 08:50 and UTC-06:00
Output: 02:50
Kathmandu
Input: 09:42 and UTC+05:45
Output: 15:27
Samoa
Input: 06:42 and UTC+13:00
Output: 19:42
Hawaii
Input: 02:40 and UTC-10:00
Output: 16:40
Note that this has gone to the previous day.
Tokyo
Input: 17:25 and UTC+09:00
Output: 02:25
Note that this has gone to the following day.
Rules
You must not use any built in date functions or libraries.
Assume all input will be valid times and time offsets.
The timezone will be in the range UTC-24:00
to UTC+24:00
inclusive.
In the case of half past midnight, the correct representation should be 00:30
, not 24:30
.
Winning
The shortest code in bytes wins.
What about TimeSpan/Duration methods/classes? I assume those are also excluded – pinkfloydx33 – 2016-09-11T09:34:34.977
Also will input values always be valid times? Ie
26:02
and08:74
would not appear? Same for the UTC offsets? – pinkfloydx33 – 2016-09-11T09:37:33.720@pinkfloydx33 1) Yes, all of those excluded. 2) Assume all input is valid – Beta Decay – 2016-09-11T09:41:50.010
Do we have to pad the output with zeroes? (eg. Can the last test case output
2:25
) – Loovjo – 2016-09-11T09:42:22.327@Loovjo No, the output does no have to be padded – Beta Decay – 2016-09-11T09:43:25.187
24:54 or 00:54? Also is UTC+14:00 a valid input? – betseg – 2016-09-11T09:50:38.383
Shouldn't the second test case be
15:27
? – Loovjo – 2016-09-11T09:52:40.050@betseg
00:24
and I've added a range of acceptable time offsets – Beta Decay – 2016-09-11T10:01:10.980Please add a test case for MART and/or NST. – Neil – 2016-09-11T10:29:43.397
@Neil May I ask why? Surely the same principle is demonstrated by the Kathmandu time – Beta Decay – 2016-09-11T11:06:22.133
1If the output doesn't need to be padded would a time like
1:5
be valid instead of1:05
? I'd think only the hours shouldn't be padded. Also your example with24:56
should that not be00:56
since you've stated a range up to24:00
and express similar in your half past midnight scenario? – pinkfloydx33 – 2016-09-11T11:29:50.863Is the time guaranteed to be the first input, or can the inputs be in either order? – Tortoise – 2016-09-11T11:49:07.793
@BetaDecay In case an answer subtracts 3 hours but then and adds 30 minutes by mistake. – Neil – 2016-09-11T12:56:03.963
@Tortoise Post it as a non-competing entrant, if you like :) Just remember to put
non-competing
in the header – Beta Decay – 2016-09-11T17:02:44.417Could the input be a single string such as
"17:25 UTC+09:00"
? – Arnauld – 2016-09-11T17:10:55.360@Arnauld Your input may be taken in anyway you wish, as long is follows the format given in the question – Beta Decay – 2016-09-11T17:21:48.430