Hi Eros,
I added unit tests to cover Now, Hour, Minute, Second, Day, Month and Year here:
https://github.com/ErosOlmi/ThinBASI...elated.tBasicU
Please note there are 9 failing tests, because:
- Hour, Minute, Second, Day, Month and Year without parameter still return zero instead of expected value (that is, current value)
- Hour, Minute, Second are unable to work with string in case it is just like "11:50:25" (but Day, Month, Year is able to work with string without time)
The first is easy to do (we talked about it earlier), the second... what about making the functions even more flexible with... regex!
Before you start parsing the values, you can get the appropriate date/time parts this easily:
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
MSGBOX GetDatePart("Hello Eros, do you remember what happened on 11/08/2014. I do not :D")
MSGBOX GetTimePart("Ciao, it is 20/11/2015 today, and I am writing this on 12:36:50")
END FUNCTION
FUNCTION GetDatePart(BYVAL inputText AS STRING) AS STRING
LOCAL position, length AS LONG
REGEXPR "([0-9]+)/([0-9]+)/([0-9]+)" IN inputText TO position, length
FUNCTION = MID$(inputText, position, length)
END FUNCTION
FUNCTION GetTimePart(BYVAL inputText AS STRING) AS STRING
LOCAL position, length AS LONG
REGEXPR "([0-9]+):([0-9]+):([0-9]+)" IN inputText TO position, length
FUNCTION = MID$(inputText, position, length)
END FUNCTION
... with this one, we would be able to return date correctly even from normal text. That would bring even more value.
I am not very strong with regular expressions, but these seem to work!
I also created documentation for those functions, you should be able to just copy paste:
Now
Description
Returns date time stamp representing current date and time on local computer.
Syntax
timeStamp = Now
Returns
String
Parameters
Remarks
The time stamp contains two parts, separated by space.
First part represents date, second part time.
The overall signature is:
DD/MM/YYYY hh:mm:ss
Restrictions
Will contain invalid date and time, if you have set it wrong on your local computer.
See also
Day, Month, Year, Hour, Minute, Second
Examples
String dateTimeStamp = Now
Day
Description
Extracts day in month from string containing "DD/MM/YYYY" pattern
If used without parameter, returns current day in month directly.
Syntax
dayNumber = Day(dateTimeStamp)
dayNumber = Day
Returns
Number in range 1..31
Parameters
dateTimeStamp String Yes Value conaining DD/MM/YYYY pattern
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Month, Year, Hour, Minute, Second
Examples
Long dayToday = Day(Now)
Long dayFromString = Day("Let's meet on 12/06/2015")
Long dayTodayDirect = Day
Month
Description
Extracts month from string containing "DD/MM/YYYY" pattern
If used without parameter, returns current month directly.
Syntax
monthNumber = Month(dateTimeStamp)
monthNumber = Month
Returns
Number in range 1..12
Parameters
dateTimeStamp String Yes Value conaining DD/MM/YYYY pattern
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Day, Year, Hour, Minute, Second
Examples
Long currentMonth = Month(Now)
Long monthFromString = Month("I don't remember what is special about 12/06/2015, do you?")
Long currentMonthDirect = Month
Year
Description
Extracts year from string containing "DD/MM/YYYY" pattern
If used without parameter, returns current year directly.
Syntax
yearNumber = Year(dateTimeStamp)
yearNumber = Year
Returns
Number
Parameters
dateTimeStamp String Yes Value conaining DD/MM/YYYY pattern
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Day, Month, Hour, Minute, Second
Examples
Long currentYear = Year(Now)
Long yearFromString = Year("Thinking of 12/06/2015...")
Long currentYearDirect = Year
Hour
Description
Extracts hour from string containing "hh:mm:ss" pattern
If used without parameter, returns current hour directly.
Syntax
hourNumber = Hour(dateTimeStamp)
hourNumber = Hour
Returns
Number in range 0..23
Parameters
dateTimeStamp String Yes Value containing pattern hh:mm:ss
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Day, Month, Year, Minute, Second
Examples
Long currentHour = Hour(Now)
Long hourFromString = Hour("I woke up today around 11:50:25")
Long currentHourDirect = Hour
Minute
Description
Extracts minute from string containing "hh:mm:ss" pattern
If used without parameter, returns current minute directly.
Syntax
minuteNumber = Minute(dateTimeStamp)
minuteNumber = Minute
Returns
Number in range 0..59
Parameters
dateTimeStamp String Yes Value containing pattern hh:mm:ss
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Day, Month, Year, Hour, Second
Examples
Long currentMinute = Minute(Now)
Long minuteFromString = Minute("It was around 11:50:25, or not?")
Long currentMinuteDirect = Minute
Second
Description
Extracts second from string containing "hh:mm:ss" pattern
If used without parameter, returns current second directly.
Syntax
secondNumber = Second(dateTimeStamp)
secondNumber = Second
Returns
Number in range 0..59
Parameters
dateTimeStamp String Yes Value containing pattern hh:mm:ss
Remarks
Restrictions
The passed dateTimeStamp must follow the mentioned signature, otherwise incorrect result can be returned.
See also
Now, Day, Month, Year, Hour, Minute
Examples
Long currentSecond = Second(Now)
Long secondFromString = Second("Current time is like 11:50:25, ... or maybe not")
Long currentSecondDirect = Second
Petr
Bookmarks