79326600

Date: 2025-01-03 13:32:01
Score: 0.5
Natty:
Report link

When you call localtime for IST, it overwrites the result of localtime for GMT.

Use gmtime for GMT to avoid timezone issues. Copy the results of gmtime and localtime into separate struct tm variables to prevent overwriting.

#include <stdio.h>
#include <time.h>

int main() {
    time_t gmt, ist;
    struct tm gmt_tm, ist_tm; // Separate instances
    char sgmt[100], sist[100];

    time(&gmt);
    ist = gmt + 19800; // IST is 5h 30m ahead

    gmt_tm = *gmtime(&gmt); // Copy GMT result
    ist_tm = *localtime(&ist); // Copy IST result

    strftime(sgmt, sizeof(sgmt), "%A, %d %B %Y, %X", &gmt_tm);
    strftime(sist, sizeof(sist), "%A, %d %B %Y, %X", &ist_tm);

    printf("Current GMT: %s\n", sgmt);
    printf("Current IST: %s\n", sist);

    return 0;
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: Hugo