- #1
DiamondV
- 103
- 0
Homework Statement
Write three functions int get_hour(int timestamp), int get_min(int timestamp), int get_second(int timestamp) which will respectively return the hour of the day, the minute of the hour, and the second of the minute from a value given as parameter which is in milliseconds.
Example they give us is:
int timestamps = 1324561223; would output 07:56:01
The online system we use is basically where we write the actual function and the online compiler is coded to automatically run our code with predefined inputs that the compiler will insert as parameters into our code and print the result
Homework Equations
The Attempt at a Solution
Heres my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int get_hour(int timestamp){
int hour;
hour = (timestamp/(1000*60*60));
return hour;
}
int get_min(int timestamp){
int min;
min = ((timestamp%3600000)/60000);
return min;
}
int get_second(int timestamp){
int second;
second = (((timestamp%3600000)%60000)/1000);
return second;
}
I've been at this question for quite sometime. Theres something wrong with my hours. I keep getting this answer: 367:56:01 for int timestamps = 1324561223;
If you compare this with the example i said above, the minutes and seconds are correct, its something wrong with the hours and I have no idea why they are getting 07hrs. I googled it and even it said 367hrs. I thought maybe this has something to do with 24hrs in a day. 367/24 is 15 which is still not the right answer.
Why are my hours not right?
Last edited by a moderator: