/*1* tclUnixTime.c --2*3* Contains Unix specific versions of Tcl functions that4* obtain time values from the operating system.5*6* Copyright (c) 1995 Sun Microsystems, Inc.7*8* See the file "license.terms" for information on usage and redistribution9* of this file, and for a DISCLAIMER OF ALL WARRANTIES.10*11* SCCS: @(#) tclUnixTime.c 1.11 96/07/23 16:17:2112*/1314#if _PACKAGE_ast1516#include <ast.h>17#include <tm.h>18#include <tv.h>1920#include "tcl.h"2122/*23* see function comments below for details24*/2526unsigned long27TclpGetSeconds()28{29return time(NiL);30}3132unsigned long33TclpGetClicks()34{35Tv_t tv;3637tvgettime(&tv);38return 1000000 * tv.tv_sec + tv.tv_nsec / 1000;39}4041int42TclpGetTimeZone(now)43unsigned long now;44{45tmset(tm_info.zone);46return tm_info.zone->west;47}4849void50TclpGetTime(tp)51Tcl_Time* tp;52{53Tv_t tv;5455tvgettime(&tv);56tp->sec = tv.tv_sec;57tp->usec = tv.tv_nsec / 1000;58}5960#else /* _PACKAGE_ast */6162#include "tclInt.h"63#include "tclPort.h"6465/*66*-----------------------------------------------------------------------------67*68* TclpGetSeconds --69*70* This procedure returns the number of seconds from the epoch. On71* most Unix systems the epoch is Midnight Jan 1, 1970 GMT.72*73* Results:74* Number of seconds from the epoch.75*76* Side effects:77* None.78*79*-----------------------------------------------------------------------------80*/8182unsigned long83TclpGetSeconds()84{85return time((time_t *) NULL);86}8788/*89*-----------------------------------------------------------------------------90*91* TclpGetClicks --92*93* This procedure returns a value that represents the highest resolution94* clock available on the system. There are no garantees on what the95* resolution will be. In Tcl we will call this value a "click". The96* start time is also system dependant.97*98* Results:99* Number of clicks from some start time.100*101* Side effects:102* None.103*104*-----------------------------------------------------------------------------105*/106107unsigned long108TclpGetClicks()109{110unsigned long now;111#ifdef NO_GETTOD112struct tms dummy;113#else114struct timeval date;115struct timezone tz;116#endif117118#ifdef NO_GETTOD119now = (unsigned long) times(&dummy);120#else121gettimeofday(&date, &tz);122now = date.tv_sec*1000000 + date.tv_usec;123#endif124125return now;126}127128/*129*----------------------------------------------------------------------130*131* TclpGetTimeZone --132*133* Determines the current timezone. The method varies wildly134* between different platform implementations, so its hidden in135* this function.136*137* Results:138* Hours east of GMT.139*140* Side effects:141* None.142*143*----------------------------------------------------------------------144*/145146int147TclpGetTimeZone (currentTime)148unsigned long currentTime;149{150/*151* Determine how a timezone is obtained from "struct tm". If there is no152* time zone in this struct (very lame) then use the timezone variable.153* This is done in a way to make the timezone variable the method of last154* resort, as some systems have it in addition to a field in "struct tm".155* The gettimeofday system call can also be used to determine the time156* zone.157*/158159#if defined(HAVE_TM_TZADJ)160# define TCL_GOT_TIMEZONE161time_t curTime = (time_t) currentTime;162struct tm *timeDataPtr = localtime(&curTime);163int timeZone;164165timeZone = timeDataPtr->tm_tzadj / 60;166if (timeDataPtr->tm_isdst) {167timeZone += 60;168}169170return timeZone;171#endif172173#if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)174# define TCL_GOT_TIMEZONE175time_t curTime = (time_t) currentTime;176struct tm *timeDataPtr = localtime(¤tTime);177int timeZone;178179timeZone = -(timeDataPtr->tm_gmtoff / 60);180if (timeDataPtr->tm_isdst) {181timeZone += 60;182}183184return timeZone;185#endif186187/*188* Must prefer timezone variable over gettimeofday, as gettimeofday does189* not return timezone information on many systems that have moved this190* information outside of the kernel.191*/192193#if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)194# define TCL_GOT_TIMEZONE195static int setTZ = 0;196int timeZone;197198if (!setTZ) {199tzset();200setTZ = 1;201}202203/*204* Note: this is not a typo in "timezone" below! See tzset205* documentation for details.206*/207208timeZone = timezone / 60;209210return timeZone;211#endif212213#if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE)214# define TCL_GOT_TIMEZONE215struct timeval tv;216struct timezone tz;217int timeZone;218219gettimeofday(&tv, &tz);220timeZone = tz.tz_minuteswest;221if (tz.tz_dsttime) {222timeZone += 60;223}224225return timeZone;226#endif227228#ifndef TCL_GOT_TIMEZONE229/*230* Cause compile error, we don't know how to get timezone.231*/232error: autoconf did not figure out how to determine the timezone.233#endif234235}236237/*238*----------------------------------------------------------------------239*240* TclpGetTime --241*242* Gets the current system time in seconds and microseconds243* since the beginning of the epoch: 00:00 UCT, January 1, 1970.244*245* Results:246* Returns the current time in timePtr.247*248* Side effects:249* None.250*251*----------------------------------------------------------------------252*/253254void255TclpGetTime(timePtr)256Tcl_Time *timePtr; /* Location to store time information. */257{258struct timeval tv;259struct timezone tz;260261(void) gettimeofday(&tv, &tz);262timePtr->sec = tv.tv_sec;263timePtr->usec = tv.tv_usec;264}265266#endif /* _PACKAGE_ast */267268269