#include <stdio.h> /* puts */
#include <time.h> /* time_t, struct tm, time, localtime, strftime */
#include <bits/stdc++.h>
#include<unistd.h>
using namespace std;
int main ()
{
char CURRZONE[] = "TZ=Asia/Kolkata";
char DESTZONE[] = "TZ=UTC";
time_t stamp;
struct tm datetime;
putenv(CURRZONE);
char buffer[20];
uint16_t timevec[] = {15,28,12,30,06,2021};
datetime.tm_year = timevec[5]-1900;
datetime.tm_mon = timevec[4]-1;
datetime.tm_mday = timevec[3];
datetime.tm_hour = timevec[2];
datetime.tm_min = timevec[1];
datetime.tm_sec = timevec[0];
stamp = mktime(&datetime);
putenv(DESTZONE);
strftime (buffer,20,"%Y:%m:%d %T",localtime(&stamp));
std::string str(buffer);
cout<<str<<"\n";
return 0;
}
it's sometimes giving me the correct output which is 2021:06:30 06:58:15
othertimes it's giving me 1969:12:31 23:59:59
I don't need to compile the program again if I run it again its giving a different value. This behaviour is in CPP not with C. Not able to understand the reason behind the same.