Question:
Every country follows its own timezone. Each timezone uses the Greenwich Mean Time(GMT). Also called the Universal Time Coordinated(UTC), as the reference and the local time is determined by adding or subtracting few hours( i.e. timezone offset) to the GMT.
You are expected to complete the function in the below editor to compute the local time based n the GMT and timezone offset.
Consider the current time in GMT as 0300 hours (3:00 AM) and the timezone offset be +0230 hours from GMT. Addming up both values would reveal 0530 hours (5:30 AM).Similarly , if the timezone offset be -0430 hours, then subtracting the time would result in 2230 hours.
Again, if the GMT is provided as 0370 hours and offset as +0200 hours, then the program should print the error:
Invalid input
Function Description
Complete the function computeLocalTime in the editor below. The function must state what must be returned or ptinted.
ComputeLocalTime has the following parameters(s):
gmTime: a string denoting the UTC(or GMT),
zoneOffset: a string denoting the numbers of hours ahead or behind UTC(or GMT)
Constraints
Both times are given hours format i.e. in nnnn format where n is an digit and 0<=n<=9
gmTime is always positive and lies between 0000 hours and 2359 hours.
zoneOffset can either be either be positive or negative.
-1200<=zoneOffset<=+1200
Sample Case 0:
Sample Input For Custom Testing
0300
+0230
Sample output
0530
Explanation
Upon adding both the GMT and the timezone offset, it can found that the resultant is 0530 hours.
Input:
0300
+0230
Output:
0530
Solution:
JAVA :
//https://codewindow.in
//join our telegram channel @codewindow
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
String str2 = br.readLine();
char sign = str2.charAt(0);
str2 = str2.substring(1);
if(sign == '+') {
int carry = 0;
int mins = Integer.parseInt(str1.substring(str1.length()-2))+Integer.parseInt(str2.substring(str1.length()-2));
if(mins >= 60) {
carry = 1;
mins -= 60;
}
int hrs = Integer.parseInt(str1.substring(0, 2))+Integer.parseInt(str2.substring(0, 2))+carry;
if(hrs >= 24)
hrs -= 24;
String hours = Integer.toString(hrs),minutes = Integer.toString(mins);
if(hrs <= 9)
hours= "0"+hrs;
if(mins <= 9)
minutes= "0"+mins;
System.out.println(hours+minutes);
} else {
int borrow = 0;
int mins = Integer.parseInt(str1.substring(str1.length()-2))-Integer.parseInt(str2.substring(str1.length()-2));
if(mins < 0) {
borrow = 1;
mins += 60;
}
int hrs = Integer.parseInt(str1.substring(0, 2))-Integer.parseInt(str2.substring(0, 2))-borrow;
if(hrs < 0)
hrs += 24;
String hours = Integer.toString(hrs),minutes = Integer.toString(mins);
if(hrs <= 9)
hours= "0"+hrs;
if(mins <= 9)
minutes= "0"+mins;
System.out.println(hours+minutes);
}
}
}
C++ :
//https://codewindow.in
//join our telegram channel @codewindow
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
int main() {
// your code goes here
string str1, str2;
cin >> str1;
cin >> str2;
char sign = str2.at(0);
str2 = str2.substr(1);
if(sign == '+') {
int carry = 0;
int mins = stoi(str1.substr(str1.length()-2))+stoi(str2.substr(str1.length()-2));
if(mins >= 60) {
carry = 1;
mins -= 60;
}
int hrs = stoi(str1.substr(0, 2))+stoi(str2.substr(0, 2))+carry;
if(hrs >= 24)
hrs -= 24;
string hours = to_string(hrs),minutes = to_string(mins);
if(hrs <= 9)
hours= "0"+to_string(hrs);
if(mins <= 9)
minutes= "0"+to_string(mins);
cout << hours+minutes;
} else {
int borrow = 0;
int mins = stoi(str1.substr(str1.length()-2))-stoi(str2.substr(str1.length()-2));
if(mins < 0) {
borrow = 1;
mins += 60;
}
int hrs = stoi(str1.substr(0, 2))-stoi(str2.substr(0, 2))-borrow;
if(hrs < 0)
hrs += 24;
string hours = to_string(hrs),minutes =to_string(mins);
if(hrs <= 9)
hours= "0"+to_string(hrs);
if(mins <= 9)
minutes= "0"+to_string(mins);
cout << hours+""+minutes;
}
return 0;
}
Also Checkout