home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / TIME.PR_ / TIME.PR
Text File  |  1995-06-20  |  2KB  |  93 lines

  1. /***
  2. *
  3. *  Time.prg
  4. *
  5. *  Sample user-defined functions for manipulating time strings
  6. *
  7. *  Copyright (c) 1993-1995, Computer Associates International Inc.
  8. *  All rights reserved.
  9. *
  10. *  NOTE: Compile with /a /m /n /w
  11. *
  12. */
  13.  
  14.  
  15. /***
  16. *
  17. *  SecondsAsDays( <nSeconds> ) --> nDays
  18. *
  19. *  Convert numeric seconds to days
  20. *
  21. *  NOTE: Same as DAYS() in Examplep.prg
  22. */
  23. FUNCTION SecondsAsDays( nSeconds )
  24.    RETURN INT(nSeconds / 86400)
  25.  
  26.  
  27.  
  28. /***
  29. *  TimeAsAMPM( <cTime> ) --> cTime
  30. *  Convert a time string to 12-hour format
  31. *
  32. *  NOTE:  Same as AMPM() in Examplep.prg
  33. */
  34. FUNCTION TimeAsAMPM( cTime )
  35.  
  36.    IF VAL(cTime) < 12
  37.       cTime += " am"
  38.    ELSEIF VAL(cTime) = 12
  39.       cTime += " pm"
  40.    ELSE
  41.       cTime := STR(VAL(cTime) - 12, 2) + SUBSTR(cTime, 3) + " pm"
  42.    ENDIF
  43.  
  44.    RETURN cTime
  45.  
  46.  
  47.  
  48. /***
  49. *  TimeAsSeconds( <cTime> ) --> nSeconds
  50. *  Convert a time string to number of seconds from midnight
  51. *
  52. *  NOTE: Same as SECS() in Examplep.prg
  53. */
  54. FUNCTION TimeAsSeconds( cTime )
  55.    RETURN VAL(cTime) * 3600 + VAL(SUBSTR(cTime, 4)) * 60 +;
  56.           VAL(SUBSTR(cTime, 7))
  57.  
  58.  
  59.  
  60. /***
  61. *  TimeAsString( <nSeconds> ) --> cTime
  62. *  Convert numeric seconds to a time string
  63. *
  64. *  NOTE: Same as TSTRING() in Examplep.prg
  65. */
  66. FUNCTION TimeAsString( nSeconds )
  67.    RETURN StrZero(INT(Mod(nSeconds / 3600, 24)), 2, 0) + ":" +;
  68.           StrZero(INT(Mod(nSeconds / 60, 60)), 2, 0) + ":" +;
  69.           StrZero(INT(Mod(nSeconds, 60)), 2, 0)
  70.  
  71.  
  72.  
  73. /***
  74. *  TimeDiff( <cStartTime>, <cEndTime> ) --> cDiffTime
  75. *  Return the difference between two time strings in the form hh:mm:ss
  76. *
  77. *  NOTE: Same as ELAPTIME() in Examplep.prg
  78. */
  79. FUNCTION TimeDiff( cStartTime, cEndTime )
  80.    RETURN TimeAsString(IF(cEndTime < cStartTime, 86400 , 0) +;
  81.           TimeAsSeconds(cEndTime) - TimeAsSeconds(cStartTime))
  82.  
  83.  
  84.  
  85. /***
  86. *  TimeIsValid( <cTime> ) --> lValid
  87. *  Validate a time string
  88. *
  89. */
  90. FUNCTION TimeIsValid( cTime )
  91.    RETURN VAL(cTime) < 24 .AND. VAL(SUBSTR(cTime, 4)) < 60 .AND.;
  92.           VAL(SUBSTR(cTime, 7)) < 60
  93.