1. EuGTK Days Between Dates
- Posted by Senator Jul 20, 2018
- 2803 views
Irv Mullins has released EuGTK 4.14.0
-- -- 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/ -- include std/datetime.e include std/convert.e include GtkEngine.e datetime dt1, dt2 object sd, ed atom yyyy,mm,dd sequence wdn1, wdn2 -- week day name constant instructions = { "Calculate the number of days between dates", """ Enter numeric values for starting date, and ending date in the format yyyymmdd 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,size=300x50,border=10,$destroy=Quit"), pan = create(GtkBox,"orientation=VERTICAL,spacing=10"), StartDate = create(GtkEntry,"placeholder text= Start Date yyyymmdd e.g. 20180628"), EndDate = create(GtkEntry,"placeholder text= EndDate yyyymmdd e.g. 20180807"), 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,{StartDate, 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 week_day_name(datetime dt) -- return {day_names[weeks_day(dt)]} -- end function -- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- -------------------------------- global function Help() -- -------------------------------- return Info(win,"instructions",instructions[1],instructions[2]) end function ---------------------------------------------------------- global function GetDaysBetweenDates() -- ----------------------------------------------------------- atom dbd sd = get(StartDate,"text") sd = to_string(sd) ed= get(EndDate,"text") ed = to_string(ed) yyyy = to_number(sd[1..4]) mm = to_number(sd[5..6]) dd = to_number(sd[7..8]) dt1 = {yyyy,mm,dd,0,0,0} wdn1 = week_day_name(dt1) yyyy = to_number(ed[1..4]) mm = to_number(ed[5..6]) dd = to_number(ed[7..8]) dt2 = {yyyy,mm,dd, 0,0,0} wdn2 = week_day_name(dt2) dbd = DaysBetweenDates(dt1,dt2) Info(win, "Days Between Dates", text:format("There are [] days between [] [] and [] []", {dbd, wdn1, sd, wdn2, ed})) return 1 end function
2. Re: EuGTK Days Between Dates
- Posted by irv Jul 20, 2018
- 2759 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
3. Re: EuGTK Days Between Dates
- Posted by Senator Jul 23, 2018
- 2670 views
Absolutely Wonderful Irv!
All that extra functionality with so little extra code.
I think I'm going to change my forum user name to Tom_Sawyer.
Regards, Ken Rhodes
4. Re: EuGTK Days Between Dates
- Posted by Senator Jul 24, 2018
- 2642 views
-- The custom format display is the same as the default format -- on my system dd1 = get(cal1,"date",0), -- default format for display; -- Tuesday, 24 Jul, 2018 -- display dd2 = get(cal2,"date","%A, %B %d,\nwhich is day %j of the year %Y") -- Thursday, 18 Oct, 2018 -- display -- infobox display also reports the default format -- for dd2: -- "There are 86 days" -- "between Tuesday, 24 Jul, 2018" -- "and Thursday, 18 Oct, 2018"
5. Re: EuGTK Days Between Dates
- Posted by irv Jul 24, 2018
- 2648 views
I've updated the parsing function to be more versatile when typing in dates.
Date formats include: m/d/yyyy, m/d/yy, yyyy/m/d, etc. Others may work too, such as Jan 4, 2000 or 15 january, 1926, or 4-2-65, or Thurs Dec 12, 42.
---------------------------------------------- global function Parse(atom ctl, object cal) -- ---------------------------------------------- object date_in = get(ctl,"text") -- save for error msg; boolean twodigityear = TRUE object x = split_any(date_in," ,/") x = pad_tail(x,3,"") for i = 1 to 12 do for j = 1 to 3 do if length(x[j]) > 2 and to_number(x[j]) > 0 then twodigityear = FALSE end if if match(month_abbrs[i],proper(x[j])) = 1 then x[j] = sprintf("%d",i) end if end for end for x = join(x,"-") cal = unpack(cal) -- get target calendar; object dt = datetime:parse(x,"%m %d %Y") if atom(dt) then dt = datetime:parse(x,"%d %m %Y") end if if atom(dt) then dt = datetime:parse(x,"%Y %m %d") end if if atom(dt) then set(ctl,"text",date_in & " <= ERROR") -- show orig input; set(ctl,"color=red,overwrite mode=TRUE") set(ctl,"select region",1,-1) return -1 else if twodigityear then dt[1] += 1900 end if set(ctl,"color=black,overwrite mode=FALSE") set(cal,"date",dt) set(ctl,"text",datetime:format(dt,"%m/%d/%Y")) end if return 1 end function
6. Re: EuGTK Days Between Dates
- Posted by Senator Jul 25, 2018
- 2593 views
Thanks to Irv for the updated parse function.
Regarding my problem with the custom display format...
dd1 = get(cal1,"date",0), -- default format for display; -- Displays Thursday, 18 Oct, 2018 -- custom format for display dd2 = get(cal2,"date","%A, %B %d,\nwhich is day %j of the year %Y") -- Displays Thursday, 18 Oct, 2018 --same as default format
While exploring one of the new demo programs in your latest
EuGTK 4.14.0 release I encountered an error message advising
me that the code requires GTK3 3.20 and that I have 3.18.9
installed on my Ubuntu 16.04 LTS computer.
So, I speculated that the custom display format might require
GTK3 3.20 and that might account for the display of the default
format as opposed to the custom display format. Perhaps I should
upgrade to the latest Ubuntu release.
Does the custom date display option require GTK3 3.20?
Regards,
Ken Rhodes
7. Re: EuGTK Days Between Dates
- Posted by irv Jul 25, 2018
- 2598 views
No, the custom date doesn't require the latest GTK version, but that is one of the small bug fixes I have made to EuGTK 4.14.1. I was planning to post that on Aug 1, but might do it sooner if I can find no more insects.
Of course, you can always get(cal,"datetime") and then use datetime:format.
EuGTK 4.14.1 also adds a few programs from the RDS archives, which I updated to use EuGTK and Eu 4.1, to make them easier to use.