James Coles
James Coles

Reputation: 51

Can someone explain to me why time is working like this?

I've spent a good hour on this, but I can't create a formula that works for my c program. (I have a new programmer).

I have to convert UTC time to its respective time in a particular city. My code works perfectly except here. Here it gives me the wrong answer. I can't wrap my head around it (I created a formula but it makes no sense to me).

In my program, time is entered as 24 hour time. 9AM = 900, 12PM = 1200, 12am = 0 etc.

If we are asked to convert 2359 to Eucla time (UTC +845) my program outputs 804. The correct answer is 844. I figured out how to calculate 844, but I make no sense of it.

2359 + 845 = 3204 (adding the timezone offset 845 to the UTC time)
3204 - 60 = 3144 (minus 60 for some reason [I followed my time overflow formula]
3144 - 2400 = 2400 (minus 2400 because time cannot be more than 2359)

How my program works

First plus UTC and offset time

calculatedTime = UTC + offset;

Then under that

if (calculatedTime < 2359) {
calculatedTime = calculatedTime - 2400;
}

I also have another function which checks for overflow time underneath

if (((calculatedTime > 59) && (calculatedTime < 99)) || ((calculatedTime > 159) && (calculatedTime < 199))) {

// All the way to 2359

calculatedTime = calculatedTime - 60 + 100;

}

Upvotes: 0

Views: 101

Answers (2)

chux
chux

Reputation: 154242

OP's code has various off-by-one errors.

// if (((calculatedTime > 59) && (calculatedTime < 99)) || 
//    ((calculatedTime > 159) && (calculatedTime < 199))) {
if (((calculatedTime > 59) && (calculatedTime < 100)) || 
    ((calculatedTime > 159) && (calculatedTime < 200))) {

// if (hours > 24) {
//     hours -= 24;
// }
if (hours >= 24) {
    hours -= 24;
}

Also code has more clarity using values like 60, 100

if (((calculatedTime >= 60) && (calculatedTime < 100)) || 
    ((calculatedTime >= 60*2) && (calculatedTime < 100*2))) {

Yet OP's approach fails with negative numbers.

To cope with positive and negative time values, split the "hhmm" time into a hours and minutes. Look for conditions of "minute" overflow. I recommend 2 helper functions to split and combine results.

#include <stdio.h>
#include <stdlib.h>

void hhmm_split(int hhmm, int *hour, int *min) {
  *hour = hhmm / 100;
  *min = hhmm % 100;
}

/* `min` may be outside the primary range of (-60 ... 60) */
int hhmm_combine(int hour, int min) {
  hour += min / 60;
  min %= 60;
  if (hour < 0 && min > 0) {
    min -= 60;
    hour++;
  } else if (hour > 0 && min < 0) {
    min += 60;
    hour--;
  }
  hour %= 24;
  return hour * 100 + min;
}

Test code

void hhmm_add(int t1, int t2) {
  int t1_hh, t1_mm, t2_hh, t2_mm;
  hhmm_split(t1, &t1_hh, &t1_mm);
  hhmm_split(t2, &t2_hh, &t2_mm);
  int sum = hhmm_combine(t1_hh + t2_hh, t1_mm + t2_mm);
  printf("t1:% 05d + t2:% 05d = sum:% 05d\n", t1, t2, sum);
}

int main(void) {
  hhmm_add(2359, 845);
  hhmm_add(2359, -845);
  hhmm_add(-2359, 845);
  hhmm_add(-2359, -845);
}

Output:

t1: 2359 + t2: 0845 = sum: 0844
t1: 2359 + t2:-0845 = sum: 1514
t1:-2359 + t2: 0845 = sum:-1514
t1:-2359 + t2:-0845 = sum:-0844

Upvotes: 0

user3386109
user3386109

Reputation: 34839

You need to separate the time into hours and minutes. Then add the time zone offsets to the hours and minutes separately. Handle roll-over. Finally, recombine the hours and minutes into the final answer.

Like this:

int main(void)
{
    // inputs
    int time = 2359;
    int zone =  845;

    // separate hours and minutes
    int timeHours = time / 100;
    int timeMinutes = time % 100;
    int zoneHours = zone / 100;
    int zoneMinutes = zone % 100;

    // add the hours and minutes
    int hours = timeHours + zoneHours;
    int minutes = timeMinutes + zoneMinutes;

    // handle the rollover conditions
    if (minutes > 60) {
        minutes -= 60;
        hours++;
    }
    if (hours > 24) {
        hours -= 24;
    }

    // recombine the hours and minutes
    int adjustedTime = hours * 100 + minutes;
    printf("%d\n", adjustedTime);
}

Note that this code only works for timezones with positive offsets. You'll need to figure out how to make it work for negative time zones.

Upvotes: 1

Related Questions