---
title: "Clock, C++, CodeWars"
slug: clock-cpp-codewars
type: evergreen
stime: 2025-01-15 @ 12:00AM
status: evergreen
certainty: certain
importance: 4
tags: [cpp, codewars, algorithms]
source: codewars-clock
---

## Problem Statement

Clock shows `h` hours, `m` minutes and `s` seconds after midnight.

Your task is to write a function which returns the time since midnight
in milliseconds.

**Example:**

```
h = 0
m = 1
s = 1

result = 61000
```

**Input constraints:**

```
0 <= h <= 23
0 <= m <= 59
0 <= s <= 59
```

## Solving Process

Upon viewing the problem, we know that we need to convert the hours,
minutes, and seconds into milliseconds. This means we need to have the
number for how many milliseconds constitute an hour, minute, and second.
This way, we can multiply the amount of [unit of time] by [number of
milliseconds per that unit]. The conversions for the units are as
follows: - Hour = 3,600,000 - Minute = 60,000 - Second = 1,000

Now that we know the units for conversion, and how to convert hours,
minutes, and seconds, the next step is to get the total. In order to
calculate the total, we must add the total amount of milliseconds
together after each has been converted, and we will save this inside an
int type variable `Total`.

```c
int Total = h * 3600000 + m * 60000 + s * 1000;
```

After this, we just want to return the total of "Total", which we do like
so.

```c
return Total;
```

## Solution

```cpp
/********************************************
*
* Title: Clock
* Author: krisyotam
* Date: 2025-07-20
*
*
*********************************************/

using namespace std;

int past(int h, int m, int s) {
  int Total = h * 3600000 + m * 60000 + s * 1000;
  return Total;
}
```

[Link To Solution on CodeWars](https://www.codewars.com/kata/reviews/5e51410a6971ed00018f1498/groups/687ccfc552816cd73e972e09)

## Related

- [Roman Numerals Decoder, C++, Codewars](roman-numerals-decoder-cpp-codewars.md)
- [Opposites Attract, C, CodeWars](opposites-attract-c-codewars.md)
- [Opposite Number, C, CodeWars](opposite-number-c-codewars.md)
- [Leap, C, Exercism](leap-c-exercism.md)
- [CodeWars](codewars.md)
- [Exercism](exercism.md)
