Functions in C to calculate hours, minutes, seconds from milliseconds input

In summary: That will give you the correct hour in 24-hour clock notation.I don't know why you're getting 89. That could be a problem with the way you're doing integer division. I would suggest doing some print statements to see the intermediate values of your calculations and make sure they're what you expect them to be.In summary, the functions get_hour, get_min, and get_second return the hour, minute, and second from a timestamp value given in milliseconds. The hour should be within the range of 0 to 24 for a 24-hour clock. To ensure this, the modulus of the hour divided by 24 should be taken. Additionally, using print statements to track the intermediate values of calculations can help
  • #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:
Physics news on Phys.org
  • #2
The problem statement says: "...return the hour of the day, the minute of the hour, and the second of the minute".

So the hour should be in the range 0..24. That's for a 24 hour clock. You might be dealing with a 12 hour clock where a.m. and p.m. hours are expected. Try that.
 
  • #3
DiamondV said:

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?
All timestamp schemes have a "zero day" from which time is counted. For example, the timestamp on Unix systems starts at 00:00:00 UTC on January 1, 1970. It's not clear if your timestamp is based on the Unix system or something else.

https://en.wikipedia.org/wiki/Unix_time
 
  • #4
gneill said:
The problem statement says: "...return the hour of the day, the minute of the hour, and the second of the minute".

So the hour should be in the range 0..24. That's for a 24 hour clock. You might be dealing with a 12 hour clock where a.m. and p.m. hours are expected. Try that.

They also gave another example of
int timestamps = 321635432; with correct output being 17:20:35
So its definitely in 24hr clock.

For that input my code outputted: 89:20:35

I tried this code aswell, no luck

int get_hour(int timestamp){
int hour;
hour = timestamp/3600000;
return hour;
}

int get_min(int timestamp){
int min;
min = timestamp/60000;
return min;
}

int get_second(int timestamp){
int second;
second = timestamp/1000;
return second;
}
 
  • #5
Going back to your first example and rendering it by hand:

##h = \left \lfloor {\frac{1324561223}{1000⋅60*60}} \right \rfloor = 367##

Now, 367/24 = 15 + 7/24. So it looks like you need to take the modulus of h/24.
 

1. How do I convert milliseconds to hours, minutes, and seconds in C?

To convert milliseconds to hours, minutes, and seconds in C, you can use the following formula:

hours = milliseconds / (1000 * 60 * 60)

minutes = (milliseconds / (1000 * 60)) % 60

seconds = (milliseconds / 1000) % 60

This will give you the equivalent hours, minutes, and seconds from the given milliseconds input.

2. Can I use the same function to convert milliseconds to different time units?

Yes, the same formula can be used to convert milliseconds to different time units. You just need to adjust the formula based on the desired time unit. For example, to convert milliseconds to days, you can divide milliseconds by (1000 * 60 * 60 * 24).

3. How do I handle input validation when using the function to calculate time from milliseconds?

To handle input validation, you can check if the input is a positive integer. If not, you can display an error message and prompt the user to enter a valid input. You can also consider using a try-catch block to handle any potential errors that may occur during the conversion process.

4. Is there a built-in function in C to convert milliseconds to time units?

No, there is no built-in function in C specifically for converting milliseconds to time units. However, you can use the time.h library to work with time-related functions in C.

5. Can I use the function to convert milliseconds to time units in other programming languages?

Yes, the logic used in the function to convert milliseconds to time units can be applied in other programming languages as well. However, the syntax and libraries used may vary depending on the language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
761
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top