Thread: Little Help

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Angry Little Help

    I have been struggling to figure out the problem
    causing the YEAR format not correct in the small C program below. It keeps output like 07-04-102 10.05p. Since 102 is not right,it suppose to be 02 in year (MM-DD-YY).
    Anyone want know what's wrong? Thanks for your help in advance.

    #include <stdio.h>
    #include <time.h>
    #include <conio.h>
    /*This file was compiled under Borland C/C++ 5.1 */

    int main()
    {
    time_t current = time(NULL);
    struct tm *ptr;
    char DATE_TIME[19];
    int HOUR;
    char AM_or_PM;

    /* print date and time using custom formatting */
    ptr = localtime(&current);
    HOUR = ptr->tm_hour;
    if (HOUR <= 11)
    AM_or_PM = 'A';
    else
    {
    HOUR -= 12;
    AM_or_PM = 'P';
    }
    if (HOUR == 0)
    HOUR = 12;

    printf("%.2d-%.2d-%.2d %2d:%.2d%c\n", ptr->tm_mon+1,
    ptr->tm_mday, ptr->tm_year, HOUR, ptr->tm_min,
    AM_or_PM);

    getch();
    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The tm_year part of the struct gives you the number of years since 1900. Therefore 2002-1900=102. To fix it, simply subtract 100.
    For your info, here's what I have on the rest of the tm struct:
    Code:
    int tm_sec 
    seconds after the minute, in the range [0,61], allowing for leap seconds 
    int tm_min 
    minutes after the hour, in the range [0,59] 
    int tm_hour 
    hours after midnight, in the range [0,23] 
    int tm_mday 
    day of the month, in the range [1,31] 
    int tm_mon 
    months since January, in the range [0,11] 
    int tm_year 
    years since 1900 
    int tm_wday 
    days since Sunday, in the range [0,6] 
    int tm_yday 
    days since January 1, in the range [0,365], allowing for leap years 
    int tm_isdst 
    Daylight Savings Time flag
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    Ah hah, I did not think of that.
    Thanx for pointing it out, Hammer.
    You are the MAN!
    Munchos gracias!
    DV007

Popular pages Recent additions subscribe to a feed