1 //-------------------------------------------------------------------------
\r
3 // This file contains some DataFlex 3.2 Console Mode functions
\r
4 // to provide extended date manipulation capabilities.
\r
6 // This file is to be included in df32func.mk
\r
8 // Copyright (c) 2006-2015, glyn@8kb.co.uk
\r
10 // df32func/date.inc
\r
11 //-------------------------------------------------------------------------
\r
13 //-------------------------------------------------------------------------
\r
15 //-------------------------------------------------------------------------
\r
17 // Returns day of the week as an integer score 1 = monday 7 = sunday
\r
18 function get_day_score global date argv returns integer
\r
19 local integer l_01tmpInt
\r
21 calc (mod(argv,7)) to l_01tmpInt
\r
22 if (l_01tmpInt < 0) calc (l_01tmpInt+7) to l_01tmpInt
\r
25 case (l_01tmpInt = 3) move 1 to l_01tmpInt
\r
27 case (l_01tmpInt = 4) move 2 to l_01tmpInt
\r
29 case (l_01tmpInt = 5) move 3 to l_01tmpInt
\r
31 case (l_01tmpInt = 6) move 4 to l_01tmpInt
\r
33 case (l_01tmpInt = 0) move 5 to l_01tmpInt
\r
35 case (l_01tmpInt = 1) move 6 to l_01tmpInt
\r
37 case (l_01tmpInt = 2) move 7 to l_01tmpInt
\r
41 function_return l_01tmpInt
\r
44 // Returns the current day as a string from the date
\r
45 function get_day global date argv returns string
\r
46 local integer l_iDay
\r
49 move (get_day_score(argv)) to l_iDay
\r
52 case (l_iDay = 1) move "Monday" to l_sDay
\r
54 case (l_iDay = 2) move "Tuesday" to l_sDay
\r
56 case (l_iDay = 3) move "Wednesday" to l_sDay
\r
58 case (l_iDay = 4) move "Thursday" to l_sDay
\r
60 case (l_iDay = 5) move "Friday" to l_sDay
\r
62 case (l_iDay = 6) move "Saturday" to l_sDay
\r
64 case (l_iDay = 7) move "Sunday" to l_sDay
\r
68 function_return l_sDay
\r
71 // Returns the current month as a string from the date
\r
72 function get_month global date argv returns string
\r
73 local integer l_iMonth
\r
74 local string l_sMonth
\r
76 calc ((integer(mid(argv, 2, 4)))-1) to l_iMonth
\r
78 case (l_iMonth = 0) move "January" to l_sMonth
\r
79 case (l_iMonth = 1) move "February" to l_sMonth
\r
80 case (l_iMonth = 2) move "March" to l_sMonth
\r
81 case (l_iMonth = 3) move "April" to l_sMonth
\r
82 case (l_iMonth = 4) move "May" to l_sMonth
\r
83 case (l_iMonth = 5) move "June" to l_sMonth
\r
84 case (l_iMonth = 6) move "July" to l_sMonth
\r
85 case (l_iMonth = 7) move "August" to l_sMonth
\r
86 case (l_iMonth = 8) move "September" to l_sMonth
\r
87 case (l_iMonth = 9) move "October" to l_sMonth
\r
88 case (l_iMonth = 10) move "November" to l_sMonth
\r
89 case (l_iMonth = 11) move "December" to l_sMonth
\r
92 function_return l_sMonth
\r
95 // Returns the current day of the month
\r
96 function get_day_of global date argv returns string
\r
97 local string l_01tmpStr
\r
98 local integer l_01tmpInt
\r
100 move (mid(argv, 2, 1)) to l_01tmpInt
\r
101 move "th" to l_01tmpStr
\r
102 if (mid(argv, 1, 1)) ne 1 begin
\r
103 if (mid(argv, 1, 2)) eq 1 move "st" to l_01tmpStr
\r
104 if (mid(argv, 1, 2)) eq 2 move "nd" to l_01tmpStr
\r
105 if (mid(argv, 1, 2)) eq 3 move "rd" to l_01tmpStr
\r
108 function_return l_01tmpStr
\r
111 // Returns a string representing the full date e.g. "Friday 31st July 2009"
\r
112 function fulldate global date argv returns string
\r
113 local string l_01tmpStr l_02tmpStr
\r
114 local integer l_01tmpInt l_01arrayindex
\r
116 move (get_day(argv)) to l_02tmpStr
\r
117 move (mid(argv, 2, 1)) to l_01tmpInt
\r
118 move "th" to l_01tmpStr
\r
119 if (mid(argv, 1, 1)) ne 1 begin
\r
120 if (mid(argv, 1, 2)) eq 1 move "st" to l_01tmpStr
\r
121 if (mid(argv, 1, 2)) eq 2 move "nd" to l_01tmpStr
\r
122 if (mid(argv, 1, 2)) eq 3 move "rd" to l_01tmpStr
\r
124 append l_02tmpStr " " l_01tmpInt (trim(l_01tmpStr)) " "
\r
125 move (get_month(argv)) to l_01tmpStr
\r
126 append l_02tmpStr (trim(l_01tmpStr)) " " (trim(mid(argv, 4, 7)))
\r
128 function_return l_02tmpStr
\r
131 // Returns a string representing the date in short form e.g. "31 July 2009"
\r
132 function shortdate global date argv returns string
\r
133 local string l_01tmpStr
\r
135 move "" to l_01tmpStr
\r
136 append l_01tmpStr (mid(argv,2,1)) " " (get_month(argv)) " " (mid(argv,4,7))
\r
138 function_return l_01tmpStr
\r
141 // Returns the current time and or date in different formats
\r
142 function now global string argv returns string
\r
143 local string l_01tmpStr l_02tmpStr
\r
144 local date l_01nowDate
\r
145 local number l_01nowTime l_02nowTime l_03nowTime l_01nowSecs
\r
146 sysdate l_01nowDate
\r
147 calc ((number(strlen))+((number(strmark))/100)) to l_01nowTime
\r
148 move "" to l_01tmpStr
\r
149 if ((trim(lowercase(argv))) = "time") append l_01tmpStr (string(decround(l_01nowTime,2)))
\r
150 if (((trim(lowercase(argv))) = "longtime") or ((trim(lowercase(argv))) = "timestamp")) begin
\r
151 sysdate l_01nowDate l_01nowTime l_02nowTime l_03nowTime
\r
152 move "" to l_01tmpStr
\r
153 if ((trim(lowercase(argv))) = "timestamp") append l_01tmpStr l_01nowDate " "
\r
154 if (l_01nowTime < 10) append l_01tmpStr 0
\r
155 append l_01tmpStr l_01nowTime ":"
\r
156 if (l_02nowTime < 10) append l_01tmpStr 0
\r
157 append l_01tmpStr l_02nowTime ":"
\r
158 if (l_03nowTime < 10) append l_01tmpStr 0
\r
159 append l_01tmpStr l_03nowTime
\r
161 if ((trim(lowercase(argv))) = "seconds") begin
\r
162 sysdate l_01nowDate l_01nowTime l_02nowTime l_03nowTime
\r
163 move (string((l_01nowTime*3600)+(l_02nowTime*60)+l_03nowTime)) to l_01tmpStr
\r
165 if ((trim(lowercase(argv))) = "date") append l_01tmpStr l_01nowDate
\r
166 if ((trim(lowercase(argv))) = "shorttimestamp") append l_01tmpStr l_01nowDate "," (string(decround(l_01nowTime,2)))
\r
167 if ((trim(lowercase(argv))) = "longtimestamp") begin
\r
168 append l_01tmpStr (fulldate(l_01nowDate)) " at "
\r
169 if (l_01nowTime > 12) append l_01tmpStr (string(decround(l_01nowTime-12,2))) "PM"
\r
170 if (l_01nowTime <= 12) append l_01tmpStr (string(decround(l_01nowTime,2))) "AM"
\r
173 function_return l_01tmpStr
\r
176 // Return a date and time in the RSS standard format
\r
177 function rssdate global date argv string argv2 returns string
\r
178 local string l_01tmpStr
\r
180 move "" to l_01tmpStr
\r
181 append l_01tmpStr (left((get_day(argv)),3)) ", " (mid(argv,2,1)) " " (left((get_month(argv)),3)) " " (mid(argv,4,7)) " " (trim(argv2)) " GMT"
\r
182 move (uppercase(l_01tmpStr)) to l_01tmpStr
\r
184 function_return l_01tmpStr
\r
187 // Retuns a date in the format CCYYMMDD
\r
188 function cymd global date argv returns string
\r
189 local string l_01tmpStr
\r
191 move (mid(argv,4,7)) to l_01tmpStr
\r
192 append l_01tmpStr (mid(argv,2,4)) (mid(argv,2,1))
\r
194 function_return l_01tmpStr
\r
197 // Retuns a date in the format CCYY-MM-DD
\r
198 function sqldate global date argv returns string
\r
199 local string l_01tmpStr
\r
200 move "" to l_01tmpStr
\r
202 if (argv <> "") begin
\r
203 move (mid(argv,4,7)) to l_01tmpStr
\r
204 append l_01tmpStr "-" (mid(argv,2,4)) "-" (mid(argv,2,1))
\r
206 function_return l_01tmpStr
\r
209 // Retuns a timestamp in the format CCYY-MM-DD HH:mm:SS
\r
210 function sqltimestamp global date argv integer argv2 integer argv3 integer argv4 returns string
\r
211 local string l_01tmpStr
\r
212 move "" to l_01tmpStr
\r
214 if ((argv <> "") and ((argv2 >= 0) and (argv2 <= 23)) and ((argv3 >= 0) and (argv3 <= 59)) and ((argv4 >= 0) and (argv4 <= 59))) begin
\r
215 move (mid(argv,4,7)) to l_01tmpStr
\r
216 append l_01tmpStr "-" (mid(argv,2,4)) "-" (mid(argv,2,1)) " " (zeropad(argv2,2)) ":" (zeropad(argv3,2)) ":" (zeropad(argv4,2))
\r
219 function_return l_01tmpStr
\r
222 // Retuns a timestamp in the format DD/MM/CCYY HH:mm:SS
\r
223 function dftimestamp global date argv integer argv2 integer argv3 integer argv4 returns string
\r
224 local string l_01tmpStr
\r
225 move "" to l_01tmpStr
\r
227 if ((argv <> "") and ((argv2 >= 0) and (argv2 <= 23)) and ((argv3 >= 0) and (argv3 <= 59)) and ((argv4 >= 0) and (argv4 <= 59))) begin
\r
228 move "" to l_01tmpStr
\r
229 append l_01tmpStr argv " " (zeropad(argv2,2)) ":" (zeropad(argv3,2)) ":" (zeropad(argv4,2))
\r
232 function_return l_01tmpStr
\r
235 // Returns date or time as a string out of a dateTime struct
\r
236 function sqldatetime global string argv string argv2 returns string
\r
237 local string l_sResult
\r
239 move (trim(argv)) to argv
\r
240 move (trim(uppercase(argv2))) to argv2
\r
242 if (argv2 = "DATE") begin
\r
243 move (mid(argv,2,9)) to l_sResult
\r
244 append l_sResult "/" (mid(argv,2,6)) "/" (mid(argv,4,1))
\r
246 if (argv2 = "TIME") begin
\r
247 move (right(argv,12)) to l_sresult
\r
250 function_return l_sResult
\r
253 // Returns date or timestamp as a string out of a dateTime struct
\r
254 function sqltimestampformat global string argv string argv2 returns string
\r
255 local string l_sResult
\r
257 move (trim(argv)) to argv
\r
258 move (trim(uppercase(argv2))) to argv2
\r
260 if (argv2 = "DATE") begin
\r
261 move (mid(argv,2,9)) to l_sResult
\r
262 append l_sResult "/" (mid(argv,2,6)) "/" (mid(argv,4,1))
\r
264 if (argv2 = "TIME") begin
\r
265 move (right(argv,length(argv)-pos(" ", argv))) to l_sresult
\r
268 function_return l_sResult
\r
271 // Returns last day of month as a date
\r
272 function last_day_of_month global integer month integer year returns date
\r
274 if (month = 12) begin
\r
275 calc (year+1) to year
\r
279 calc (month+1) to month
\r
282 move (('01/'+string(month)+'/'+string(year))-1) to l_dDate
\r
284 function_return l_dDate
\r
287 // Returns a posix timestamp when supplied with a date and numeric values
\r
288 // for hour, minutes ,seconds and milliseconds: (date,hr,min,sec,millisec)
\r
290 // Also see timestamp_to_posix and posix_to_timestamp in timestamp.inc
\r
291 function posixtime global date argv number argv2 number argv3 number argv4 number argv5 returns number
\r
292 local number l_posix
\r
294 // Leap seconds not coded
\r
295 calc (((integer(argv))-(integer(date("01/01/1970"))))*86400) to l_posix
\r
296 calc (((((argv2*60)+argv3)*60)+argv4)+(argv5/1000)+l_posix) to l_posix
\r
298 function_return l_posix
\r
301 // Returns a comma separated string representing a timestamp (date,hr,min,sec,millisec)
\r
302 // when supplied with a posix time value.
\r
303 function posixtime_reverse global number argv returns string
\r
305 local number l_subt
\r
306 local integer l_hr l_min l_sec l_msec
\r
307 local string l_posix_reverse
\r
309 // Leap seconds not coded
\r
310 calc ((argv/86400)+(integer(date("01/01/1970")))) to l_date
\r
311 calc (argv-(((integer(l_date))-(integer(date("01/01/1970"))))*86400)) to l_subt
\r
312 calc (l_subt/3600) to l_hr
\r
313 calc ((l_subt-(l_hr*3600))/60) to l_min
\r
314 calc (l_subt-(((l_hr*60)+l_min)*60)) to l_sec
\r
315 calc ((l_subt-((((l_hr*60)+l_min)*60)+l_sec))*1000) to l_msec
\r
317 move l_date to l_posix_reverse
\r
318 append l_posix_reverse "," l_hr "," l_min "," l_sec "," l_msec
\r
320 function_return l_posix_reverse
\r
323 // Returns 0 until the timeout in seconds value passed is exceeded
\r
324 // when it returns 1.
\r
326 // Relies on the value of global integer "g_nRuntime_entry";
\r
327 // the only way to use more than once is to reset that global to 0
\r
328 // by calling runtime_timeout_reset.
\r
329 function runtime_timeout global integer argv returns integer
\r
330 local integer l_iTimeout
\r
331 local number l_nHour l_nMinute l_nSec l_nRuntime_now
\r
332 local date l_dToday
\r
334 sysdate l_dToday l_nHour l_nMinute l_nSec
\r
335 move 0 to l_iTimeout
\r
337 if (argv <> 0) begin
\r
338 if (g_nRuntime_entry = 0) begin
\r
339 move (posixTime(l_dToday,l_nHour,l_nMinute,l_nSec,0)) to g_nRuntime_entry
\r
342 move (posixTime(l_dToday,l_nHour,l_nMinute,l_nSec,0)) to l_nRuntime_now
\r
344 if ((l_nRuntime_now - g_nRuntime_entry) > argv) begin
\r
345 move 1 to l_iTimeout
\r
349 function_return l_iTimeout
\r
352 // Resets the value of global integer "g_nRuntime_entry" as used by
\r
354 function runtime_timeout_reset global returns integer
\r
355 move 0 to g_nRuntime_entry
\r
359 // Returns the time left in seconsd until runtime_timeout is exceeded.
\r
360 function runtime_timeout_left global returns integer
\r
361 local number l_nHour l_nMinute l_nSec l_nRuntime_now
\r
362 local date l_dToday
\r
364 sysdate l_dToday l_nHour l_nMinute l_nSec
\r
365 move (posixTime(l_dToday,l_nHour,l_nMinute,l_nSec,0)) to l_nRuntime_now
\r
367 function_return (l_nRuntime_now - g_nRuntime_entry)
\r