# military time to std time

This is a discussion on military time to std time within the C Programming forums, part of the General Programming Boards category; Ok, I know how to do this if I know how to convert say 1423 to a string, is there ...

1. ## military time to std time

Ok, I know how to do this if I know how to convert say 1423 to a string, is there an easy way to do this?
or is there a math trick to this?

to get the AM/PM I can substract the number by 1300 and see if it's positive or negative, however I don't know how to get the hours and minutes

2. Code:
```int x=1423;
char string[5];
sprintf(string,"%d",x);```
I guess the rest might involve numbers like 60 and 24.

ps. I think the more common name for this clock is the "24 hour clock", as it is standard many places in the world (where the 12 hour clock is not used), even outside the military...

3. Why not check if it's bigger than 1300 instead? Then subtract 1200 and make it AM/PM. You may have to deal with midnight too.

If you have the number as a string, you could convert it to an integer first, the do modulo 60 to get minutes.

--
Mats

4. Originally Posted by matsp
Why not check if it's bigger than 1300 instead? Then subtract 1200 and make it AM/PM. You may have to deal with midnight too.

If you have the number as a string, you could convert it to an integer first, the do modulo 60 to get minutes.

--
Mats
well say that I have 1425, I substract that by 1200 I get 225, how do I extract the hours and minutes in int?

5. Originally Posted by -EquinoX-
well say that I have 1425, I substract that by 1200 I get 225, how do I extract the hours and minutes in int?
By dividing by 100 (gives you 2) and using modulo 100 (gives you 25).

--
Mats

6. Divide (modulo actually) the number repeatedly by its base (10 in this case) until the dividend is zero.
Now start collecting the remainders in a char array by tracing the modulo path backwards.

Edit: never mind, I jumped late into this one.

7. never mind I got it now, I can use modulo and divide by 100

8. Code:
```#include <stdio.h>

void time_conv(int cur_time, int *hours, int *mins, char* s){
int result = 0;
if (cur_time - 1300 > 0){
result = cur_time - 1200;
*hours =  result/100;
*mins =  result % 100;
s = "PM";
}else {
*hours = cur_time/100;
*mins = cur_time % 100;
s = "AM";
}

}

int main(void) {
int time = 934;
int hours;
int mins;
char s[2];
time_conv(time, &hours, &mins, s);
printf("%d:%d %s\n", hours, mins, s);
return 0;
}```
my problem with the code above is that when ever I tried to print s, which is the AM/PM it gives me a seg fault.. can someone help me

9. you need to declare s as
const char* s;

and pass pointer to it to modify it as you pass hours for example

I see you can get 12:40AM

what about 12:40PM is this time not acceptable at all?

10. The arrays s[] needs to be bigger as all char strings in C are null terminated so you need to make room for one more character ie the terminating null.

11. Originally Posted by vart
you need to declare s as
const char* s;

and pass pointer to it to modify it as you pass hours for example

I see you can get 12:40AM

what about 12:40PM is this time not acceptable at all?
I am already passing in pointers there right? I changed my code above to const char* s and it still doesn't work

12. Originally Posted by -EquinoX-
I am already passing in pointers there right? I changed my code above to const char* s and it still doesn't work
variable itself is a pointer
you not passing pointer to this variable (it will be pointer to pointer), just value of the pointer - so currently you cannot update the value of the pointer

13. so how can I pass the pointer?

14. Code:
```int main(void)
{
const char * s;

change_str(&s);
return 0;
}
void change_str(const char** pStr)
{
*pStr = "AM";
}```
like this...