home *** CD-ROM | disk | FTP | other *** search
/ Corel Draw 6 / corel-draw-6-cd1.iso / scripts / time.csc < prev    next >
Text File  |  1995-08-18  |  4KB  |  107 lines

  1. ' Convert Date and time to any format
  2. ' Time.csc August 8, 1995
  3. ' This script shows how to get different formats of
  4. ' dates and times using DLL calls
  5. '#########################################################################
  6.  
  7. ' DLL functions
  8. 'Parts of can easily be pasted into other scripts to format dates in those scripts.
  9. DECLARE FUNCTION SCRIPTDATETODOSDATE LIB "oleaut32" ( BYVAL time AS DATE, BYREF dosdate AS INTEGER, BYREF dostime AS INTEGER) AS LONG ALIAS "VariantTimeToDosDateTime"
  10. DECLARE FUNCTION DOSDATETOFILETIME LIB "kernel32" ( BYVAL dosdate AS INTEGER, BYVAL dostime AS INTEGER, BYREF FILETIME AS STRING) AS INTEGER ALIAS "DosDateTimeToFileTime"
  11. DECLARE FUNCTION FILETIMETOSYSTEMTIME LIB "kernel32" ( BYREF filetime AS STRING, BYREF systemtime AS STRING) AS INTEGER ALIAS "FileTimeToSystemTime"
  12. DECLARE FUNCTION GETDATEFORMAT LIB "kernel32" (BYVAL Locale AS LONG, BYVAL Flags AS LONG, BYREF datetime AS STRING, BYREF format AS STRING, datestr as STRING, BYVAL bufsize AS LONG ) AS LONG ALIAS "GetDateFormatA"
  13. DECLARE FUNCTION GETTIMEFORMAT LIB "kernel32" (BYVAL Locale AS LONG, BYVAL Flags AS LONG, BYREF datetime AS STRING, BYREF format AS STRING, timestr as STRING, BYVAL bufsize AS LONG ) AS LONG ALIAS "GetTimeFormatA"
  14.  
  15. ' Local functions
  16. DECLARE FUNCTION formatdate ( BYVAL datetime AS DATE, BYVAL dateformat AS STRING, BYVAL timeformat AS STRING ) AS STRING
  17.  
  18.  
  19. ' Constants and globals
  20. CONST OK=1
  21. CONST NUMTIMEFORMAT = 5
  22. CONST NUMDATEFORMAT = 11
  23. CONST NUMFORMAT = NUMTIMEFORMAT * NUMDATEFORMAT
  24.  
  25. ' Main local
  26. DIM DateFormat(NUMFORMAT) AS STRING
  27. DIM TimeFormat(NUMFORMAT) AS STRING
  28. DIM FormatList(NUMFORMAT) AS STRING
  29. DIM DateChoice AS DATE
  30.  
  31.  
  32. ' Fill date format array
  33. FOR i = 1 TO NUMTIMEFORMAT
  34.     DateFormat( i + NUMTIMEFORMAT * 0 ) = "dddd',' MMMM dd yyyy"    
  35.     DateFormat( i + NUMTIMEFORMAT * 1 ) = "ddd',' MMM d y"    
  36.     DateFormat( i + NUMTIMEFORMAT * 2 ) = "dd'/'MM'/'yy"
  37.     DateFormat( i + NUMTIMEFORMAT * 3 ) = "d'/'M'/'y"
  38.     DateFormat( i + NUMTIMEFORMAT * 4 ) = "MM'/'dd'/'yy"
  39.     DateFormat( i + NUMTIMEFORMAT * 5 ) = "M'/'d'/'y"
  40.     DateFormat( i + NUMTIMEFORMAT * 6 ) = "yyyy, MMMM dd"
  41.     DateFormat( i + NUMTIMEFORMAT * 7 ) = "yyyy'/'MMM'/'dd"
  42.     DateFormat( i + NUMTIMEFORMAT * 8 ) = "yy'/'MM'/'dd"
  43.     DateFormat( i + NUMTIMEFORMAT * 9 ) = "y'/'M'/'d"
  44.     DateFormat( i + NUMTIMEFORMAT * 10) = ""
  45. NEXT i
  46.  
  47. ' Fill time format array
  48. FOR i = 1 TO NUMFORMAT STEP NUMTIMEFORMAT
  49.     TimeFormat( i     ) = "HH':'mm':'ss"
  50.     TimeFormat( i + 1 ) = "H':'m':'s"
  51.     TimeFormat( i + 2 ) = "hh':'mm':'ss tt"
  52.     TimeFormat( i + 3 ) = "h':'m':'s t"
  53.     TimeFormat( i + 4 ) = ""
  54. NEXT i
  55.  
  56. ' Fill dialog list
  57. FOR i = 1 TO NUMFORMAT
  58.     FormatList(i) = formatdate( CURRDATE, DateFormat(i), TimeFormat(i) )    
  59. NEXT i
  60.  
  61. ' Initialize dialog
  62. UserDatetime$= CURRDATE
  63. FormatListValue%=1
  64.  
  65. ' Definition of our format selection dialog
  66. BEGIN DIALOG datetimedlg 257, 172, "Date and Time conversions"
  67.     OKBUTTON  193, 14, 56, 16
  68.     CANCELBUTTON  193, 37, 56, 16
  69.     TEXT  8, 87, 101, 9, "Desired Format:"
  70.     LISTBOX  8, 97, 175, 80, FormatList$,FormatListValue%
  71.     GROUPBOX  8, 7, 175, 71, "Date and Time"
  72.     OPTIONGROUP TypeofDate%
  73.         OPTIONBUTTON  18, 23, 89, 10, "Current"
  74.         OPTIONBUTTON  18, 40, 58, 10, "User defined"
  75.     TEXTBOX  32, 53, 141, 16, UserDatetime$
  76. END DIALOG
  77.  
  78. ' Only convert if we pressed OK
  79. IF DIALOG(datetimedlg)=OK THEN
  80.     IF TypeofDate=0 THEN
  81.         DateChoice = CURRDATE
  82.     ELSE
  83.         DateChoice = UserDatetime
  84.     END IF
  85.     messagebox formatdate( DateChoice, DateFormat(FormatListValue), TimeFormat(FormatListValue) ), "Selected Date and Time"
  86. END IF
  87.  
  88.  
  89. '#########################################################################
  90. '# Format DATE var to specied date and time format
  91. FUNCTION formatdate( datetime, dateformat, timeformat )
  92.     formatteddate$=space(255)
  93.     formattedtime$=space(255)
  94.     filetime$ = space(20)
  95.     systemtime$ = space(20)
  96.     SCRIPTDATETODOSDATE datetime, dosdate%, dostime% 
  97.     DOSDATETOFILETIME dosdate%, dostime%, filetime$
  98.     FILETIMETOSYSTEMTIME filetime$, systemtime$
  99.     GETDATEFORMAT 0, 0, systemtime$, dateformat, formatteddate$, 254
  100.     GETTIMEFORMAT 0, 0, systemtime$, timeformat$, formattedtime$, 254
  101.     IF formatteddate$<>"" then
  102.         formatdate = formatteddate$+" "+formattedtime$    
  103.     ELSE
  104.         formatdate = formattedtime$
  105.     END IF
  106. END FUNCTION
  107.