Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implementing get_local_time get_local_time is the most important function in the

ID: 3703472 • Letter: I

Question

Implementing get_local_time

get_local_time is the most important function in the assignment.

get_local_time is given as 4 parameters: a town and a UTC month, day and time. All use the representations described above.

If any of get_local_time parameters are invalid it should return the constant INVALID_INPUT which is #defined in time2code.c Otherwise get_local_timeshould return the local time in the town, using the representation described above.

You are given this code for get_local_time.

Add your code to this function. You will have to change the return statement. The return is included only so the function compiles. It only returns the correct value in a few cases.

You should define your own functions and call them from get_local_time.

Implementing run_unit_tests

You are given this code for run_unit_tests.

run_unit_tests includes two unit tests. Add your own unit tests for inputs to run_unit_tests. Implement them in exactly the same way as the example unit tests using assert statements.

Do not take the inputs autotest uses and add them to run_unit_tests - invent your own tests.

Explanation / Answer

#include <stdio.h>
#include <assert.h>

// You must use these #defines - DO NOT CHANGE THEM

#define TOWN_ADELAIDE       0
#define TOWN_BRISBANE       1
#define TOWN_BROKEN_HILL    2
#define TOWN_CANBERRA       3
#define TOWN_DARWIN         4
#define TOWN_EUCLA          5
#define TOWN_HOBART         6
#define TOWN_LORD_HOWE_IS   7
#define TOWN_MELBOURNE      8
#define TOWN_PERTH          9
#define TOWN_SYDNEY         10

// New Zealand

#define TOWN_AUCKLAND       11
#define TOWN_CHRISTCHURCH   12
#define TOWN_WELLINGTON     13

// Australia

#define TIMEZONE_AWST_OFFSET 800 // Australian Western Standard Time

#define TIMEZONE_ACWST_OFFSET 845 // Australian Central Western Standard Time

#define TIMEZONE_ACST_OFFSET 930 // Australian Central Standard Time
#define TIMEZONE_ACDT_OFFSET 1030 // Australian Central Daylight Time

#define TIMEZONE_AEST_OFFSET 1000 // Australian Eastern Standard Time
#define TIMEZONE_AEDT_OFFSET 1100 // Australian Eastern Daylight Time

#define TIMEZONE_LHST_OFFSET 1030 // Lord Howe Standard Time
#define TIMEZONE_LHDT_OFFSET 1100 // Lord Howe Daylight Time

// New Zealand
#define TIMEZONE_NZST_OFFSET 1200 // NZ Standard Time
#define TIMEZONE_NZDT_OFFSET 1300 // NZ Daylight Time

// returned by get_local_time
#define INVALID_INPUT (-1)


// ADD YOUR #defines (if any) here

int get_local_time(int town, int utc_month, int utc_day, int utc_time);
void run_unit_tests(void);


// ADD PROTOTYPES FOR YOUR FUNCTIONS HERE

// DO NOT CHANGE THIS FUNCTION

// This main function won't be marked as part of the assignment
// It tests the functions you have to write.
// Do not change this main function. If it does not work with
// your functions you have misunderstood the assignment.

int main(void) {
    int call_get_local_time = 0;

    printf("Enter 0 to call run_unit_tests() ");
    printf("Enter 1 to call get_local_time() ");
    printf("Call which function: ");
    scanf("%d", &call_get_local_time);

    if (!call_get_local_time) {
        printf("calling run_unit_tests() ");
        run_unit_tests();
        printf("unit tests done ");
        return 0;
    }

    int town = 0;
    int month = 0;
    int day = 0;
    int time = 0;

    printf("Enter %d for Adelaide ", TOWN_ADELAIDE);
    printf("Enter %d for Brisbane ", TOWN_BRISBANE);
    printf("Enter %d for Broken_hill ", TOWN_BROKEN_HILL);
    printf("Enter %d for Canberra ", TOWN_CANBERRA);
    printf("Enter %d for Darwin ", TOWN_DARWIN);
    printf("Enter %d for Eucla ", TOWN_EUCLA);
    printf("Enter %d for Hobart ", TOWN_HOBART);
    printf("Enter %d for Lord Howe Island ", TOWN_LORD_HOWE_IS);
    printf("Enter %d for Melbourne ", TOWN_MELBOURNE);
    printf("Enter %d for Perth ", TOWN_PERTH);
    printf("Enter %d for Sydney ", TOWN_SYDNEY);
    printf("Enter %d for Auckland ", TOWN_AUCKLAND);
    printf("Enter %d for Christchurch ", TOWN_CHRISTCHURCH);
    printf("Enter %d for Wellington ", TOWN_WELLINGTON);

    printf("Which town: ");
    scanf("%d", &town);

    printf("Enter UTC month as integer 1..12: ");
    scanf("%d", &month);

    printf("Enter UTC day as integer 1..31: ");
    scanf("%d", &day);

    printf("Enter UTC time as hour * 100 + minutes ");
    printf("Enter UTC time as integer [0..2359]: ");
    scanf("%d", &time);

    int local_time = get_local_time(town, month, day, time);

    if (local_time == INVALID_INPUT) {
        printf("invalid input - local time could not be calculated ");
    } else {
        printf("local_time is %d ", local_time);
    }
    return 0;
}

// Given an Australian town and a 2018 UTC time
// return the local time in the Australian town
//
// time is returned as hours*100 + minutes
// INVALID_INPUT is return for invalid inputs
//
// utc_month should be 1..12
// utc_day should be 1..31
// utc_time should be 0..2359
// utc_time is hours*100 + minutes
//
// note UTC year is assumed to be 2018
// note UTC seconds is assumed to be zero
//

int get_local_time(int town, int utc_month, int utc_day, int utc_time) {
    // CHANGE THIS FUNCTION
    // YOU ARE NOT PERMITTED TO USE ARRAYS, LOOPS, PRINTF OR SCANF
    // SEE THE ASSIGNMENT SPECIFICATION FOR MORE RESTRICTIONS
    int TIMEZONE_OFFSET = 0;
    if(utc_month < 1 || utc_month > 12){
    return INVALID_INPUT;
}
if(utc_day < 1 || utc_day > 31){
  return INVALID_INPUT;
}
if(utc_time < 0 || utc_time > 2359){
  return INVALID_INPUT;
}
if( town < 0 || town > 13){
  return INVALID_INPUT;
}
if(utc_month >= 10 || utc_month <= 4){
  if(town == 1){
   TIMEZONE_OFFSET = 1000;
  }else if(town == 10 || town == 8 || town == 6 || town == 3){
   TIMEZONE_OFFSET = 1100;
  }else if(town == 0 || town == 2){
   TIMEZONE_OFFSET = 1030;
  }else if(town == 4){
   TIMEZONE_OFFSET = 930;
  }else if(town == 9){
   TIMEZONE_OFFSET = 800;
  }else if(town == 13){
   TIMEZONE_OFFSET = 1300;
  }else if(town == 11 || town == 12){
   TIMEZONE_OFFSET = 1200;
  }else if(town == 5){
   TIMEZONE_OFFSET = 845;
  }else if(town == 7){
   TIMEZONE_OFFSET = 1030;
  }
}else{
  if(town == 1 || town == 10 || town == 8 || town == 6 || town == 3){
   TIMEZONE_OFFSET = 1000;
  }else if(town == 0 || town == 4 || town == 2){
   TIMEZONE_OFFSET = 930;
  }else if(town == 9){
   TIMEZONE_OFFSET = 800;
  }else if(town == 5){
   TIMEZONE_OFFSET = 845;
  }else if(town == 7){
   TIMEZONE_OFFSET = 1030;
  }else if(town == 13){
   TIMEZONE_OFFSET = 1200;
  }else if(town == 11 || town == 12){
   TIMEZONE_OFFSET = 1200;
  }
}
int time = utc_time + TIMEZONE_OFFSET;
if(time > 2359){
  time = time - 2400;
}
    return time;
}


// ADD YOUR FUNCTIONS HERE


// ADD A COMMENT HERE EXPLAINING YOUR OVERALL TESTING STRATEGY

// run unit tests for get_local_time

void run_unit_tests(void) {
    // 2 example unit tests

    // UTC midnight on 20th March -> time in Sydney: 11am AEDT
    assert(get_local_time(TOWN_SYDNEY, 3, 20, 0) == 1100);

    // 42 is not a valid month
    assert(get_local_time(TOWN_SYDNEY, 42, 0, 0) == INVALID_INPUT);

    // ADD YOUR ASSERT STATEMENTS HERE

    // you should add at least 40 more assert statements to this function
    // with a comment for each test explaining it

    // there should be comment before this function
    // explaining your overall testing strategy
}