preload

String/Date functions (WinRunner, TSL)

Posted by Albert Gareev on Mar 24, 2007 | Categories: Source codeText Data

Parent page: Service Functions – String / WinRunner

Month Name/Number conversion

static const month_number[] = {
 "JAN" = "01",
 "FEB" = "02",
 "MAR" = "03",
 "APR" = "04",
 "MAY" = "05",
 "JUN" = "06",
 "JUL" = "07",
 "AUG" = "08",
 "SEP" = "09",
 "OCT" = "10",
 "NOV" = "11",
 "DEC" = "12"
 }; 
###
static const month_name[] = {
 "01" = "JAN",
 "02" = "FEB",
 "03" = "MAR",
 "04" = "APR",
 "05" = "MAY",
 "06" = "JUN",
 "07" = "JUL",
 "08" = "AUG",
 "09" = "SEP",
 "10" = "OCT",
 "11" = "NOV",
 "12" = "DEC"
 }; 
###
public function str_month_name(in number) {
 if (length(number) == 1)
  number = "0" & number;
 return(month_name[number]);
}
###
public function str_month_number(in name) {
 return(month_number[toupper(name)]);
}

Date format conversions

# From "24 Mar 2007" to "03/24/2007"
public function str_date_val(in date_str) {
 auto i, s_month, d_month;
 auto result;
 
 i = match(date_str, "[A-Z,a-z][A-Z,a-z][A-Z,a-z]");
 if (i==0) return("");
 
 s_month = substr(date_str, i, 3);
 d_month = month_number[toupper(s_month)];
 result = d_month & "/" & substr(date_str,1,i-1) & "/" & substr(date_str,i+3);
 result = str_date_align(result);
 
 return(result);
}
#### From "Mar 24, 2007" to "03/24/2007"

public function str_date_val2(in date_str) {
 auto i, s, s_month, d_month;
 auto result;
 auto c, mxVector[];
 
 i = match(date_str, "[A-Z,a-z][A-Z,a-z][A-Z,a-z]");
 if (i==0) return("");

 c = split(date_str, mxVector, " ");
 if (c != 3) return("");
 
 s_month = mxVector[1];
 d_month = month_number[toupper(s_month)];
 
 s = substr(mxVector[2],1,length(mxVector[2])-1);

 result = d_month & "/" & s & "/" & mxVector[3];
 result = str_date_align(result);
 
 return(result);
}

###

#normalize date string

public function str_date_align(in date_str) {
 auto rc, c, dtArray[], result;

 if (date_str == "") return("");
 c = split(date_str, dtArray, "/");
 if (c != 3) return("");
 result = "";
 
 if (length(dtArray[1]) == 1)
  result = result & "0" & dtArray[1] & "/";
 else
  result = result & dtArray[1] & "/";
  
 if (length(dtArray[2]) == 1)
  result = result & "0" & dtArray[2] & "/";
 else
  result = result & dtArray[2] & "/";
  
 if (length(dtArray[3]) == 2)
  result = result & "20" & dtArray[3];
 else
  result = result & dtArray[3];
 
 rc = match(result, "[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]");
 if (rc == 0) return("");  

 return(result);
}

###

public function date_str2(in date_val_str) {
 auto Date[];
 auto result;
 
 str_date_split(date_val_str, Date);
 result = str_month_name(Date["month"]) & " " & Date["day"] & ", " & Date["year"];
 return(result);
}


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.