# # # # date_time utilities # # # get_is_leap_year() # get_year() # get_month() # get_month_as_number() # get_month_as_string() # get_day() # get_hour() # get_minute() # get_second() # get_number_of_days() # get_number_of_days_in_month(); # get_date_time_granularity() # get_weekday() # get_is_date_time_in_epoc(); # # # # # # get_is_leap_year() # # # subroutine(get_is_leap_year(int the_year), ( bool is_leap_year; if (the_year % 4 != 0) then ( is_leap_year = false; ) else if (the_year % 400 == 0) then ( is_leap_year = true; ) else if (the_year % 100 == 0) then ( is_leap_year = false; ) else ( is_leap_year = true; ); is_leap_year; )); # # # # get_year() # # # subroutine(get_year(string the_date), ( int the_year = substr(the_date, 7, 4); the_year; )); # # # # get_month() # # # # Returns the month string from a date string subroutine(get_month(string the_date), ( string the_month = substr(the_date, 3, 3); the_month; )); # # # # get_month_as_number() # # # # Returns the month as number 1-12 from string s # Note, the_date argument s may be an entire Salang date or just the month (01/Jan/2005 ..., Jan) subroutine(get_month_as_number(string s), ( s = lowercase(s); string m; int n = 0; if (matches_regular_expression(s, "([a-z]{3,3})")) then ( m = $1; if (m eq "jan") then (n = 1;) else if (m eq "feb") then (n = 2;); else if (m eq "mar") then (n = 3;); else if (m eq "apr") then (n = 4;); else if (m eq "may") then (n = 5;); else if (m eq "jun") then (n = 6;); else if (m eq "jul") then (n = 7;); else if (m eq "aug") then (n = 8;); else if (m eq "sep") then (n = 9;); else if (m eq "oct") then (n = 10;); else if (m eq "nov") then (n = 11;); else if (m eq "dec") then (n = 12;); else (n = 0;); ); n; )); # # # # get_month_as_string() # # # # Returns the month as string (Jan, Feb, ...) from of a month number such 1, 2, ... subroutine(get_month_as_string(int n), ( string m; if (n == 1) then (m = "Jan";) else if (n == 2) then (m = "Feb";) else if (n == 3) then (m = "Mar";) else if (n == 4) then (m = "Apr";) else if (n == 5) then (m = "May";) else if (n == 6) then (m = "Jun";) else if (n == 7) then (m = "Jul";) else if (n == 8) then (m = "Aug";) else if (n == 9) then (m = "Sep";) else if (n == 10) then (m = "Oct";) else if (n == 11) then (m = "Nov";) else (m = "Dec";); m; )); # # # # get_day() # # # subroutine(get_day(string the_date), ( int the_day = substr(the_date, 0, 2); the_day; )); # # # # get_hour() # # # subroutine(get_hour(string the_date), ( int the_hour = substr(the_date, 12, 2); the_hour; )); # # # # get_minute() # # # subroutine(get_minute(string the_date), ( int the_minute = substr(the_date, 15, 2); the_minute; )); # # # # get_second() # # # subroutine(get_second(string the_date), ( int the_second = substr(the_date, 18, 2); the_second; )); # # # # get_number_of_days() # # # # Returns the number of days between a given start date and end date subroutine(get_number_of_days(string start_date, string end_date), ( int start_date_in_epoc = date_time_to_epoc(start_date); int end_date_in_epoc = date_time_to_epoc(end_date); int number_of_days = ((end_date_in_epoc - start_date_in_epoc) / (24*60*60)) + 1; number_of_days; )); # # # # get_number_of_days_in_month() # # # # Returns the number of days for the given month/year subroutine(get_number_of_days_in_month(int the_year, int the_month), ( int number_of_days; if (the_month == 2) then ( number_of_days = if (!get_is_leap_year(the_year)) then (28) else (29); ) else if (the_month == 4 or the_month == 6 or the_month == 9 or the_month == 11) then ( number_of_days = 30; ) else ( number_of_days = 31; ); number_of_days; )); # # # # get_date_time_granularity() # # # subroutine(get_date_time_granularity(string sample_date), ( string date_time_granularity; if (starts_with(sample_date, "__/___/")) then ( date_time_granularity = "year" ) else if (starts_with(sample_date, "__/")) then ( date_time_granularity = "month" ) else if (ends_with(sample_date, "__:__:__")) then ( date_time_granularity = "day" ) else if (ends_with(sample_date, ":__:__")) then ( date_time_granularity = "hour" ) else if (ends_with(sample_date, ":__")) then ( date_time_granularity = "minute" ) else ( date_time_granularity = "second" ); date_time_granularity; )); # # # # get_weekday() # # # # by Zeller's algorithmn # returns the weekday for a given date in integer format (1-7) where # 1 = Sunday # 2 = Monday # 3 = Tuesday # etc. # The month argument is 1-12, where 1 = Jan, 2 = Feb, etc. subroutine(get_weekday( int year, int month, int day), ( volatile.temp_month.1 = 11; volatile.temp_month.2 = 12; volatile.temp_month.3 = 1; volatile.temp_month.4 = 2; volatile.temp_month.5 = 3; volatile.temp_month.6 = 4; volatile.temp_month.7 = 5; volatile.temp_month.8 = 6; volatile.temp_month.9 = 7; volatile.temp_month.10 = 8; volatile.temp_month.11 = 9; volatile.temp_month.12 = 10; if (month == 1 or month == 2) then ( year -= 1; ); int cent = year / 100; # century of year int yy = year % 100; # year of century int a = node_value("volatile.temp_month." . month); int w = (13 * a - 1) / 5; int x = yy / 4; int y = cent / 4; int z = w + x + y + day + yy - (2 * cent); int r = z % 7; if (r < 0) then ( r += 7; ); r + 1; )); # # # # get_is_date_time_in_epoc() # # # subroutine(get_is_date_time_in_epoc( string sample_date), ( # debug_message("#### get_is_date_time_in_epoc()\n"); # debug_message("#### sample_date: " . sample_date . "\n"); # Returns true if this is a date_time in EPOC bool is_date_time_in_epoc = !matches_regular_expression(sample_date, "[^0-9]+"); # debug_message("#### is_date_time_in_epoc: " . is_date_time_in_epoc . "\n"); # Return is_date_time_in_epoc; ));