Days Between Dates Revisited
- Posted by Senator Feb 08, 2018
- 1098 views
Once again, just for fun.... Junko C. Miura's Days Between Dates code revised for Euphoria 4.0x using the std/datetime.e library.
-- -- daybdate.ex -- include get.e include std/datetime.e constant TRUE = 1 constant FALSE = 0 constant KEYBOARD = 0 constant SCREEN = 1 sequence s integer finished, ok, yyyy, mm, dd datetime dt1, dt2 ----------------------------------------------------------------------- -- You might want to add these functions to std/datetime.e -- ----------------------------------------------------------------------- function DaysBetweenDates(datetime dt1, datetime dt2) return floor(diff(dt1,dt2)/86400) -- 86400 = seconds in a day end function function week_day_name(datetime dt) return {day_names[weeks_day(dt)]} end function --------------------------------------------------------------------------- ---------------------------------------------------------------------------- function get_datetime() s = {GET_SUCCESS, FALSE} ok = FALSE while s[1] != GET_EOF and not ok do -- check of GET_EOF CAN be removed puts(SCREEN, "enter year (YYYY)\n") s = get(KEYBOARD) if s[1] = GET_FAIL then puts(SCREEN, "syntactically incorrect input found, try again\n") elsif not integer(s[2]) then puts(SCREEN, "year must be 4-digit integer, try again\n") elsif s[2] > 1582 then yyyy = s[2] ok = TRUE else puts(SCREEN, "year must be >1582. ") end if end while s = {GET_SUCCESS, FALSE} ok = FALSE while s[1] != GET_EOF and not ok do -- check of GET_EOF CAN be removed puts(SCREEN, "enter month (MM)\n") s = get(KEYBOARD) if s[1] = GET_FAIL then puts(SCREEN, "syntactically incorrect input found, try again\n") elsif not integer(s[2]) then puts(SCREEN, "month must be integer, try again\n") elsif s[2] > 0 and s[2] <= 12 then mm = s[2] ok = TRUE else puts(SCREEN, "month must be >0 and <=12. ") end if end while s = {GET_SUCCESS, FALSE} ok = FALSE while s[1] != GET_EOF and not ok do -- check of GET_EOF CAN be removed puts(SCREEN, "enter day (DD)\n") s = get(KEYBOARD) if s[1] = GET_FAIL then puts(SCREEN, "syntactically incorrect input found, try again\n") elsif not integer(s[2]) then puts(SCREEN, "day must be integer, try again\n") elsif s[2] > 0 and s[2] <= 31 then dd = s[2] ok = TRUE else puts(SCREEN, "day must be >0 and <=31. ") end if end while return {yyyy, mm, dd, 0,0,0} -- padded tail for type datetime end function --- main program execution begins here: finished = FALSE s = {GET_SUCCESS, FALSE} while s[1] != GET_EOF and not finished do puts(SCREEN, "Enter which to perform, 1 for day of the week, or\n" & " 2 for days between dates, or\n" & " 3 for quit\n") s = get(KEYBOARD) if s[1] = GET_FAIL then puts(SCREEN, "syntactically incorrect input found, try again\n") else if s[2] = 1 then dt1 = get_datetime() printf(SCREEN, "Day Of The Week For %4d/%d/%d : ", dt1) printf(SCREEN, "%s\n", week_day_name(dt1)) elsif s[2] = 2 then dt1 = get_datetime() dt2 = get_datetime() printf(SCREEN, "The Number Of Days Between %d/%d/%d And %4d/%d/%d ", dt1[1..3]&dt2[1..3]) printf(SCREEN, "Is %d\n", DaysBetweenDates(dt1,dt2)) elsif s[2] = 3 then finished = TRUE end if end if end while