Re: EuGTK Days Between Dates
- Posted by irv Jul 20, 2018
- 2752 views
Nice! Here's a different take on it, using calendars to click and date parsing, so various formats can be typed in:
-- -- GTK Days Between Dates, requires OpenEuphoria 4.x -- a simple EuGTK demo program by Kenneth Rhodes -- -- Check out Irv Mullins EuGTk 4.14.0 -- https://sites.google.com/site/euphoriagtk/Home/ -- -- Added a couple calendars for ease of use - Irv -- Dates can be typed in using various formats, -- or selected from the calendars via mouse. include std/datetime.e include GtkEngine.e constant instructions = { "Calculate the number of days between dates", """ Select start and end dates from the calendars, then click the OK button, or type a date into the box below the calendar. Date formats include: m/d/yy, m/d/yyyy, yyyy/m/d, etc. Experiment! The program displays the number of days between the starting and ending date as well as the week day names of the starting and ending dates """} constant -- INTERFACE win = create(GtkWindow,"title = Compute Days Between Dates,border=10,$destroy=Quit"), pan = create(GtkBox,"orientation=vertical,spacing=10"), cal1 = create(GtkCalendar,"name=cal1,font=8,$day-selected=Update"), StartDate = create(GtkEntry,"placeholder text=Start Date,$activate=Parse,data=cal1"), cal2 = create(GtkCalendar,"name=cal2,font=8,$day-selected=Update"), EndDate = create(GtkEntry,"placeholder text=End Date,$activate=Parse,data=cal2"), btnbox = create(GtkButtonBox), btn1 = create(GtkButton,"gtk-quit","Quit"), btn2 = create(GtkButton,"gtk-help","Help"), btn3 = create(GtkButton,"gtk-ok","GetDaysBetweenDates") gtk:add(win,pan) gtk:add(pan,{cal1, StartDate, cal2, EndDate}) pack_end(pan,btnbox) gtk:add(btnbox,{btn1,btn2,btn3}) set(btn3,"grab focus") show_all(win) main() -------------------------------------------------------------------------------- -- You might want to add these functions to std/datetime.e -- -------------------------------------------------------------------------------- global function DaysBetweenDates(datetime dt1, datetime dt2) return floor(diff(dt1,dt2)/86400) -- 86400 = seconds in a day end function -- -------------------------------------------------------------------------------- ------------------------- global function Help() -- ------------------------- return Info(win,"instructions",instructions[1],instructions[2]) end function --------------------------- global function Update() -- update date displays whenever either changes; --------------------------- set(StartDate,"text",get(cal1,"date",0)) set(EndDate,"text",get(cal2,"date",0)) return 1 end function ---------------------------------------------- global function Parse(atom ctl, object cal) -- ---------------------------------------------- object cdate = get(ctl,"text") cal = unpack(cal) -- get target calendar; object dt = datetime:parse(cdate,"%m %d %Y") if atom(dt) then dt = datetime:parse(cdate,"%d %m %Y") end if if atom(dt) then dt = datetime:parse(cdate,"%y %m %d") end if if atom(dt) then dt = datetime:parse(cdate,"%Y %m %d") end if if atom(dt) then dt = datetime:parse(cdate,"%Y %d %m") end if if atom(dt) then set(ctl,"text",cdate & " <- ERROR") set(ctl,"color","red") else if dt[1] < 1900 then dt[1] += 1900 end if set(cal,"date",dt) -- set target calendar; set(ctl,"color","black") end if return 1 end function ----------------------------------------- global function GetDaysBetweenDates() -- ----------------------------------------- Update() -- make sure dates are displayed in entry boxes; atom dbd object dt1 = get(cal1,"datetime"), -- dt format to compute with; dt2 = get(cal2,"datetime"), dd1 = get(cal1,"date",0), -- default format for display; dd2 = get(cal2,"date","%A, %B %d,\nwhich is day %j of the year %Y") -- custom fmt; dbd = DaysBetweenDates(dt1,dt2) Info(win, "Days Between Dates", text:format("There are [] days",dbd), text:format("between <b>[]</b>\nand <b>[]</b>", {dd1,dd2})) return 1 end function