Hogwarts Wand Docs: ../server/ac_time.c

Wand Sourcecode: ../server/ac_time.c

//
// ac_time.c - time functions
//
// Copyright (C) 2006  Nathan (Acorn) Pooley
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License (gpl.txt) for more details. 
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
// 
// You can contact the author, Nathan (Acorn) Pooley, by writing
// to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
// or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
//

//#@DOC@ time functions

//###########################################################################
//############################### INCLUDES ##################################
//###########################################################################

#include "ac_time.h"

#include <sys/time.h>
#include <time.h>

//###########################################################################
//############################### TYPEDEFS ##################################
//###########################################################################

//###########################################################################
//############################### GLOBALS ###################################
//###########################################################################

//###########################################################################
//############################### CODE ######################################
//###########################################################################

//===========================================================================
// AcTimeGet() - return t1 - t0.  If t1<t0 returns sec=0, usec=0
//===========================================================================
// returns 0 on success.
int AcTimeGet(AcTime *result)
{
    struct timeval tv;
    int rv = gettimeofday(&tv,0);
    result->sec = tv.tv_sec;
    result->usec = tv.tv_usec;
    return rv;
}

//===========================================================================
// AcTimeDiffLong() - return t1 - t0.  If t1<t0 returns sec=0, usec=0
//===========================================================================
// returns 0 on success.
int AcTimeDiff(AcTime *result, AcTime *t1, AcTime *t0)
{
    time_t t = 0;
    long us = t1->usec - t0->usec;

    if (t1->sec == t0->sec) {
        if (us < 0) us = 0;

    } else if (t1->sec < t0->sec) {
        us = 0;

    } else {
        t = t1->sec - t0->sec;
        if (us < 0) {
            do {
                us += 1000000;
                if (t==0) {
                    us = 0;
                    break;
                }
                t  -= 1;
            } while(us < 0);
        } else {
            while(us > 1000000) {
                us -= 1000000;
                t  += 1;
            }
        }
    }
    
    result->sec  = t;
    result->usec = us;
    return 0;
}

This file Copyright (C) 2006 by Nathan (Acorn) Pooley
Go to TOP Wand page
Go to Acorn's personal webpage
Go to Hogwarts website: www.gotohogwarts.com
Snout: www.snout.org/game
Gadgets of Justice Unlimited
Snout GC (Wiki)
Snout Wiki
File created by do_doc at Wed May 30 03:25:04 PDT 2007