неясность с difftime

Discussion in 'WASM.UNIX' started by varnie, Oct 3, 2007.

  1. varnie

    varnie New Member

    Blog Posts:
    0
    привет, дружыщща.

    имеется следующее:
    Code (Text):
    1. time_t firsttime, nexttime;
    2. firsttime = time((time_t *) NULL);
    3. for ( условия )
    4. {
    5. //...
    6. //здесь что-то делаем
    7. //и здесь тоже
    8. //...
    9. nexttime = time((time_t *) NULL);
    10. if ( difftime(nexttime, firsttime) > 1.0f )
    11. {  
    12.     printf("firsttime = %s\n nexttime = %s\n difftime = %f\n", ctime(&firsttime), ctime(&nexttime), difftime(nexttime, firsttime));
    13.     firsttime = nexttime;
    14. }
    15. }
    выводятся одинаковые даты со временем, т.е. по логике разница во времени должна быть > 1 сек., но тем не менее printf отображает абсолютно одинаковые firsttime и nexttime вплоть до секунды.

    почему так происходит, и где я пролетел?
     
  2. nobodyzzz

    nobodyzzz New Member

    Blog Posts:
    0
    char* ctime(const time_t* timer)
    Convert time_t time value to string in the same format as asctime. The string pointed is statically allocated and shared by ctime and asctime functions. Each time one of these functions is called the content of the string is overwritten. ctime also uses internally the buffer used by gmtime and localtime as return value, so a call to this function will overwrite this.
    тоесть надо делать
    Code (Text):
    1. strcpy(sz1sttime, ctime(&firtstime);
    2. strcpy(sz2ndtime, ctime(&nextime);
    3. ...
     
  3. varnie

    varnie New Member

    Blog Posts:
    0
    nobodyzzz,
    да, теперь все работает. спасибо за помощь.

    оффтоп, но вот сейчас попробовал реализовать то же самое при помощи SDL либы и ее ф-ций, т.е. применил:
    Code (Text):
    1. uint firsttime = SDL_GetTicks();
    2. uint nexttime = firsttime;
    3. uint FPS = 0;
    4. for ( условие )
    5. {
    6. //поскипано
    7. ++FPS;
    8. nexttime = SDL_GetTicks();
    9. if ( nexttime - firsttime > 1000 ) { printf("FPS = %d\n", FPS); FPS = 0;  firsttime = nexttime; }
    10. }
    и FPS в 2 раза с лишним б0льший высчитывается чем ежели как в посте #1 применять работу с unix ф-циями по получению разницы во времени для того чтобы производить расчеты.

    непонятно, т.к. диапазон по времени что в посте #1, что здесь указан 1 секунда (или 1000 миллисекунд как здесь, что одно и то же ведь), а FPS совсем разные высчитываются.

    может и здесь меня ткнете носом куда надо) спасибо.