Various formatting conversion routines (mainly dates)

Oct 18, 2009
214
8
#1
This is a set of formatting routines mainly for dates. These are mainly for converting non-standard formats between systems or for displaying data in a more human-readable format (e.g., CCYY-MM-DD-hh-mm-ss rather than CCYYMMDDhhmmss)

They convert between ISO (CCYYMMDD), slash6 (MM/DD/YY), ISO-dash (CCYY-MM-DD), slash8 (MM/DD/CCYY) and various other formats.

Note that sometimes conversions will require calling two routines in a row, e.g., there is an ISO_TO_SLASH routine (slash8) and a SLASH8_TO_6 routine but no ISO_TO_SLASH6 routine; there are SLASH_TO_ISO (slash8) and ISO_WITH_DASH but no SLASH_TO_ISO_WITH_DASH.

Formatting routines are also included for:
  1. Social Security Numbers
  2. U.S. telephone numbers with area codes
  3. U.S. 9-digit ZIP Codes
Note that NOW_WITH_MIN_HYPHENS and NOW_WITH_SEC_HYPHENS simply define a variable with those names and set them to the current time. The main use would be for adding a time-stamp to a backup copy filename. The variables do not automatically update! If your program called the routine 5 minutes ago, that will be variable's value until you call the routine again.

Note: Although most of these list two parameters in the label, none of them require using a second parameter. That is only the name of the variable that will contain the value returned. None of these change the value of the first parameter.

::******************************************************
:FORMAT_SSN [SSN_in SSN_out]
::
:: Adds hyphens to SSN
::
:: Parameters: SSN_in: string in format 143210313
:: Output is returned in %SSN_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_SSN %SSN
:: Set SSN=%SSN_out
::
::******************************************************

IFF %@len[%@trim[%SSN_in]] EQ 9 THEN
Set SSN_out=%@left[3,%SSN_in]-%@instr[3,2,%SSN_in]-%@right[4,%SSN_in]
ELSE
Set SSN_out=
ENDIFF

RETURN
:: End of FORMAT_SSN [SSN_in SSN_out]


::******************************************************
:FORMAT_PHONE [phone_in phone_out]
::
:: Adds hyphens to 10-digit phone
::
:: Parameters: phone_in: string in format 2143210313
:: Output is returned in %phone_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_PHONE %calling_phone_var
:: Set calling_phone_var=%phone_out
::
::******************************************************

IFF %@len[%@trim[%phone_in]] EQ 10 THEN
Set phone_out=%@left[3,%phone_in]-%@instr[3,3,%phone_in]-%@right[4,%phone_in]
ELSE
Set phone_out=
ENDIFF

RETURN
:: End of FORMAT_PHONE [phone_in phone_out]


::******************************************************
:FORMAT_ZIP_9 [ZIP_in ZIP_out]
::
:: Adds hyphen to 9-digit ZIP Codes
::
:: Parameters: ZIP_in: string in format 752310313
:: Output is returned in %ZIP_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_ZIP_9 %calling_zip_var
:: Set calling_zip_var=%ZIP_out
::
::******************************************************

set ziplen=%@len[%@trim[%ZIP_in]]

IFF %ziplen EQ 9 THEN
Set ZIP_out=%@left[5,%ZIP_in]-%@right[4,%ZIP_in]
ELSEIFF %ziplen EQ 5 THEN
Set ZIP_out=%ZIP_in
ELSE
Set ZIP_out=
ENDIFF

UNSET ziplen

RETURN
:: End of FORMAT_ZIP_9 [ZIP_in ZIP_out]


::******************************************************
:ISO_DASH_TO_ISO [isodate_dash isodate]
::
:: Converts CCYY-MM-DD to CCYYMMDD e.g., 2014-03-13 > 20140313
::
:: Parameters: isodate_dash string in format 2014-03-13
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_DASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@replace[-,,%isodate_dash]

RETURN
:: End of ISO_DASH_TO_ISO [isodate_dash isodate]


::******************************************************
:ISO_WITH_DASH [isodate isodate_dash]
::
:: Converts CCYYMMDD to CCYY-MM-DD e.g., 20140313 > 2014-03-13
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %isodate_dash, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_WITH_DASH %datevar
:: Set datevar=%isodate_dash
::
::******************************************************
Set isodate_dash=%@left[4,%isodate]-%@instr[4,2,%isodate]-%@right[2,%isodate]

RETURN
:: End of ISO_WITH_DASH [isodate isodate_dash]


::******************************************************
:ISO_TO_SLASH [isodate slashdate]
::
:: Converts CCYYMMDD to MM/DD/CCYY, e.g., 20140313 > 03/13/2014
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %slashdate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_TO_SLASH %datevar
:: Set datevar=%slashdate
::
::******************************************************
Set slashdate=%@instr[4,2,%isodate]/%@right[2,%isodate]/%@left[4,%isodate]

RETURN
:: End of ISO_TO_SLASH [isodate slashdate]


::******************************************************
:SLASH_TO_ISO [slashdate isodate]
::
:: Converts MM/DD/CCYY to CCYYMMDD, e.g., 03/13/2014 > 20140313
::
:: Parameter: slashdate: string in format 03/13/2014
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@right[4,%slashdate]%@left[2,%slashdate]%@instr[3,2,%slashdate]

RETURN
:: End of SLASH_TO_ISO [slashdate isodate]


::******************************************************
:SLASH6_TO_8 [slashdate6 slashdate8]
::
:: Converts MM/DD/YY to MM/DD/CCYY, e.g., 03/13/14 to 03/13/2014
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/14
:: Output is returned in %slashdate8, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH6_TO_8 %datevar
:: Set datevar=%slashdate8
::
::******************************************************
Set slashdate8=%@left[-2,%slashdate6]20%@right[2,%slashdate6]

RETURN
:: End of SLASH6_TO_8 [slashdate6 slashdate8]


::******************************************************
:SLASH8_TO_6 [slashdate8 slashdate6]
::
:: Converts MM/DD/CCYY to MM/DD/YY, e.g., 03/13/2014 to 03/13/14
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/2014
:: Output is returned in %slashdate6, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH8_TO_6 %datevar
:: Set datevar=%slashdate6
::
::******************************************************
Set slashdate6=%@left[6,%slashdate8]%@right[2,%slashdate8]

RETURN
:: End of SLASH8_TO_6 [slashdate6 slashdate8]


::******************************************************
:DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]
::
:: Parameter: YYYYMMDDx string in format YYYYMMDD<anything>
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB DATE_AS_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************
Set dateYYMMDD=%@left[6,%@right[-2,%YYYYMMDDx]]

RETURN
:: End of DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]


::******************************************************
:NOW_WITH_MIN_HYPHENS
::
:: Sets var %now_w_min_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]
Set now_w_min_hyphens=%t
UNSET t

RETURN
:: End :NOW_WITH_MIN_HYPHENS


::******************************************************
:NOW_WITH_SEC_HYPHENS
::
:: Sets var %now_w_sec_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm-ss
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]-%@right[2,%t]
Set now_w_sec_hyphens=%t
UNSET t

RETURN
:: End :NOW_WITH_SEC_HYPHENS


::******************************************************
:SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]
::
:: Parameter: date string in format 7/3/20xx or 07/03/20xx or 7/03/20xx or 07/3/20xx
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB SLASHVARDATE_TO_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************

set monthMM=%@formatn[02.0,%@word["/",0,%slashvardate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashvardate]]
set yearYY=%@right[2,%slashvardate]

Set dateYYMMDD=%yearYY%%monthMM%%dayDD
UNSET monthMM dayDD yearYY

RETURN
:: End of SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]
 
Oct 18, 2009
214
8
#2
Easier way to do NOW_WITH_MIN_HYPHENS and NOW_WITH_SEC_HYPHENS:

Set now_w_min_hyphens=%_year-%_month-%_day-%_hour-%_minute

Set now_w_sec_hyphens=%_year-%_month-%_day-%_hour-%_minute-%_second
 
May 29, 2008
533
3
Groton, CT
#3
The "easier" way is prone to be erroneous. If you invoke %_year-%_month-%_day-%_hour-%_minute-%_second just before the change of a minute such as at 12:35:59.999999, you might get the earlier minute with zero seconds, if the clock rolls over between the acquisition of %_minute and %_second, and the report would be 12-35-00. Similarly, if you invoke %_year-%_month-%_day-%_hour-%_minute near the end of an hour, like at 02:59:59.99999999, the report might be 02-00. Invoked at 2013-12-31,23:59:59.999999, the result could be 2013-01-01-00-00-00. It's not likely, but it's possible. I'd recommend using the code in the original post.
 
May 29, 2008
533
3
Groton, CT
#5
Here's another way:

Set now_w_min_hyphens=%@left[-3,%@replace[:,-,%_isodate-%_time]]

Set now_w_sec_hyphens=%@replace[:,-,%_isodate-%_time]

Of course, for this to work, you need to be displaying your time in 24-hour format.
 
Oct 18, 2009
214
8
#6
The main purpose of the "Now_with" ... routines is mainly to add a timestamp to backup files. So if it shows SS as 00, that's fine. I had to create the _w_sec routine because I originally used _w_min to do a backup before modifying data and again after modifying data. I saw that sometimes I was getting only one backup. If the overall program took less than a minute, the second backup was overwriting the first backup.
 
Oct 18, 2009
214
8
#7
I just noticed that I swapped ISODate and ISODate_dash. I thought "ISO Date" was only digits (despite what TCC Help says). The conversion code is all correct, just the descriptive names are wrong. Each routine contains a description that shows the format of the input and output data, so selecting the correct routine shoudn't be a big problem.

I'm going to be posting an improved version of the routines and I'll correct the names in that.
 
Oct 18, 2009
214
8
#8
IMPROVED AND CORRECTED conversion routines.

Note 1: In addition to corrections, I realized that if an output is YYMMDD it doesn't matter whether the input is YYMMDD or CCYYMMDD, so I changed some of the routines from SLASH8 to SLASH_ANY.

Note 2: The next Reply contains the TCC file test-library.btm

** START OF CODE **

:: Library.btm
::
:: Subroutine library file
:: To use, somewhere you can define LibFile and LIBRARY:
::
:: Set LibFile=C:\Program Files\JPSoft\Library.btm
:: ALIAS LIBRARY=GOSUB "%LibFile" %2$
::
:: That code basically does the following:
:: GOSUB "C:\Program Files\JPSoft\BTM\Library.btm" Subrou_Name [%2 %3 %4 %5 %6 %7]

:: RE: OVN (Output-Variable Name)
::
:: This is the NAME OF THE VARIABLE in which the return value (OUTPUT of routine)
:: will be stored. The name must have a format valid for environment variables.
:: For instance, if you want 7 days added to a date by (fictional) routine
:: ONE_WEEK_LATER [start_date OVN] with the result stored in %week_later:
::
:: Set Date=2013-03-02
:: GOSUB ONE_WEEK_LATER %Date week_later
:: :: Following displays: 2013-03-09
:: Echo %week_later
::
:: Note that the name of the return variable is NOT quoted AND does NOT have
:: a % sign. If you WERE to use a % sign, you would have to PRE-DEFINE that
:: variable with a format valid for ENVIRONMENT VARIABLE NAMES, e.g.
::
:: Set Date=2013-03-02
:: Set New_Date_Name=week_later
:: GOSUB ONE_WEEK_LATER %Date %New_Date_Name
:: :: Following displays: week_later
:: Echo %New_Date_Name
:: :: Following displays: 2013-03-09
:: Echo %week_later


:: ========================================
:: SUBROUS
:: ========================================
::
:: DEFINE_DISPLAY_COLORS
::
:: SELECT_FILE [Pattern OVN]
::
::
:: FORMAT STANDARD NUMBERS: SSN, Phone, ZIP
::
:: FORMAT_SSN [SSN_in SSN_out]
:: FORMAT_PHONE [phone_in phone_out]
:: FORMAT_ZIP_9 [ZIP_in ZIP_out]
::
:: ISODATE FORMAT CONVERSION
::
:: ISO_TO_CCYYMMDD [isodate OVN]
:: CCYYMMDD_TO_ISO [CCYYMMDD OVN]
::
:: SLASHDATE FORMAT CONVERSION
::
:: CCYYMMDD_TO_SLASH8 [CCYYMMDD OVN]
:: SLASH8_TO_CCYYMMDD [slashdate OVN]
:: SLASH_ANY_TO_6 [slashdate OVN]
:: SLASH_ANY_TO_SLASH20xx [slashdate OVN]
:: SLASH_ANY_TO_YYMMDD [slashdate OVN]
::
:: YYMMDD FORMAT CONVERSION
::
:: CCYYMMDD_TO_YYMMDD [CCYYMMDD OVN]
::
:: BETTER "NOW"
::
:: NOW_WITH_MIN_HYPHENS
:: NOW_WITH_SEC_HYPHENS
::
:: End of list of subroutine names

@echo off

Echo.
Echo.
Echo ****************************************************
Echo.
Echo File %_batchname
Echo only contains library subroutines !!!
Echo.
Echo ****************************************************
Echo.
PAUSE
QUIT

::****************************************************
:DEFINE_DISPLAY_COLORS
::
:: Defines four aliases:
::
:: error: red on white
:: main: blue on white
:: Emphasis_1: green on white
:: Emphasis_2: bright blue on white
::
::****************************************************

:: Set 4 aliases
ALIAS USE_ERROR_COLOR=color red on bright white
ALIAS USE_MAIN_COLOR=color blue on bright white
ALIAS USE_EMPHASIS_1_COLOR=color green on bright white
ALIAS USE_EMPHASIS_2_COLOR=color bright blue on bright white

:: Set the color
USE_MAIN_COLOR

RETURN
:: End of DEFINE_DISPLAY_COLORS


::******************************************************
:SELECT_FILE [Pattern OVN]
::
:: Gets filename
:: If no name is picked QUITS THE PROGRAM.
::
:: Parameters: Pattern: pattern match for SELECT command
:: OVN: string with the name of the return variable
::
:: CALL WITH: GOSUB SELECT_FILE Pattern MyFileVarName
::
::******************************************************

SELECT /1 SET tmp=(%Pattern)

:: No name picked (Esc pressed)
IFF "%tmp" EQ "" THEN
:: No file specified
ECHO.
ECHO NO FILE NAME SPECIFIED ! -- EXITING
PAUSE
QUIT
ENDIFF

Set %OVN=%tmp
UNSET tmp

RETURN
:: End of SELECT_FILE [Pattern OVN]


::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
::
: UTILITY & CONVERSION ROUTINES
::
::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

::******************************************************
:FORMAT_SSN [SSN_in OVN]
::
:: Adds hyphens to SSN
::
:: Parameters:
:: SSN_in: string in format 143210313
:: OVN: String with the NAME of the variable that the
:: result will be stored in. This CAN be the name of
:: a variable passed TO the routine with the SSN data
::
:: CALL WITH: GOSUB FORMAT_SSN %SSN SSN
::
::******************************************************

IFF %@len[%@trim[%SSN_in]] EQ 9 THEN
Set %OVN=%@left[3,%SSN_in]-%@instr[3,2,%SSN_in]-%@right[4,%SSN_in]
ELSE
Set %OVN=
ENDIFF

RETURN
:: End of FORMAT_SSN [SSN_in OVN]


::******************************************************
:FORMAT_PHONE [phone_in OVN]
::
:: Adds hyphens to 10-digit phone
::
:: Parameters: phone_in: string in format 2143210313
:: OVN: string with the name of the return variable
::
:: CALL WITH: GOSUB FORMAT_PHONE %phone_num PHONE_NUM
::
::******************************************************

IFF %@len[%@trim[%phone_in]] EQ 10 THEN
Set %OVN=%@left[3,%phone_in]-%@instr[3,3,%phone_in]-%@right[4,%phone_in]
ELSE
Set %OVN=
ENDIFF

RETURN
:: End of FORMAT_PHONE [phone_in OVN]


::******************************************************
:FORMAT_ZIP_9 [ZIP_in OVN]
::
:: Adds hyphen to 9-digit ZIP Codes
::
:: Parameters: ZIP_in: string in format 752310313
:: OVN: string with the name of the return variable
::
:: CALL WITH: GOSUB FORMAT_ZIP_9 %ZIP ZIP_out
::
::******************************************************

set ziplen=%@len[%@trim[%ZIP_in]]

IFF %ziplen EQ 9 THEN
Set %OVN=%@left[5,%ZIP_in]-%@right[4,%ZIP_in]
ELSEIFF %ziplen EQ 5 THEN
Set %OVN=%ZIP_in
ELSE
Set %OVN=
ENDIFF

UNSET ziplen

RETURN
:: End of FORMAT_ZIP_9 [ZIP_in OVN]


::******************************************************
:ISO_TO_CCYYMMDD [isodate OVN]
::
:: Converts ISO format to CCYYMMDD e.g., 2014-03-13 > 20140313
::
:: Parameters: isodate format string
:: OVN: string with the name of the return variable
::
:: Call with: ISO_TO_CCYYMMDD %datevar CCYYMMDD
::
::******************************************************
Set %OVN=%@replace[-,,%isodate]

RETURN
:: End of ISO_TO_CCYYMMDD [isodate OVN]


::******************************************************
:CCYYMMDD_TO_ISO [CCYYMMDD OVN]
::
:: Converts CCYYMMDD to CCYY-MM-DD e.g., 20140313 > 2014-03-13
::
:: Parameters: CCYYMMDD: string in format 20140313
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB CCYYMMDD_TO_ISO %datevar isodate
::
::******************************************************
Set %OVN=%@left[4,%CCYYMMDD]-%@instr[4,2,%CCYYMMDD]-%@right[2,%CCYYMMDD]

RETURN
:: End of CCYYMMDD_TO_ISO [CCYYMMDD OVN]


::******************************************************
:CCYYMMDD_TO_SLASH8 [CCYYMMDD OVN]
::
:: Converts CCYYMMDD to MM/DD/CCYY, e.g., 20140313 > 03/13/2014
::
:: Parameters: CCYYMMDD: string in format 20140313
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB CCYYMMDD_TO_SLASH8 %datevar slash8
::
::******************************************************
Set %OVN=%@instr[4,2,%CCYYMMDD]/%@right[2,%CCYYMMDD]/%@left[4,%CCYYMMDD]

RETURN
:: End of CCYYMMDD_TO_SLASH8 [CCYYMMDD OVN]


::******************************************************
:SLASH8_TO_CCYYMMDD [slashdate OVN]
::
:: Converts MM/DD/CCYY to CCYYMMDD, e.g., 03/13/2014 > 20140313
::
:: Parameter: slashdate: string in format 03/13/2014
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB SLASH8_TO_CCYYMMDD %datevar CCYYMMDD
::
::******************************************************
Set %OVN=%@right[4,%slashdate]%@left[2,%slashdate]%@instr[3,2,%slashdate]

RETURN
:: End of SLASH8_TO_CCYYMMDD [slashdate OVN]


::******************************************************
:SLASH_ANY_TO_6 [slashdate OVN]
::
:: Parameter: date string in format M/D/Y
:: M and D MAY have leading zeroes. Y can be CCYY or
:: only YY.
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB SLASH_ANY_TO_6 %datevar slash6
::
::******************************************************

set monthMM=%@formatn[02.0,%@word["/",0,%slashdate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashdate]]
set yearYY=%@right[2,%slashdate]

Set %OVN=%dayDD%/%monthMM%/%yearYY%

UNSET monthMM dayDD yearYY

RETURN
:: End of SLASH_ANY_TO_6 [slashdate OVN]


::******************************************************
:SLASH_ANY_TO_SLASH20xx [slashdate OVN]
::
:: Parameter: date string in format M/D/Y
:: M and D MAY have leading zeroes. Y can be CCYY or
:: only YY.
:: OVN: string with the name of the return variable
::
:: Call with: SLASH_ANY_TO_SLASH20xx %datevar slash20xx
::
::******************************************************

set monthMM=%@formatn[02.0,%@word["/",0,%slashdate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashdate]]
set yearYY=%@right[2,%slashdate]

Set %OVN=%dayDD/%monthMM/20%yearYY

UNSET monthMM dayDD yearYY

RETURN
:: End of SLASH_ANY_TO_SLASH20xx [slashdate OVN]


::******************************************************
:SLASH_ANY_TO_YYMMDD [slashdate OVN]
::
:: Parameter: date string in format M/D/Y
:: M and D MAY have leading zeroes. Y can be CCYY or
:: only YY.
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB SLASH_ANY_TO_YYMMDD %datevar YYMMDD
::
::******************************************************

set monthMM=%@formatn[02.0,%@word["/",0,%slashdate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashdate]]
set yearYY=%@right[2,%slashdate]

Set %OVN=%yearYY%%monthMM%%dayDD
UNSET monthMM dayDD yearYY

RETURN
:: End of SLASH_ANY_TO_YYMMDD [slashdate OVN]


::******************************************************
:CCYYMMDD_TO_YYMMDD [CCYYMMDD OVN]
::
:: Drops the century from a date in CCYYMMDD format
::
:: Parameter: CCYYMMDD date string 20140313
:: OVN: string with the name of the return variable
::
:: Call with: GOSUB CCYYMMDD_TO_YYMMDD %datevar YYMMDD
::
::******************************************************
Set %OVN=%@left[6,%@right[-2,%CCYYMMDD]]

RETURN
:: End of CCYYMMDD_TO_YYMMDD [CCYYMMDD OVN]


::******************************************************
:NOW_WITH_MIN_HYPHENS
::
:: Sets var %now_w_min_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm
::
::******************************************************
Set now_w_min_hyphens=%_year-%_month-%_day-%_hour-%_minute

RETURN
:: End :NOW_WITH_MIN_HYPHENS


::******************************************************
:NOW_WITH_SEC_HYPHENS
::
:: Sets var %now_w_sec_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm-ss
::
::******************************************************
Set now_w_sec_hyphens=%_year-%_month-%_day-%_hour-%_minute-%_second

RETURN
:: End :NOW_WITH_SEC_HYPHENS

** END OF CODE **
 
Last edited:
Oct 18, 2009
214
8
#9
TCC file: test-library.btm

You will need to change the line that defines %LibFile

** START OF CODE **

:: test-library.btm
::
:: Tests the subrous in the file library.btm

@echo off
CLS

:: Change LibFile to where the Library.btm file is located. Don't quote the name.
Set LibFile=C:\Program Files\JPSoft\Library.btm
ALIAS LIBRARY=GOSUB "%LibFile" %2$

:: FORMAT STANDARD NUMBERS: SSN, Phone, ZIP

Echo.
Echo FORMAT_SSN [SSN SSN]
Set SSN=123456789
LIBRARY FORMAT_SSN %SSN SSN
Echo %SSN

:: Same as above but shows what happens if you use
:: a variable as the second parameter
Echo.
Echo `FORMAT_SSN [SSN %SSN]`
Set SSN=123456789
Set OutVar=SSN_out
LIBRARY FORMAT_SSN %SSN %OutVar
:: Echos: 123-45-6789
Echo %SSN
:: Echos: 123-45-6789
Echo %SSN_out
:: Echos: SSN_out
Echo %OutVar

Echo.
Echo FORMAT_PHONE [Phone OVN]
Set Phone=9876543210
LIBRARY FORMAT_PHONE %Phone Phone_out
Echo %Phone_out

Echo.
Echo FORMAT_ZIP_9 [ZIP OVN]
Set ZIP=123456789
LIBRARY FORMAT_ZIP_9 %ZIP ZIP_out
Echo %ZIP_out

:: ISODATE FORMAT CONVERSION
::
Echo.
Echo ISO_TO_CCYYMMDD [isodate OVN]
Set isodate=2013-03-09
LIBRARY ISO_TO_CCYYMMDD %isodate date_out
Echo %date_out

Echo.
Echo CCYYMMDD_TO_ISO [CCYYMMDD OVN]
Set indate=20130315
LIBRARY CCYYMMDD_TO_ISO %indate date_out
Echo %date_out


:: SLASHDATE FORMAT CONVERSION

Echo.
Echo CCYYMMDD_TO_SLASH8 [CCYYMMDD OVN]
Set indate=20140620
LIBRARY CCYYMMDD_TO_SLASH8 %indate date_out
Echo %date_out

Echo.
Echo SLASH8_TO_CCYYMMDD [slashdate OVN]
Set slash8=11/18/2014
LIBRARY SLASH8_TO_CCYYMMDD %slash8 date_out
Echo %date_out

Echo.
Echo SLASH_ANY_TO_6 [slashdate OVN]
Set slash8=10/04/2014
LIBRARY SLASH_ANY_TO_6 %slash8 date_out
Echo %date_out

Echo.
Echo SLASH_ANY_TO_SLASH20xx [slashdate OVN]
Set slash6=01/04/13
LIBRARY SLASH_ANY_TO_SLASH20xx %slash6 date_out
Echo %date_out
Set slash8=01/04/2013
LIBRARY SLASH_ANY_TO_SLASH20xx %slash8 date_out
Echo %date_out
Echo.
Set slash6=01/04/13
LIBRARY SLASH_ANY_TO_SLASH20xx [%slash6 date_out]
Echo %date_out
Set slash8=01/04/2013
LIBRARY SLASH_ANY_TO_SLASH20xx [%slash8 date_out]
Echo %date_out

Echo.
Echo SLASH_ANY_TO_YYMMDD [slashdate OVN]
Set slashdate=01/02/2003
LIBRARY SLASH_ANY_TO_YYMMDD %slashdate date_out
Echo %date_out

:: YYMMDD FORMAT CONVERSION

Echo.
Echo CCYYMMDD_TO_YYMMDD [CCYYMMDD OVN]
Set indate= 20130602
LIBRARY CCYYMMDD_TO_YYMMDD %indate date_out
Echo %date_out

:: BETTER "NOW"

Echo.
Echo NOW_WITH_MIN_HYPHENS
LIBRARY NOW_WITH_MIN_HYPHENS
Echo %now_w_min_hyphens

Echo.
Echo NOW_WITH_SEC_HYPHENS
LIBRARY NOW_WITH_SEC_HYPHENS
Echo %now_w_sec_hyphens

QUIT

** END OF CODE **
 
Last edited:
Oct 18, 2009
214
8
#10
Re NOW_WITH_MIN_HYPHENS and NOW_WITH_SEC_HYPHENS

I have two times where I accessed %_Day and %_Seconds are less than 10, one where %_Seconds = 0.

In both cases it gives a single-digit number:

Prior_THR_Placements-2014-12-1-17-28-1.bak
Prior_THR_Placements-2014-12-2-9-28-0.bak​

Here are revised and additional routines. The only revisions to NOW_WITH_MIN_HYPHENS and NOW_WITH_SEC_HYPHENS is a more accurate description.


::******************************************************
:NOW_WITH_MIN_HYPHENS
::
:: Sets var %now_w_min_hyphens
:: Returns _datetime in format CCYY-MM-DD-hh-mm
::
:: NOTE: If date/time is Feb 1, 2:01 am it will return 2014-2-1-2-1
::
::******************************************************
Set now_w_min_hyphens=%_year-%_month-%_day-%_hour-%_minute

RETURN
:: End :NOW_WITH_MIN_HYPHENS


::******************************************************
:NOW_HYPHENS_MIN
::
:: Sets var %now_dash_min
:: Returns _datetime always in format CCYY-MM-DD-hh-mm
::
::******************************************************
Set now_dash_min=%_year-%@right[2,0%_month]-%@right[2,0%_day]-%@right[2,0%_hour]-%@right[2,0%_minute]

RETURN
:: End :NOW_HYPHENS_MIN


::******************************************************
:NOW_WITH_SEC_HYPHENS
::
:: Sets var %now_w_sec_hyphens
:: Returns _datetime in format CCYY-MM-DD-hh-mm-ss
::
:: NOTE: If date/time is Feb 1, 2:01 am it will return 2014-2-1-2-1
::
::******************************************************
Set now_w_sec_hyphens=%_year-%_month-%_day-%_hour-%_minute-%_second

RETURN
:: End :NOW_WITH_SEC_HYPHENS


::******************************************************
:NOW_HYPHENS_SEC
::
:: Sets var %now_dash_sec
:: Returns _datetime always in format CCYY-MM-DD-hh-mm-ss
::
::******************************************************
Set now_dash_sec=%_year-%@right[2,0%_month]-%@right[2,0%_day]
Set now_dash_sec=%now_dash_sec-%@right[2,0%_hour]-%@right[2,0%_minute]-%@right[2,0%_second]

RETURN
:: End :NOW_HYPHENS_SEC
 
Last edited:
Oct 18, 2009
214
8
#11
New conversion routine: MSG_BOX_CHOICE


::****************************************************
:MSG_BOX_CHOICE [Msg_Box_Response OVN]
::
:: Converts a Message Box button choice to human-readable text
::
:: Parameters: Msg_Box_Response: certain numbers from 10 to 24
:: OVN: string with the name of the return variable
::
:: CALL WITH: GOSUB MSG_BOX_CHOICE %_? Choice_Text
::
:: Messages returned:
:: YesOK
:: No
:: Cancel
:: Retry
:: TryAgain
:: Continue
:: Ignore
:: Abort
:: Help
:: Timeout
:: Custom_1
:: Custom_2
:: Custom_3
:: Custom_4
:: ** BAD **
::
::****************************************************

:: This DOES NOT set OVN, it sets the variable whose
:: NAME is STORED in %OVN (indirect set)

SWITCH %Msg_Box_Response%

CASE 10
Set Msg_Box_Resp=YesOK

CASE 11
Set Msg_Box_Resp=No

CASE 12
Set Msg_Box_Resp=Cancel

CASE 13
Set Msg_Box_Resp=Retry

CASE 14
Set Msg_Box_Resp=TryAgain

CASE 15
Set Msg_Box_Resp=Continue

CASE 16
Set Msg_Box_Resp=Ignore

CASE 17
Set Msg_Box_Resp=Abort

CASE 18
Set Msg_Box_Resp=Help

CASE 20
Set Msg_Box_Resp=Timeout

CASE 21
Set Msg_Box_Resp=Custom_1

CASE 22
Set Msg_Box_Resp=Custom_2

CASE 23
Set Msg_Box_Resp=Custom_3

CASE 24
Set Msg_Box_Resp=Custom_4

DEFAULT
Set Msg_Box_Resp=** BAD **

ENDSWITCH

:: This DOES NOT set OVN, it sets the variable whose
:: NAME is STORED in %OVN (indirect Set)
Set %OVN=%Msg_Box_Resp

UNSET Msg_Box_Resp

RETURN
:: End of MSG_BOX_CHOICE [Msg_Box_Response OVN]
 
Last edited:
Oct 18, 2009
214
8
#12
Test routine for MSG_BOX_CHOICE [Msg_Box_Response OVN]

(I'm using TCC/LE 13, which doesn't allow GOSUB-RETURN in a FOR loop.)


@echo off
cls

Set m=10
GOSUB MSG_BOX_CHOICE %m Choice
Echo %Choice

Set m=11
GOSUB MSG_BOX_CHOICE %m Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 12 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 13 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 14 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 15 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 16 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 17 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 18 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 20 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 21 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 22 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 23 Choice
Echo %Choice

GOSUB MSG_BOX_CHOICE 24 Choice
Echo %Choice
 
Oct 18, 2009
214
8
#13
Re FORMAT_PHONE

Although the FORMAT_PHONE routine is correct, as a practical matter it needs a CASE ... ENDCASE set in the IFF section.

As a practical matter you will find "no phone" numbers such as (hyphens added) 111-111-1111, 222-222-2222, xxx-xxx-xxxx, 000-000-0000, 817-000-0000 [the local valid area code followed by "placeholders"], etc. Without an additional CASE ... ENDCASE, those will be passed right through the routine as valid phone numbers.

Also, nowadays it is not unusual to see phone number entries such as 214-987-6543 (cell) or 214-987-6543 x105. Both of those would be stripped out as not valid.