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. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    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
    Last edited by -EquinoX-; 04-04-2009 at 12:15 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by matsp View Post
    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. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    never mind I got it now, I can use modulo and divide by 100

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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
    Last edited by -EquinoX-; 04-04-2009 at 12:43 PM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by vart View Post
    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. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by -EquinoX- View Post
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so how can I pass the pointer?

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    int main(void)
    {
       const char * s;
    
       change_str(&s);
       return 0;
    }
    void change_str(const char** pStr)
    {
       *pStr = "AM";
    }
    like this...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 01:38 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21