home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE51.TAR / mbase51 / src / mb_time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  10.8 KB  |  488 lines

  1. /*
  2.  * METALBASE 5.1
  3.  *
  4.  * Released January 1st, 1993 by Huan-Ti [ t-richj@microsoft.com ]
  5.  *
  6.  */
  7.  
  8. #include <mbase.h>
  9.  
  10. /*
  11.  * PROTOTYPES -----------------------------------------------------------------
  12.  *
  13.  */
  14.  
  15.    static struct tm *_getlt XARGS( (void) );
  16.  
  17.  
  18. /*
  19.  * VARIABLES ------------------------------------------------------------------
  20.  *
  21.  */
  22.  
  23. static char *WordMonth[12] =
  24.    {
  25.    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  26.    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  27.    };
  28.  
  29. static char *WordDay[7] =
  30.    {
  31.    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  32.    };
  33.  
  34.  
  35. /*
  36.  * SERVICE ROUTINES -----------------------------------------------------------
  37.  *
  38.  */
  39.  
  40. static struct tm *
  41. _getlt ()                 /* == Get LocalTime, in {struct tm *} */
  42. {
  43.    time_t date;
  44.    time (&date);          /* If your compiler complains, try date=time(); */
  45.    return localtime (&date);
  46. }
  47.  
  48.  
  49. /*
  50.  * TIME/DATE FUNCTIONS --------------------------------------------------------
  51.  *
  52.  */
  53.  
  54. long
  55. elap_t (tme)
  56. mb_time tme;
  57. {
  58.    long     dif;
  59.    mb_time  now;
  60.  
  61.    now = curtime();
  62.  
  63.    dif  = 3600L * (long)(GetHours(now)   - GetHours(tme));
  64.    dif +=   60L * (long)(GetMinutes(now) - GetMinutes(tme));
  65.    dif +=    1L * (long)(GetSeconds(now) - GetSeconds(tme));
  66.  
  67.    if (dif < 0)             /* If it's negative, then assume that the */
  68.       dif += (3600L * 24);  /* day has changed, and add 24 hours.     */
  69.  
  70.    return dif;
  71. }
  72.  
  73. mb_time
  74. tmtotime  (tim)  /* Pass NULL to get current time */
  75. struct tm *tim;
  76. {
  77.    struct tm  *ptr;
  78.    mb_time     ret;
  79. #ifndef NO_TIMEB
  80.    struct timeb tb;
  81. #endif
  82.  
  83.    ptr = (tim == (struct tm *)0) ? _getlt() : tim;
  84.  
  85.    SetHours   (ret, ptr->tm_hour);
  86.    SetMinutes (ret, ptr->tm_min);
  87.    SetSeconds (ret, ptr->tm_sec);
  88.    SetMicros  (ret, 0);           /* No 100/sec precision from _getlt(). */
  89.  
  90. #ifndef NO_TIMEB
  91.    if (tim == NULL)
  92.       {
  93.       ftime (&tb);
  94.       SetMicros (ret, tb.millitm / 10);
  95.       }
  96. #endif
  97.  
  98.    return ret;
  99. }
  100.  
  101. mb_date
  102. tmtodate  (tim)  /* Pass NULL to get current date */
  103. struct tm *tim;
  104. {
  105.    struct tm  *ptr;
  106.    mb_date     ret;
  107.  
  108.    ptr = (tim == (struct tm *)0) ? _getlt() : tim;
  109.  
  110.    SetYear  (ret, ptr->tm_year + 1900L);  /* tm_year stored as 0==1900   */
  111.    SetMonth (ret, ptr->tm_mon + 1L);      /* tm_mon stored as 0==january */
  112.    SetDay   (ret, ptr->tm_mday);          /* tm_mday stored as 1==first  */
  113.  
  114.    return ret;
  115. }
  116.  
  117. struct tm *
  118. datetimetotm (dat, tim)
  119. mb_date       dat;
  120. mb_time            tim;
  121. {
  122.    static struct tm ret, *ptr;
  123.  
  124.    dat = (! dat) ? curdate() : dat;
  125.    tim = (! tim) ? curtime() : tim;
  126.    ptr = _getlt();
  127.  
  128.    ptr->tm_sec  = GetSeconds (tim);
  129.    ptr->tm_min  = GetMinutes (tim);
  130.    ptr->tm_hour = GetHours   (tim);
  131.    ptr->tm_mday = GetDay   (dat);         /* tm_mday stored as 1==first  */
  132.    ptr->tm_mon  = GetMonth (dat)-1;       /* tm_mon stored as 0==january */
  133.    ptr->tm_year = GetYear  (dat)-1900;    /* tm_year stored as 0==1900   */
  134.  
  135.    ptr->tm_wday = DayOfWeek (dat);
  136.    ptr->tm_yday = DayJulian (dat);
  137.    ptr->tm_isdst = 0;                     /* Flag for daylight savings   */
  138.  
  139.    return &ret;
  140. }
  141.  
  142. char *
  143. fmt_date (dat, opt)
  144. mb_date   dat;
  145. int            opt;
  146. {
  147.    static char buf[20];
  148.  
  149.    switch (opt)
  150.       {
  151.       case 1:   sprintf ( buf, "%02d/%02d/%02d", GetMonth(dat),
  152.                               GetDay(dat),
  153.                               GetYear(dat) -1900);
  154.                break;
  155.  
  156.       case 2:   sprintf ( buf, "%02d%02d%02d", GetYear(dat) -1900,
  157.                               GetMonth(dat),
  158.                               GetDay(dat));
  159.                break;
  160.  
  161.       case 3:   sprintf ( buf, "%02d-%02d-%02d", GetMonth(dat),
  162.                               GetDay(dat),
  163.                               GetYear(dat) -1900);
  164.                break;
  165.  
  166.       case 4:   sprintf ( buf, "%s %s %d %d", WordDay[DayOfWeek(dat)],
  167.                               WordMonth[GetMonth(dat)-1],
  168.                               GetDay(dat),
  169.                               GetYear(dat) );
  170.                break;
  171.  
  172.       default:  sprintf (buf, "%02d/%02d/%04d", GetMonth(dat),
  173.                              GetDay(dat), GetYear(dat));
  174.                break;
  175.       }
  176.    return buf;
  177. }
  178.  
  179. char *
  180. fmt_time (tim, opt)
  181. mb_time   tim;
  182. int            opt;
  183. {
  184.    static char buf[20];
  185.    int         hr;
  186.  
  187.    hr = GetHours (tim);
  188.  
  189.    hr = (hr == 0) ? 12 : (hr > 12) ? hr-12 : hr;
  190.  
  191.    switch (opt)
  192.       {
  193.       case  1:  sprintf (buf, "%d:%02d %s", hr, GetMinutes(tim),
  194.                               (GetHours(tim) >= 12) ? "pm" : "am");
  195.                break;
  196.  
  197.       case  2:  sprintf (buf, "%02d:%02d", GetHours(tim), GetMinutes(tim));
  198.                break;
  199.  
  200.       default:  sprintf (buf, "%02d:%02d:%02d", GetHours(tim),
  201.                          GetMinutes(tim), GetSeconds(tim));
  202.                break;
  203.       }
  204.    return buf;
  205. }
  206.  
  207. mb_date
  208. scn_date (str)
  209. char     *str;
  210. {
  211.    char     buf[80];
  212.    char    *a, *b;
  213.    long     x;
  214.    mb_date  rtn = (mb_date)0;
  215.  
  216.    if (! str || ! *str)  return rtn;
  217.    strcpy (buf, str);
  218.    if ((a=strpbrk (buf, "/-"))==NULL)
  219.       {
  220.       strzcpy (buf, &str[0], 2);  SetYear  (rtn, atol (buf)+1900L);
  221.       strzcpy (buf, &str[2], 2);  SetMonth (rtn, atol (buf));
  222.       strzcpy (buf, &str[4], 2);  SetDay   (rtn, atol (buf));
  223.       return rtn;
  224.       }
  225.    b=strpbrk(str,"/-")+1;
  226.  
  227.    *a = 0;  SetMonth (rtn, atol (buf));  strcpy (buf, b);
  228.    if ((b=strpbrk (b, "/-"))==NULL)  return rtn;
  229.  
  230.    a=strpbrk(buf, "/-"); *a = 0; SetDay (rtn, atol(buf));
  231.  
  232.    strcpy (buf, b+1);  x=atol(buf);
  233.    if (strlen (buf) < 3)  x += 1900;
  234.    SetYear (rtn, x);
  235.  
  236.    return rtn;
  237. }
  238.  
  239. mb_time
  240. scn_time (str)
  241. char     *str;
  242. {
  243.    char     buf[80];
  244.    char    *a, *b;
  245.    long     x;
  246.    mb_time  rtn = (mb_time)0;
  247.  
  248.    if (! str || ! *str)  return rtn;
  249.    strcpy (buf, str);
  250.    if ((a=strchr (buf, ':'))==NULL)  return rtn;
  251.    b=strchr(str,':')+1;
  252.  
  253.    *a = 0;  SetHours(rtn, atol (buf));  strcpy (buf, b);
  254.    if ((a=strchr (b, ':'))==NULL)
  255.       {
  256.       x=GetHours (rtn);  rtn=(mb_time)0;  SetMinutes (rtn, atol(buf));
  257.       if ((a=strchr (str, 'p'))==NULL)  a=strchr (str, 'P');
  258.       if (a)
  259.          {
  260.          if ((*(a+1) == 'm' || *(a+1) == 'M') && x < 12)
  261.             x += 12;
  262.          }
  263.       else  /* Check for 12:XX:XX am, and change it to 00:XX:XX */
  264.          {
  265.          if ((a=strchr(str,'a')) != NULL || (a=strchr(str,'a')) != NULL)
  266.             if (x == 12)
  267.                x = 0;
  268.          }
  269.       SetHours (rtn, x);
  270.       return rtn;
  271.       }
  272.    b=strchr(buf,':');  *b = 0;  SetMinutes (rtn, atol (buf));
  273.  
  274.    strcpy (buf, a+1);  SetSeconds (rtn, atol (buf));
  275.    
  276.    return rtn;
  277. }
  278.  
  279. int
  280. DaysPerMonth (dat)  /* 28-31, depending on dat's month and day */
  281. mb_date       dat;
  282. {
  283.    switch (GetMonth(dat))
  284.       {
  285.       case  1:  return  31;
  286.       case  2:  return  28 + (DaysPerYear (GetYear(dat)) == 366);
  287.       case  3:  return  31;
  288.       case  4:  return  30;
  289.       case  5:  return  31;
  290.       case  6:  return  30;
  291.       case  7:  return  31;
  292.       case  8:  return  31;
  293.       case  9:  return  30;
  294.       case 10:  return  31;
  295.       case 11:  return  30;
  296.       case 12:  return  31;
  297.       }
  298.    return 0;
  299. }
  300.  
  301. int
  302. DaysPerYear (year)  /* 365 or 366 */
  303. int          year;
  304. {
  305.    if (year % 4   != 0)  return 365;  /* If it's not every fourth year, 365. */
  306.    if (year % 400 == 0)  return 366;  /* Every 400 years IS a leap year.     */
  307.    if (year % 100 == 0)  return 365;  /* Every 100 years ISN'T a leap year.  */
  308.    return 366;
  309. }
  310.  
  311. int
  312. DayOfWeek (dat)  /* Returns 0==sunday; only works after the year 1 BC */
  313. mb_date    dat;
  314. {
  315.    int   year;
  316.    int   dow;   /* Day of week: 0=sun,1=mon,2=tue,3=wed,4=thr,5=fri,6=sat */
  317.  
  318.    dow = 6;      /* January 1st on 0 AD was a saturday */
  319.  
  320.    for (year = 0; year < GetYear(dat); year++)  /* Years left to skip */
  321.       {
  322.       dow += (DaysPerYear (year) == 365) ? 1 : 2;
  323.       }
  324.  
  325.    dow += DayJulian (dat) -1;  /* Add nothing for january 1st */
  326.    dow %= 7;                   /* And track it back into 0-6 */
  327.  
  328.    return dow;
  329. }
  330.  
  331. int
  332. DayJulian (dat)  /* Returns 1==January 1st */
  333. mb_date    dat;
  334. {
  335.    mb_date tdat;
  336.    int     d, m;
  337.  
  338.    SetYear  (tdat, GetYear(dat));
  339.    SetDay   (tdat, GetDay(dat));
  340.  
  341.    d = 0;
  342.    for (m = 1; m < GetMonth(dat); m++)
  343.       {
  344.       SetMonth (tdat, m);
  345.       d += DaysPerMonth(tdat);
  346.       }
  347.  
  348.    d += GetDay(dat);
  349.  
  350.    return d;
  351. }
  352.  
  353.  
  354. /*
  355.  * PHONE ROUTINES -------------------------------------------------------------
  356.  *
  357.  */
  358.  
  359. mb_phone *
  360. phncpy   (trg, src)
  361. mb_phone *trg,*src;
  362. {
  363.    trg->area   = src->area;
  364.    trg->prefix = src->prefix;
  365.    trg->number = src->number;
  366.    trg->ext    = src->ext;
  367.    return trg;
  368. }
  369.  
  370. char *
  371. fmt_phone (ph, opt)    /* opt=1: use () for AC, opt=0: just use -'s */
  372. mb_phone  *ph;
  373. int            opt;
  374. {
  375.    static char buf[25];
  376.    ulong       ac, pre, num, ext;
  377.  
  378.    buf[0] = 0;
  379.  
  380.    if (!ph)
  381.       return buf;
  382.  
  383.    ac  = (ulong)ph->area;
  384.    pre = (ulong)ph->prefix;
  385.    num = (ulong)ph->number;
  386.    ext =        ph->ext;
  387.  
  388.    if (!num)
  389.       return buf;
  390.  
  391.  
  392.    if (ext != 0 && ac == 0)  opt = 2;
  393.    if (ext == 0 && ac == 0)  opt = 5;
  394.    if (ext == 0 && ac != 0)  opt = (opt == 1) ? 3 : 4;
  395.  
  396.    switch (opt)
  397.       {
  398.       case  1: sprintf (buf, "(%ld) %ld-%04ld x%ld", ac, pre, num, ext); break;
  399.       case  2: sprintf (buf,       "%ld-%04ld x%ld",     pre, num, ext); break;
  400.       case  3: sprintf (buf, "(%ld) %ld-%04ld",      ac, pre, num);      break;
  401.       case  4: sprintf (buf,   "%ld-%ld-%04ld",      ac, pre, num);      break;
  402.       case  5: sprintf (buf,       "%ld-%04ld",          pre, num);      break;
  403.       default: sprintf (buf,   "%ld-%ld-%04ld x%ld", ac, pre, num, ext); break;
  404.       }
  405.    return buf;
  406. }
  407.  
  408. void
  409. scn_phone (ph, str)
  410. mb_phone  *ph;
  411. char          *str;
  412. {
  413.    char  *a, *b, buf[128];
  414.  
  415.    ph->area = ph->prefix = ph->number = (ushort)0;
  416.    ph->ext  = 0L;
  417.    strcpy (buf, str);
  418.  
  419.    if ((a = strchr (buf, '(')) != NULL)
  420.       {
  421.       if ((b = strchr (a, ')')) == NULL)  return;
  422.       *b = 0;  b++;
  423.       ph->area = (ushort)atol (1+a);
  424.       }
  425.    else
  426.       {
  427.       if ((a = strchr (buf, '-')) == NULL)  return;
  428.       if (strchr (1+a, '-') != NULL)
  429.          {
  430.          b = a; *b = 0;  b++;
  431.          ph->area = (ushort)atol(buf);
  432.          }
  433.       else
  434.          {
  435.          b = &buf[0];  /* No area code */
  436.          }
  437.       }
  438.  
  439.    if ((a = strchr (b, '-')) == NULL)  { ph->area = 0; return; }
  440.    *a = 0; a++;
  441.    ph->prefix = (ushort)atol (b);
  442.  
  443.    if ((b = strpbrk (a, " xX")) != NULL)
  444.       {
  445.       *b = 0;
  446.       b++;
  447.       }
  448.  
  449.    ph->number = (ushort)atol (a);
  450.  
  451.    if (b != NULL)
  452.       {
  453.       if ((a = strpbrk (b, "xX")) != NULL)
  454.          b = 1+a;
  455.  
  456.       ph->ext = (ulong)atol (b);
  457.       }
  458. }
  459.  
  460. double
  461. tomoney (n)       /* Rounds to two decimal places of precision */
  462. double   n;
  463. {
  464.    long   x;
  465.    x = (long)((n + 0.005) * 100.0);
  466.    return (x / 100.0);
  467. }
  468.  
  469. int
  470. PhaseOfMoon (dt)  /* 0-7; 0 = new moon, 4 = full moon */
  471. mb_date      dt;
  472. {
  473.    int  first, second;
  474.  
  475.    if (! dt)
  476.       {
  477.       dt = curdate();
  478.       }
  479.  
  480.    first  = (GetYear(dt) % 19) + 1;
  481.    second = (11 * first + 18) % 30;
  482.  
  483.    if ((second == 25 && first > 11) || second == 24)  second++;
  484.  
  485.    return ( ((((( DayJulian(dt) +second) * 6) + 11) % 177) / 22) & 7);
  486. }
  487.  
  488.