C# Clock in Mirror

Peter can see a clock in the mirror from the place he sits in the office. When he saw the clock shows 12:22

alt text

He knows that the time is 11:38

alt text

in the same manner:

05:25 –> 06:35

01:50 –> 10:10

11:58 –> 12:02

12:01 –> 11:59

Please complete the method which is provided with mirror time as string, and return the real time as string.

The return time value should always be between 1<= time < 13. This is the convenient way that human read the clock.

So there is no 00:20, instead it is 12:20.

There is no 13:20, instead it is 01:20.

my soultion to this task is :

public static string WhatIsTheTime(string timeInMirror)
{

string timeInMirror = “02:00”;

DateTime dt = Convert.ToDateTime(timeInMirror, System.Globalization.CultureInfo.InvariantCulture);

string s = dt.ToString(“hh:mm”);

string[] time;

time = s.Split(‘:’);

int h = Convert.ToInt32(time[0]);

int m = Convert.ToInt32(time[1]);

int hc = 0;

int mc = 0;

if (h == 12 & m == 00)
{
hc = h;

mc = m;
}
else
{
hc = (11 – h);

mc = 60 – m;
}
if (h == 12 & m != 0)
{
hc = 12 – (13 – h);

mc = 60 – m;
}
if (m == 00)
{
hc = 12 – h;

mc = m;
}

string shc = hc.ToString();

string smc = mc.ToString();

string finalcurrentdate = shc+”:”+smc;

DateTime ct = Convert.ToDateTime(finalcurrentdate);

string mirrortime = ct.ToString(“hh:mm”);

return mirrortime;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s