function returning hour in either 12 or 24 hour format

This is a discussion on function returning hour in either 12 or 24 hour format within the C Programming forums, part of the General Programming Boards category; I want to write a function that would return the hour of the time in either 12 or 24 hour ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    function returning hour in either 12 or 24 hour format

    I want to write a function that would return the hour of the time in either 12 or 24 hour format. The hour data is accessed through reading a register on a Real-time clock by a function:

    Code:
    unsigned char oneByteRead(unsigned char adr, short ramCal);
    where as adr is the address (let's call the address location to be passed HOURADR), and ramCal is whether the register is in Calendar data or custom RAM data (let's call the calendar data RTCCLOCK).

    Basically my function would determine the format that the hour is in and return the hour. I don't see any problem returning the 24 hour format. The only problem is returning the AM/PM hour data to complete the function call. Would I need to pass a separate pointer parameter in order to return 2 things at a time, one being the hour, the other being AM/PM, if the function determines that the byte is stored in 12 hour format?

    Thank you in advance for the comments!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    There are two items of information to return for 12 hour format: a value in the range 1-12 (assuming 30 minutes after midnight is represented as 12:30AM) and an indicator of whether the time is AM or PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you didn't want to have to change the function return value from unsigned char to something else, you certainly don't have to. You get 256 different combinations of values, and surely you can devise a scheme that indicated 24 hour format, or 12 hour format, with an AM or PM indicator.

    For instance, any value inclusive between 1 and 24 (or 0 and 23), is 24 hour format.
    For any value inclusive between 101 and 112 (or 100 and 111) is 12 hour, AM.
    For any value inclusive between 201 and 212 (or 200 and 211) is 12 hour PM.

    You can use any range you wanted.

    Todd

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm thinking of passing a custom struct type through the function as it would make more sense for me to look at the code later.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    I agree with that. It's a more logical approach that doesn't require so much complication.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM

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