time(NULL) returning wrong month??? - Unix

This is a discussion on time(NULL) returning wrong month??? - Unix ; hello everyone, i am really confused with some of the c++ time() fn results i get: let me put you into perspective: im writing a logging utility the log files created will contain 14byte samples of certain values of interest. ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: time(NULL) returning wrong month???

  1. time(NULL) returning wrong month???

    hello everyone,
    i am really confused with some of the c++ time() fn results i get:
    let me put you into perspective:

    im writing a logging utility the log files created will contain 14byte
    samples of certain values of interest. the last 4bytes of each sample
    are its timestamp (timestamp of samples), that is the # of seconds
    since the epoch.
    so thats one 'date' (timestamp of samples) to consider.

    the 2nd date is the log file attribute timestamp (attribute
    timestamp). of when it is created - the value anyway that you see when
    you do an ls -l of the file. this is of the least importane for me but
    i just am refering to it in case you understand smth i do not.

    last is the date string i use to create the filename with (filename
    timestamp). explaining: since the data will be copied on a flash drive
    from the ARM unit on which the log utility will be run and then copied
    onto a computer, i decided i didn't want to have to deal with changing
    file attributes so i hard coded the file creation date onto the
    filename (since it is important).

    now that we have the 3 dates a little about the system:
    its an ARM based embedded system and is running debian linux
    i have set its date (with date -s) and since the unit is located in
    greece the timezone is EET (east european time).
    having set the date as such, when i call (in total) 3 functions to get
    the time or the date

    the timestamp of the samples is returned by a driver that is written
    in c (unlike everything else that is c++) and that uses the kernel
    call do_gettimeofday().

    FileTime GetTime () //FileTime is a typedef of unsigned long
    //this function returns the time in seconds from the Epoch
    {
    time_t current_time;
    current_time=time(NULL);
    Time=((FileTime)current_time);
    return (Time);
    };

    and

    DateTime GetFDate()
    {

    time_t current_time;
    current_time=time(NULL);
    DatTim.day=localtime(&current_time)->tm_mday;
    DatTim.month=localtime(&current_time)->tm_mon;
    DatTim.year=localtime(&current_time)->tm_year+1900;
    DatTim.LTime=GetFTime();

    return (DatTim);
    };

    where

    FTime GetFTime()
    {
    time_t current_time;
    current_time=time(NULL);

    FTim.hour=localtime(&current_time)->tm_hour;
    FTim.minute=localtime(&current_time)->tm_min;
    FTim.second=localtime(&current_time)->tm_sec;

    return (FTim);
    };

    the DatTim.month returns to me (currently) february, instead of march!
    (it generally returns 1 month before the current month). I use
    GetFDate to write the filename date string. however the bash command
    'date' returns the right date... so the attribute timestamp is
    correct, but the filename timestamp is wrong... any ideas? could it be
    some bug?

    thank you for your help
    nass


  2. Re: time(NULL) returning wrong month???

    On Mar 23, 11:54 am, "nass" wrote:
    > hello everyone,
    > i am really confused with some of the c++ time() fn results i get:
    > let me put you into perspective:
    >
    > im writing a logging utility the log files created will contain 14byte
    > samples of certain values of interest. the last 4bytes of each sample
    > are its timestamp (timestamp of samples), that is the # of seconds
    > since the epoch.
    > so thats one 'date' (timestamp of samples) to consider.
    >
    > the 2nd date is the log file attribute timestamp (attribute
    > timestamp). of when it is created - the value anyway that you see when
    > you do an ls -l of the file. this is of the least importane for me but
    > i just am refering to it in case you understand smth i do not.
    >
    > last is the date string i use to create the filename with (filename
    > timestamp). explaining: since the data will be copied on a flash drive
    > from the ARM unit on which the log utility will be run and then copied
    > onto a computer, i decided i didn't want to have to deal with changing
    > file attributes so i hard coded the file creation date onto the
    > filename (since it is important).
    >
    > now that we have the 3 dates a little about the system:
    > its an ARM based embedded system and is running debian linux
    > i have set its date (with date -s) and since the unit is located in
    > greece the timezone is EET (east european time).
    > having set the date as such, when i call (in total) 3 functions to get
    > the time or the date
    >
    > the timestamp of the samples is returned by a driver that is written
    > in c (unlike everything else that is c++) and that uses the kernel
    > call do_gettimeofday().
    >
    > FileTime GetTime () //FileTime is a typedef of unsigned long
    > //this function returns the time in seconds from the Epoch
    > {
    > time_t current_time;
    > current_time=time(NULL);
    > Time=((FileTime)current_time);
    > return (Time);
    >
    > };
    >
    > and
    >
    > DateTime GetFDate()
    > {
    >
    > time_t current_time;
    > current_time=time(NULL);
    > DatTim.day=localtime(&current_time)->tm_mday;
    > DatTim.month=localtime(&current_time)->tm_mon;
    > DatTim.year=localtime(&current_time)->tm_year+1900;
    > DatTim.LTime=GetFTime();
    >
    > return (DatTim);
    >
    > };
    >
    > where
    >
    > FTime GetFTime()
    > {
    > time_t current_time;
    > current_time=time(NULL);
    >
    > FTim.hour=localtime(&current_time)->tm_hour;
    > FTim.minute=localtime(&current_time)->tm_min;
    > FTim.second=localtime(&current_time)->tm_sec;
    >
    > return (FTim);
    >
    > };
    >
    > the DatTim.month returns to me (currently) february, instead of march!
    > (it generally returns 1 month before the current month). I use
    > GetFDate to write the filename date string. however the bash command
    > 'date' returns the right date... so the attribute timestamp is
    > correct, but the filename timestamp is wrong... any ideas? could it be
    > some bug?
    >
    > thank you for your help
    > nass


    may i also ask,
    does do_gettimeofday() which is lower level function than time() take
    into account daylight saving changes?
    thank you for your info
    nass


  3. Re: time(NULL) returning wrong month???

    "nass" writes:

    [...]

    > the DatTim.month returns to me (currently) february, instead of march!
    > (it generally returns 1 month before the current month).


    The month value of a struct tm is in (0, 11), not (1, 12).
    Additionally, the year member is offsetted by -1900 (ie
    2001 <=> 101).

    BTW, consider reading the fine reference manual next time.

  4. Re: time(NULL) returning wrong month???

    "nass" wrote in
    news:1174643683.762636.274030@n76g2000hsh.googlegr oups.com:

    >
    > FileTime GetTime () //FileTime is a typedef of unsigned long
    > //this function returns the time in seconds from the Epoch
    > {
    > time_t current_time;
    > current_time=time(NULL);
    > Time=((FileTime)current_time);
    > return (Time);
    > };
    >
    > and
    >
    > DateTime GetFDate()
    > {
    >
    > time_t current_time;
    > current_time=time(NULL);
    > DatTim.day=localtime(&current_time)->tm_mday;
    > DatTim.month=localtime(&current_time)->tm_mon;
    > DatTim.year=localtime(&current_time)->tm_year+1900;
    > DatTim.LTime=GetFTime();
    >
    > return (DatTim);
    > };
    >
    > where
    >
    > FTime GetFTime()
    > {
    > time_t current_time;
    > current_time=time(NULL);
    >
    > FTim.hour=localtime(&current_time)->tm_hour;
    > FTim.minute=localtime(&current_time)->tm_min;
    > FTim.second=localtime(&current_time)->tm_sec;
    >
    > return (FTim);
    > };
    >
    > the DatTim.month returns to me (currently) february, instead of
    > march! (it generally returns 1 month before the current month).
    > I use GetFDate to write the filename date string. however the
    > bash command 'date' returns the right date... so the attribute
    > timestamp is correct, but the filename timestamp is wrong... any
    > ideas? could it be some bug?


    Could it be something as simple as the fact that tm_mon
    is zero-based, i.e., the value of tm_mon for March is 2, not 3?

    I think there is another, more subtle, issue with the
    above code. It calls time() twice, once for the date and
    once for the time. If called near midnight, it is theoretically
    possible for the date to change between the first and second
    call, and for the time stamp to contain the one day's
    date and the next day's time.

    MV

    --
    I do not want replies; please follow-up to the group.

  5. Re: time(NULL) returning wrong month???

    >DateTime GetFDate()
    >{
    >
    > time_t current_time;
    > current_time=time(NULL);
    > DatTim.day=localtime(&current_time)->tm_mday;
    > DatTim.month=localtime(&current_time)->tm_mon;
    > DatTim.year=localtime(&current_time)->tm_year+1900;
    > DatTim.LTime=GetFTime();
    >
    > return (DatTim);
    >};


    >the DatTim.month returns to me (currently) february, instead of march!


    My guess is that it returns an integer 2, not february.

    The valid range of tm_mon is 0 - 11, not 1 - 12.



+ Reply to Thread