home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / dev / Libraries / Date / src / Date_Time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-18  |  16.2 KB  |  660 lines

  1.  
  2.  /* ----------------------------------------------------------------------- */
  3.  
  4.  #ifndef __MakeLib
  5.    float TimeToJD(const unsigned short hour, const unsigned short min, const unsigned short sec)
  6.  #else
  7.    float __saveds __asm TimeToJD(register __d0 const unsigned short hour, register __d1 const unsigned short min, register __d2 const unsigned short sec)
  8.  #endif
  9.  
  10. /*
  11. ******* Date/TimeToJD *******************************************************
  12. *
  13. *   NAME
  14. *    TimeToJD -- Returns the JD for a time. (V33)
  15. *
  16. *   SYNOPSIS
  17. *    jd = TimeToJD(hour,min,sec);
  18. *    d0           d0   d1  d2
  19. *
  20. *    float TimeToJD(const unsigned short hour, const unsigned short min,
  21. *        const unsigned short sec);
  22. *
  23. *   FUNCTION
  24. *    Returns the JD for a specified time.
  25. *
  26. *   INPUTS
  27. *    hour - hour of the time to convert
  28. *    min  - minute of the time to convert
  29. *    sec  - sec. of the time to convert
  30. *
  31. *   RESULT
  32. *    jd - This is the JD time
  33. *
  34. *   EXAMPLE
  35. *    ...
  36. *    jd = TimeToJD(16,33,0);
  37. *    ...
  38. *
  39. *   NOTES
  40. *    none
  41. *
  42. *   BUGS
  43. *    There is no check, if the specified time is a valid time!
  44. *
  45. *   SEE ALSO
  46. *    JDToTime()
  47. *
  48. *****************************************************************************
  49. *
  50. *
  51. */
  52.  
  53.  {
  54.   return((float)(((unsigned long)hour*3600+(unsigned int)min*60+sec) / 86400.0));
  55.  }
  56.  
  57.  
  58.  #ifndef __MakeLib
  59.    #ifndef __cplusplus
  60.      void JDToTime(float jd, unsigned short *const rhour, unsigned short *const rmin, unsigned short *const rsec)
  61.    #else
  62.      void JDToTime(float jd, unsigned short &rhour, unsigned short &rmin, unsigned short &rsec)
  63.    #endif
  64.  #else
  65.    void __saveds __asm JDToTime(register __d0 float jd, register __a0 unsigned short *const rhour, register __a1 unsigned short *const rmin, register __a2 unsigned short *const rsec)
  66.  #endif
  67.  
  68. /*
  69. ******* Date/JDToTime *******************************************************
  70. *
  71. *   NAME
  72. *    JDToTime -- Returns the real time for a JD time. (V33)
  73. *
  74. *   SYNOPSIS
  75. *    JDToTime(jd,rhour,rmin,rsec);
  76. *         d0  a0    a1   a2
  77. *
  78. *    void JDToTime(float jd, unsigned short *const rhour,
  79. *        unsigned short *const rmin, unsigned short *const rsec);
  80. *
  81. *    void JDToTime(float jd, unsigned short &rhour, unsigned short &rmin,
  82. *        unsigned short &rsec);
  83. *
  84. *   FUNCTION
  85. *    Returns the real time for a JD time.
  86. *
  87. *   INPUTS
  88. *    jd - JD time
  89. *
  90. *   RESULT
  91. *    rhour - 24 hour real time
  92. *    rmin  - real minutes
  93. *    rsec  - real seconds
  94. *
  95. *   EXAMPLE
  96. *    ...
  97. *    JDToTime(0.76543,&rhour,&rmin,&rsec);
  98. *    ...
  99. *
  100. *   NOTES
  101. *    none.
  102. *
  103. *   BUGS
  104. *    If jd is > 0 (including days) there will be occur arithmetic bugs!
  105. *
  106. *   SEE ALSO
  107. *    TimeToJD()
  108. *
  109. *****************************************************************************
  110. *
  111. *
  112. */
  113.  
  114.  {
  115.   unsigned long sec;
  116.  
  117.   if (jd > 0.0)
  118.     jd -= (float)floor((double)jd);
  119.   sec = (unsigned long)(jd * 86400.0);
  120.   #ifndef __cplusplus
  121.     *rhour = (unsigned short)(sec / 3600);
  122.     sec -= (*rhour) * 3600;
  123.     *rmin = (unsigned short)(sec / 60);
  124.     sec -= (*rmin) * 60;
  125.     *rsec = (unsigned short)sec;
  126.   #else
  127.     rhour = (unsigned short)(sec / 3600);
  128.     sec -= rhour * 3600;
  129.     rmin = (unsigned short)(sec / 60);
  130.     sec -= rmin * 60;
  131.     rsec = (unsigned short)sec;
  132.   #endif
  133.  }
  134.  
  135.  /* ----------------------------------------------------------------------- */
  136.  
  137.  #ifndef __MakeLib
  138.    short TimeZoneFactor(const short degree)
  139.  #else
  140.    short __saveds __asm TimeZoneFactor(register __d0 const short degree)
  141.  #endif
  142.  
  143. /*
  144. ******* Date/TimeZoneFactor *************************************************
  145. *
  146. *   NAME
  147. *    TimeZoneFactor -- Returns the value you have to add to GMT time (V33)
  148. *
  149. *   SYNOPSIS
  150. *    addhours = TimeZoneFactor(degrees);
  151. *       d0                d0
  152. *
  153. *    short TimeZoneFactor(const short degree);
  154. *
  155. *   FUNCTION
  156. *    This gives you the hours you have to add to GMT time,
  157. *    specified on the fact, that a timezone is 15 degrees
  158. *    and that GMT is centered on 0 degrees!
  159. *
  160. *   INPUTS
  161. *    degrees - Position of timezone you live in
  162. *    (from -180 east to +180 west)
  163. *
  164. *   RESULT
  165. *    addhours - Time to add to GMT time to get your locale zone time
  166. *        (-12 to +12)
  167. *
  168. *   EXAMPLE
  169. *    ...
  170. *    addhours = TimeZoneFactor(-8);
  171. *    ...
  172. *
  173. *   NOTES
  174. *    none
  175. *
  176. *   BUGS
  177. *    No errorcheck, if you put in valid degrees (-180 to +180)
  178. *    Only full degrees are supportet, keep sure that you
  179. *    round in the right way for 0.x degree places
  180. *    I am not sure about the correct +/- behaviour!!!
  181. *
  182. *   SEE ALSO
  183. *
  184. *
  185. *****************************************************************************
  186. *
  187. *
  188. */
  189.  
  190.  {
  191.   if (degree >= 0)
  192.     return((short)(degree / 15.0 + 0.5));
  193.   else
  194.     return((short)(degree / 15.0 - 0.5));
  195.  }
  196.  
  197.  
  198.  #ifndef __MakeLib
  199.    long LMT(const unsigned long secs, const float meridiandegree, const float posdegree)
  200.  #else
  201.    long __saveds __asm LMT(register __d0 const unsigned long secs, register __d1 const float meridiandegree, register __d2 const float posdegree)
  202.  #endif
  203.  
  204. /*
  205. ******* Date/LMT ************************************************************
  206. *
  207. *   NAME
  208. *    LMT -- Calculates your local time in your timezone (V33)
  209. *
  210. *   SYNOPSIS
  211. *    secs = LMT(secs,meridian,pos);
  212. *     d0        d0     d1    d2
  213. *
  214. *    unsigned long LMT(const unsigned long secs,
  215. *        const float meridiandegree, const float posdegree);
  216. *
  217. *   FUNCTION
  218. *    Calculates your Local Mean Time of your place!
  219. *
  220. *   INPUTS
  221. *    secs     - Seconds of the running day (hours*3600+min*60+sec)
  222. *    meridian - Degrees of your timezone-meridian
  223. *    pos      - Degrees of your place
  224. *
  225. *   RESULT
  226. *    secs - Local seconds of the running day
  227. *
  228. *   EXAMPLE
  229. *    ...
  230. *    secs = LMT(76080,-15.0,-8.923055556);
  231. *    ...
  232. *
  233. *   NOTES
  234. *    none
  235. *
  236. *   BUGS
  237. *    No errorcheck, if you put in valid degrees (-180 to +180)
  238. *
  239. *   SEE ALSO
  240. *
  241. *
  242. *****************************************************************************
  243. *
  244. *
  245. */
  246.  
  247.  {
  248.   return((long)secs + (long)((meridiandegree / 15.0 - posdegree / 15.0)*3600.0));
  249.  }
  250.  
  251.  /* ----------------------------------------------------------------------- */
  252.  
  253.  #ifndef __MakeLib
  254.    unsigned long TimeToSec(const unsigned short hour, const unsigned short min, const unsigned short sec)
  255.  #else
  256.    unsigned long __saveds __asm TimeToSec(register __d0 const unsigned short hour, register __d1 const unsigned short min, register __d2 const unsigned short sec)
  257.  #endif
  258.  
  259. /*
  260. ******* Date/TimeToSec ******************************************************
  261. *
  262. *   NAME
  263. *    TimeToSec -- Returns the time in seconds (V33)
  264. *
  265. *   SYNOPSIS
  266. *    secs = TimeToSec(hour,min,sec);
  267. *     d0          d0   d1  d2
  268. *
  269. *    unsigned long TimeToSec(const unsigned short hour,
  270. *        const unsigned short min, const unsigned short sec);
  271. *
  272. *   FUNCTION
  273. *    Gives you back the time in seconds
  274. *
  275. *   INPUTS
  276. *    hour - hours you want (0-23)
  277. *    min  - minutes you want (0-59)
  278. *    sec  - seconds you want (0-59)
  279. *
  280. *   RESULT
  281. *    secs - Time in seconds
  282. *
  283. *   EXAMPLE
  284. *    ...
  285. *    secs = TimeToSec(21,15,00);
  286. *    ...
  287. *
  288. *   NOTES
  289. *    Don't forget to convert AM/PM time to 24h time!
  290. *
  291. *   BUGS
  292. *    No errorcheck, if you use a valid time
  293. *
  294. *   SEE ALSO
  295. *    SecToTime()
  296. *
  297. *****************************************************************************
  298. *
  299. *
  300. */
  301.  
  302.  {
  303.   return((unsigned long)hour*3600+(unsigned long)min*60+sec);
  304.  }
  305.  
  306.  
  307.  #ifndef __MakeLib
  308.    #ifndef __cplusplus
  309.      void SecToTime(unsigned long secs, unsigned short *const hour, unsigned short *const min, unsigned short *const sec)
  310.    #else
  311.      void SecToTime(unsigned long secs, unsigned short &hour, unsigned short &min, unsigned short &sec)
  312.    #endif
  313.  #else
  314.    void __saveds __asm SecToTime(register __d0 unsigned long secs, register __a0 unsigned short *const hour, register __a1 unsigned short *const min, register __a2 unsigned short *const sec)
  315.  #endif
  316.  
  317. /*
  318. ******* Date/SecToTime ******************************************************
  319. *
  320. *   NAME
  321. *    SecToTime -- Returns the time from seconds (V33)
  322. *
  323. *   SYNOPSIS
  324. *    SecToTime(secs,hour,min,sec);
  325. *           d0   a0  a1  a2
  326. *
  327. *    void SecToTime(unsigned long secs, unsigned short *const hour,
  328. *        unsigned short *const min, unsigned short *const sec);
  329. *
  330. *    void SecToTime(unsigned long secs, unsigned short &hour,
  331. *        unsigned short &min, unsigned short &sec);
  332. *
  333. *   FUNCTION
  334. *    Gives you back the time from the specified seconds
  335. *
  336. *   INPUTS
  337. *    secs - Time in seconds
  338. *
  339. *   RESULT
  340. *    hour - hours (0-23)
  341. *    min  - minutes (0-59)
  342. *    sec  - seconds (0-59)
  343. *
  344. *   EXAMPLE
  345. *    ...
  346. *    SecToTime(76860,&hour,&min,&sec);
  347. *    ...
  348. *
  349. *   NOTES
  350. *    Don't forget to convert 24h time to AM/PM time if needed!
  351. *
  352. *   BUGS
  353. *    No errorcheck, if you use a valid time
  354. *
  355. *   SEE ALSO
  356. *    TimeToSec()
  357. *
  358. *****************************************************************************
  359. *
  360. *
  361. */
  362.  
  363.  {
  364.   #ifndef __cplusplus
  365.     *hour = (unsigned short)(secs / 3600);
  366.     secs -= (unsigned long)(*hour) * 3600;
  367.     *min = (unsigned short)(secs / 60);
  368.     *sec = (unsigned short)(secs - (unsigned long)(*min) * 60);
  369.   #else
  370.     hour = (unsigned short)(secs / 3600);
  371.     secs -= (unsigned long)(hour) * 3600;
  372.     min = (unsigned short)(secs / 60);
  373.     sec = (unsigned short)(secs - (unsigned long)(min) * 60);
  374.   #endif
  375.  }
  376.  
  377.  /* ----------------------------------------------------------------------- */
  378.  
  379.  #ifndef __MakeLib
  380.    short Compare2Times(const unsigned short hour1, const unsigned short min1, const unsigned short sec1, const unsigned short hour2, const unsigned short min2, const unsigned short sec2)
  381.  #else
  382.    short __saveds __asm Compare2Times(register __d0 const unsigned short hour1, register __d1 const unsigned short min1, register __d2 const unsigned short sec1, register __d3 const unsigned short hour2, register __d4 const unsigned short min2, register __d5 const unsigned short sec2)
  383.  #endif
  384.  
  385. /*
  386. ******* Date/Compare2Times **************************************************
  387. *
  388. *   NAME
  389. *    Compare2Times -- Compares time1 with time2. (V33.100)
  390. *
  391. *   SYNOPSIS
  392. *    compare = Compare2Times(hour1,min1,sec1,hour2,min2,sec2);
  393. *      d0             d0    d1   d2   d3    d4   d5
  394. *
  395. *    short Compare2Times(const unsigned short hour1,
  396. *        const unsigned short min1, const unsigned short sec1,
  397. *        const unsigned short hour2, const unsigned short min2,
  398. *        const unsigned short sec2);
  399. *
  400. *   FUNCTION
  401. *    Compare2Times compares time1 with time2 (24h format only).
  402. *
  403. *   INPUTS
  404. *    hour1 - Hour of the first time.
  405. *    min1  - Minute of the first time.
  406. *    sec1  - Second of the first time.
  407. *    hour2 - Hour of the second time.
  408. *    min2  - Minute of the second time.
  409. *    sec2  - Second of the second time.
  410. *
  411. *   RESULT
  412. *    compare - -1 : time1 < time2
  413. *           0 : time1 = time2
  414. *           1 : time1 > time2
  415. *
  416. *   EXAMPLE
  417. *    ...
  418. *    if (Compare2Times(13,10,0,9,0,0) == -1)
  419. *      printf("<\n");
  420. *    else
  421. *      printf(">=\n");
  422. *    ...
  423. *
  424. *   NOTES
  425. *    This compares two times of 24h format!
  426. *
  427. *   BUGS
  428. *    There is no check if the times are valid times!
  429. *
  430. *   SEE ALSO
  431. *    Compare2Dates()
  432. *
  433. *****************************************************************************
  434. *
  435. *
  436. */
  437.  
  438.  {
  439.   if (hour1 < hour2)
  440.     return(-1);
  441.   else if (hour1 > hour2)
  442.     return(1);
  443.   else if (min1 < min2)
  444.     return(-1);
  445.   else if (min1 > min2)
  446.     return(1);
  447.   else if (sec1 < sec2)
  448.     return(-1);
  449.   else if (sec1 > sec2)
  450.     return(1);
  451.   else
  452.     return(0);
  453.  }
  454.  
  455.  /* ----------------------------------------------------------------------- */
  456.  
  457.  #ifndef __MakeLib
  458.    bool ValidTime(const unsigned short hour, const unsigned short min, const unsigned short sec)
  459.  #else
  460.    bool __saveds __asm ValidTime(register __d0 const unsigned short hour, register __d1 const unsigned short min, register __d2 const unsigned short sec)
  461.  #endif
  462.  
  463. /*
  464. ******* Date/ValidTime ******************************************************
  465. *
  466. *   NAME
  467. *    ValidTime -- Checks if the time is a valid 24h-format time (V33.135)
  468. *
  469. *   SYNOPSIS
  470. *    valid = ValidTime(hour,min,sec);
  471. *      d0           d0   d1  d2
  472. *
  473. *    bool ValidTime(const unsigned short hour,
  474. *        const unsigned short min, const unsigned short sec);
  475. *
  476. *   FUNCTION
  477. *    ValidTime checks if the time (24h format only) is valid.
  478. *
  479. *   INPUTS
  480. *    hour - Hour of the time.
  481. *    min  - Minute of the time.
  482. *    sec  - Second of the time.
  483. *
  484. *   RESULT
  485. *    valid - true  : The time is ok.
  486. *        false : This is not a correct time!
  487. *
  488. *   EXAMPLE
  489. *    ...
  490. *    if (ValidTime(25,0,0))
  491. *      printf("ok\n");
  492. *    else
  493. *      printf("wrong time!!!\n");
  494. *    ...
  495. *
  496. *   NOTES
  497. *    None.
  498. *
  499. *   BUGS
  500. *    None.
  501. *
  502. *   SEE ALSO
  503. *    ValidJulianDate(),ValidGregorianDate(),ValidHeisDate()
  504. *
  505. *****************************************************************************
  506. *
  507. *
  508. */
  509.  
  510.  {
  511.   if ((hour < 24) && (min < 60) && (sec < 60))
  512.     return(true);
  513.   else
  514.     return(false);
  515.  }
  516.  
  517.  /* ----------------------------------------------------------------------- */
  518.  
  519.  #ifndef __MakeLib
  520.    long TimeDiff(const unsigned short hour1, const unsigned short min1, const unsigned short sec1, const unsigned short hour2, const unsigned short min2, const unsigned short sec2)
  521.  #else
  522.    long __saveds __asm TimeDiff(register __d0 const unsigned short hour1, register __d1 const unsigned short min1, register __d2 const unsigned short sec1, register __d3 const unsigned short hour2, register __d4 const unsigned short min2, register __d5 const unsigned short sec2)
  523.  #endif
  524.  
  525. /*
  526. ******* Date/TimeDiff *******************************************************
  527. *
  528. *   NAME
  529. *    TimeDiff -- Returns the difference in seconds (V33)
  530. *
  531. *   SYNOPSIS
  532. *    secs = TimeDiff(hour1,min1,sec1,hour2,min2,sec2);
  533. *     d0         d0    d1   d2   d3    d4   d5
  534. *
  535. *    long TimeDiff(const unsigned short hour1, const unsigned short min1,
  536. *        const unsigned short sec1, const unsigned short hour2,
  537. *        const unsigned short min2, const unsigned short sec2);
  538. *
  539. *   FUNCTION
  540. *    Gives you back the difference between the first and the second time
  541. *    in seconds.
  542. *
  543. *   INPUTS
  544. *    hour1 - hours of the first time
  545. *    min1  - minutes of the first time
  546. *    sec1  - seconds of the first time
  547. *    hour2 - hours of the second time
  548. *    min2  - minutes of the second time
  549. *    sec2  - seconds of the second time
  550. *
  551. *   RESULT
  552. *    secs - The difference betwen time1 and time1 in seconds.
  553. *
  554. *   EXAMPLE
  555. *    ...
  556. *    secs = TimeDiff(21,15,00,22,0,0);
  557. *    ...
  558. *
  559. *   NOTES
  560. *    Don't forget to convert AM/PM time to 24h time!
  561. *    use SecToTime() to convert the seconds back to a hour,min,secs
  562. *    format!
  563. *
  564. *   BUGS
  565. *    No errorcheck, if you use a valid time
  566. *
  567. *   SEE ALSO
  568. *    SecToTime(),TimeToSec()
  569. *
  570. *****************************************************************************
  571. *
  572. *
  573. */
  574.   {
  575.    return((long)(TimeToSec(hour1,min1,sec1)-TimeToSec(hour2,min2,sec2)));
  576.   }
  577.  
  578.  /* ----------------------------------------------------------------------- */
  579.  
  580.  #ifndef __MakeLib
  581.    #ifndef __cplusplus
  582.      void DiffTime(const unsigned short hour, const unsigned short min, const unsigned short sec, long diffsecs, unsigned short *const rhour, unsigned short *const rmin, unsigned short *const rsec)
  583.    #else
  584.      void DiffTime(const unsigned short hour, const unsigned short min, const unsigned short sec, long diffsecs, unsigned short &rhour, unsigned short &rmin, unsigned short &rsec)
  585.    #endif
  586.  #else
  587.    void __saveds __asm DiffTime(register __d0 const unsigned short hour, register __d1 const unsigned short min, register __d2 const unsigned short sec, register __d3 long diffsecs, register __a0 unsigned short *const rhour, register __a1 unsigned short *const rmin, register __a2 unsigned short *const rsec)
  588.  #endif
  589.  
  590.  
  591. /*
  592. ******* Date/DiffTime *******************************************************
  593. *
  594. *   NAME
  595. *    DiffTime -- Returns the diff. time to another time. (V33)
  596. *
  597. *   SYNOPSIS
  598. *    DiffTime(hour,min,sec,secs,rhour,rmin,rsec);
  599. *          d0  d1  d2  d3   a0    a1   a2
  600. *
  601. *    void DiffTime(const unsigned short hour, const unsigned short min,
  602. *        const unsigned short sec, long secs, unsigned short *const rhour,
  603. *        unsigned short *const rmin, unsigned short *const rsec);
  604. *
  605. *    void DiffTime(const unsigned short hour, const unsigned short min,
  606. *        const unsigned short sec, long secs, unsigned short &rhour,
  607. *        unsigned short &rmin, unsigned short &rsec);
  608. *
  609. *   FUNCTION
  610. *    Returns the time which lies diffsecs before/after the specified time.
  611. *
  612. *   INPUTS
  613. *    hour     - hour
  614. *    min      - minute
  615. *    sec      - second
  616. *    diffsecs - difference to the time in seconds
  617. *
  618. *   RESULT
  619. *    rhour - new hour
  620. *    rmin  - new minute
  621. *    rsec  - new second
  622. *
  623. *   EXAMPLE
  624. *    ...
  625. *    DiffTime(12,19,0,2460,&hour,&min,&sec);
  626. *    ...
  627. *
  628. *   NOTES
  629. *    Don't forget to convert AM/PM to 24h time!
  630. *    Don't forget to convert 24h time to AM/PM time if needed!
  631. *
  632. *   BUGS
  633. *    No errorcheck, if you use a valid time
  634. *
  635. *   SEE ALSO
  636. *    TimeToSec(),SecToTime()
  637. *
  638. *****************************************************************************
  639. *
  640. *
  641. */
  642.  
  643.   {
  644.    long secs;
  645.  
  646.    secs = (long)TimeToSec(hour,min,sec);
  647.    secs += diffsecs;
  648.    if (secs < 0)
  649.     {
  650.      secs += 86400L;
  651.     }
  652.    if (secs > 86400L)
  653.     {
  654.      secs -= 86400L;
  655.     }
  656.    SecToTime((unsigned long)secs,rhour,rmin,rsec);
  657.   }
  658.  
  659.  /* ----------------------------------------------------------------------- */
  660.