Please ConvertToEnglish() – Subtracting zero

Here’s something I encountered while going through some legacy C code.

year =  (p_dt_tm[0]-'0')*1000 + (p_dt_tm[1]-'0')*100 +
        (p_dt_tm[2]-'0')*10   +  p_dt_tm[3]-'0';
mon  =  (p_dt_tm[4]-'0')*10   +  p_dt_tm[5]-'0';
day  =  (p_dt_tm[6]-'0')*10   +  p_dt_tm[7]-'0';
hour =  (p_dt_tm[8]-'0')*10   +  p_dt_tm[9]-'0';
min =  (p_dt_tm[10]-'0')*10   +  p_dt_tm[11]-'0';
sec =  (p_dt_tm[12]-'0')*10   +  p_dt_tm[13]-'0';

Can you figure out and explain what it’s supposed to do?

For bonus points, give an example of what the variable p_dt_tm can contain.

If you can view the code in fixed width font, then you’ll also notice the extent some programmers go to align their code nicely. See how all the plus signs line up vertically? And when the indices go to 2 digits, the programmer deliberately removed some spaces to realign.

Please ConvertToEnglish() – Obscure SQL

Sometimes you come across a simple SQL statement. Yet you’re confounded by the reasoning behind it. You think, “What is this trying to do?!”

Never had that experience before? Well, here’s your chance. Here’s a heavily anonymised version:

select max(is_active) from customers where customer_id = 'ABC'

And here’s the accompanying database table named “customers”.

customer_id customer_name is_active
ABC ABC Corporation Y
PQR PQR Company Y
XYZ XYZ Company N
XYZ XYZ MegaCorp N

Study both the SQL statement and the data in the database table. Then tell me what you think is the business logic behind the SQL statement.

What I mean is, suppose some manager needs some information. And the SQL statement is part of the solution to obtain that information. What does that manager need to know?

Need some help? Here’s an equivalent version:

select min(customer_status) from customers where customer_id = 'ABC'
customer_id customer_name customer_status
ABC ABC Corporation C
PQR PQR Company C
XYZ XYZ Company H
XYZ XYZ MegaCorp H

This entire exercise is to train you to decipher existing logic and data with very little help or documentation.

Bonus points: What do you think the values for the columns “is_active” and “customer_status” stand for?

Please ConvertToEnglish()

So, I thought it would be interesting to do something different. How about deciphering code? The challenge is to explain a piece of code by giving a minimalist description in English. We’ll practise understanding code with no comments and only the surrounding context as clues.

Here’s the code

int foo(int para)
	int temp = para << 3;
	return temp + temp + temp;

Explain what the function is doing in the simplest possible way. Don't just tell me what it's doing line by line. Tell me what it finally does.

Post your answer in a comment. Bonus points if you can give a reason why it was coded that way (yes, it has a rational explanation). I'll post my answer in a few days time.