Converts a fractional part of a serial Julian date and time into hours, minutes and seconds.

View versions (1)

Interface

#include <codecogs/units/time/timehms.h>

using namespace Units::Time;

Overview

This function converts the fractional part of a serial Julian date and time into Hours, Minutes and Seconds, thereby performing the inverse of time. For the Gregorian calendar this conversion is achieved using:

a=Time - int(Time)
(1)
b=24*3600*a
(2)
Hours=int \left ( \frac{b}{3600} \right )
(3)
Minutes=int \left ( \frac{b\ mod\ 3600}{60} \right )
(4)
Seconds=int \left ( b\ mod\ 60 \right )
(5)

See:

The opposite of this function is time

Example 1

#include <stdio.h>
#include <codecogs/units/time/timehms.h>

int main()
{
  for(double i=-0.5;i<=2;i+=0.1)
  {
    int h,m,s;
    Units::Time::timeHMS(i, h,m,s);
    printf("\n %02d:%02d:%02d", h, m, s);
  }
  return 0;
}

Output:

12:00:00
14:24:00
16:48:00
19:12:00
21:36:00
00:00:00
02:24:00
04:48:00
07:12:00
09:36:00
12:00:00
14:24:00
16:48:00
19:12:00
21:36:00
00:00:00
02:24:00
04:48:00
07:12:00
09:36:00
12:00:00
14:24:00
16:48:00
19:12:00
21:36:00

Parameters

nTime
contains a time as a fraction of a day. The whole part of this number is ignored, so it could represent either days in a Julian or Gregorian calendar.
Hours
is a location (passed by reference) into which is placed the year.
Minutes
is a location (passed by reference) into which is placed the month.
Seconds
is a location (passed by reference) into which is placed the day.
GPL Licence — free for non commercial use. See Licence details.

FUNCTION

timeHMS

This function uses a number of simple rules to extract the most likely time from a text string, returning the individual elements (Hours, Minutes and Seconds) of the date. Unlike the Excel version this function is extremely forgiving, it will therefore interpret most date forms and will not return an error.

Example 1

This example is intended to illustrates the type of times that can be processed, rather than to being functional (see time for a more functional examples).

#include <stdio.h>
#include <codecogs/units/time/time.h>

void main()
{
  int h,m,s;
  timeHMS("10pm", h,m,s);               // 22:00:00
  timeHMS("10:12pm", h,m,s);            // 22:12:00
  timeHMS("4am40/34", h,m,s);           // 04:40:34
  timeHMS("2,3,4", h,m,s);              // 02:03:04
  timeHMS("12@31@4", h,m,s);            // 12:31:04
  timeHMS("16:5:04pm", h,m,s);          // 28:05:04
}

Parameters

Time
is a text string that contains the time, with the following attributes:
Blank
  • Any separator that isn't numeric, alphabetic or '-' can be used (i.e. non alphanumeric). Therefore you can have negative hours, minute or seconds through the use of the negative sign.
  • Only the first 3 terms will be processed. If less than 3 terms are specified, then zero is used for the missing values.
  • The time elements are always expected to be in the order of: hours, minutes, seconds
  • By default a 24hr clock is assumed, however if either "am" or "pm" in specified in any part of the string, then the first number is assumed to be part of a 12 hour clock from either the morning or afternoon, respectively. N.B. 13pm would return an hour=25 (i.e. 1am in the next day).

Parameters

Hours
is a location (passed by reference) into which is placed the hours of Time.
Minutes
is a location (passed by reference) into which is placed the minutes of Time.
Seconds
is a location (passed by reference) into which is placed the seconds of Time.

This function can extract an illegal time, e.g. timeHMS("14 70 65 pm",h,m,s) will return the hour=26, minutes=70 and seconds=65. To be confident of having a legitimate date, use time instead, e.g. time("31 February 2004") will return a day fraction of ~1.132697 which equals 03:11:05 of the next day.

Author

Will Bateman (Nov 2004)