Help needed with various time functions

Hello helpful friends,

Hi I am newbie to c++ but not new to programming. Trying to write a program that will show the Time from various Time zones like Toronto, Mumbai, etc.

The code is below. The problem I am facing is that the time is not displayed correctly and I suspect it has something to do with the time function.
The time for Mumbai should be +5.30 hrs from the UTC/GMT time but it seems to be seeded with the same time as Toronto.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>

using namespace std;
int main()
{
    time_t rawTimeMumbai;
    time_t rawTimeToronto;
    time_t rawTimeUTC;
    
    rawTimeMumbai = time(NULL);
    rawTimeToronto = time(NULL);
    rawTimeUTC = time(NULL);
    
    struct tm* torontoTime;
    struct tm* mumbaiTime;
    struct tm* utcTime;
    
    utcTime = gmtime(&rawTimeUTC);
    torontoTime = gmtime(&rawTimeToronto);
    mumbaiTime = gmtime(&rawTimeMumbai);
    
    printf("Current Time in UTC : %2d:%02d \n", utcTime->tm_hour, utcTime->tm_min );
    
    puts(" ");
    puts("Current time");
    puts("-------------");
    puts(" ");
    
    torontoTime->tm_hour -=  5;
    mktime(torontoTime);
     printf("Toronto : %2d:%02d \n", torontoTime->tm_hour, torontoTime->tm_min);
    
    mumbaiTime->tm_hour += 5;
    mumbaiTime->tm_min += 30;
    mktime(mumbaiTime);
  
   
    printf("Mumbai : %2d:%02d\n", mumbaiTime->tm_hour, mumbaiTime->tm_min);
    
    printf("End of Program");
    
}




Output of the code is below

1
2
3
4
5
6
7
8
9

Current Time in UTC :  6:34 
 
Current time
 
Toronto :  1:34 
Mumbai :  7:04
End of Program



The time for Mumbai should be 12.07PM and not 7.04. Seems like Mumbai's time is being set with the same as Toronto and then getting incremented by 5.30 hrs.

I appreciate your assistance!

Thanks

BlackDisc

You need to copy the std::tm struct. After
1
2
    torontoTime = gmtime(&rawTimeToronto);
    mumbaiTime = gmtime(&rawTimeMumbai);

both torontoTime and mumbaiTime point to the same (static) object.

Return value
Pointer to a static internal std::tm object on success, or NULL otherwise. The structure may shared between std::gmtime, std::localtime, and std::ctime and may be overwritten on each invocation. - http://en.cppreference.com/w/cpp/chrono/c/gmtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <ctime>
#include <cstdio>

int main()
{
    std::time_t now = std::time(0) ;
    const std::tm utc_time = *std::gmtime( &now ) ; // copy to local struct
    std::printf("time UTC: %2d:%02d \n", utc_time.tm_hour, utc_time.tm_min ) ;

    std::tm toronto_time = utc_time ; // copy utc_time
    toronto_time.tm_hour -= 5 ; // adjust for time zone
    std::mktime( &toronto_time ) ; // renormalise the fields
    std::printf("time toronto: %2d:%02d \n", toronto_time.tm_hour, toronto_time.tm_min ) ;

    std::tm mumbai_time = utc_time ; // copy utc_time
    mumbai_time.tm_hour += 5 ; // adjust for time zone
    mumbai_time.tm_min += 30 ;
    std::mktime( &mumbai_time ) ; // renormalise the fields
    std::printf("time mumbai: %2d:%02d \n", mumbai_time.tm_hour, mumbai_time.tm_min ) ;
}
Topic archived. No new replies allowed.