"Fossies" - the Fresh Open Source Software Archive

Member "ical-tcl/time/Time.C" (15 Apr 2019, 3411 Bytes) of package /linux/privat/ical-3.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "Time.C": 3.0.2_vs_3.0.3.

    1 /* Copyright (c) 1993 by Sanjay Ghemawat */
    2 
    3 #include <stddef.h>
    4 #include <sys/types.h>
    5 #include <math.h>
    6 #include <string.h>
    7 #include <alloca.h>
    8 #include <stdlib.h>
    9 
   10 #include "config.h"
   11 
   12 #include "Month.h"
   13 #include "WeekDay.h"
   14 #include "Time_.h"
   15 
   16 /*
   17  * Want to get high resolution for region of time that will be most
   18  * heavily used.  This region will probably be around now. Therefore,
   19  * rep will be number of seconds elapsed since we construct the first
   20  * Time value for this run of the program.
   21  */
   22 
   23 int    Time::initialized = 0;   /* Initialized yet? */
   24 double Time::offset = 0.0;      /* Offset for time values */
   25 int    Time::junkInt = 0;
   26 
   27 void Time::Initialize() {
   28     struct timeval buf;
   29     gettimeofday(&buf, 0);
   30 
   31     offset = buf.tv_sec;
   32     initialized = 1;
   33 }
   34 
   35 Time Time::Now() {
   36     struct timeval buf;
   37     gettimeofday(&buf, 0);
   38     return Time(buf);
   39 }
   40 
   41 void Time::BreakDownDate(int& mday,WeekDay& wday,Month& month, int& year) const
   42 {
   43     this->BreakDown(mday, wday, month, year);
   44 }
   45 
   46 void Time::BreakDownClock(int& hour, int& min, int& sec, int& milli) const {
   47     WeekDay junkWDay;
   48     Month   junkMonth;
   49 
   50     BreakDown(junkInt,junkWDay,junkMonth,junkInt, hour, min, sec, milli);
   51 }
   52 
   53 void Time::BreakDown(int& mday, WeekDay& wday, Month& month, int& year,
   54                      int& hour, int& min, int& sec, int& milli,
   55                      const char *tz) const
   56 {
   57     char *old;
   58     if (! initialized) Initialize();
   59 
   60     time_t clock = (time_t) round(rep + offset);
   61 
   62     if (tz) {
   63         if ((old=getenv("TZ"))) old=strdup(old);
   64         setenv("TZ", tz, 1);
   65         tzset();
   66     }
   67 
   68     struct tm* t = localtime(&clock);
   69 
   70     if (tz) {
   71         if (old) setenv("TZ", old, 1); else unsetenv("TZ");
   72         tzset();
   73         free(old);
   74     }
   75 
   76     mday  = t->tm_mday;                         /* tm_mday in 1..31 */
   77     wday  = WeekDay::Sunday() + t->tm_wday;     /* tm_wday in 0..6.  Sun = 0 */
   78     month = Month::January() + t->tm_mon;       /* tm_mon  in 0..11. Jan = 0 */
   79     year  = t->tm_year + 1900;
   80     hour  = t->tm_hour;
   81     min   = t->tm_min;
   82     sec   = t->tm_sec;
   83     milli = (int)round((rep - floor(rep)) * 1000);
   84 }
   85 
   86 Time::Time(const struct timeval& tv) {
   87     if (! initialized) Initialize();
   88     rep = (tv.tv_sec - offset) + ((double) tv.tv_usec) / 1000000.0;
   89 }
   90 
   91 void Time::Convert(struct timeval& tv) const {
   92     if (! initialized) Initialize();
   93 
   94     tv.tv_sec  = (long) floor(rep + offset);
   95     tv.tv_usec = (long) round((rep + offset - tv.tv_sec) * 1000000.0);
   96 }
   97 
   98 #if 0
   99 time_t timezone_to_local(time_t clock, const char *tz) {
  100     const char *old=getenv("TZ");
  101     if (old) old=strdupa(old);
  102     
  103     struct tm* t = localtime(&clock);
  104     setenv("TZ", tz, 1);
  105     tzset();
  106     
  107     clock = mktime(t);
  108     if (old) setenv("TZ", old, 1); else unsetenv("TZ");
  109     tzset();
  110 
  111     return clock;
  112 }
  113 
  114 time_t local_to_timezone(time_t clock, const char *tz) {
  115     const char *old=getenv("TZ");
  116     if (old) old=strdupa(old);
  117     
  118     setenv("TZ", tz, 1);
  119     tzset();
  120     struct tm* t = localtime(&clock);
  121     
  122     if (old) setenv("TZ", old, 1); else unsetenv("TZ");
  123     tzset();
  124     clock = mktime(t);
  125 
  126     return clock;
  127 }
  128 #endif
  129 
  130 Duration::Duration(const struct timeval& tv) {
  131     rep = tv.tv_sec + ((double) tv.tv_usec) / 1000000.0;
  132 }
  133 
  134 void Duration::Convert(struct timeval& tv) const {
  135     tv.tv_sec  = (long) floor(rep);
  136     tv.tv_usec = (long) round((rep - tv.tv_sec) / 1000000.0);
  137 }