Bug 93727 - Date Literal should be supported
Summary: Date Literal should be supported
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium enhancement
Assignee: Not Assigned
URL:
Whiteboard: target:5.5.0
Keywords: difficultyInteresting, easyHack, skillCpp
Depends on:
Blocks:
 
Reported: 2015-08-28 04:54 UTC by himajin100000
Modified: 2017-08-31 05:40 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
macroTest.xls: push-button tests for this bug report (13.00 KB, application/vnd.ms-excel)
2017-04-14 07:58 UTC, Justin L
Details
macroTest.xls: updated push-button tests for this bug report - all date tests work in Excel (39.50 KB, application/vnd.ms-excel)
2017-04-15 10:00 UTC, Justin L
Details

Note You need to log in before you can comment on or make changes to this bug.
Description himajin100000 2015-08-28 04:54:07 UTC
STEPS TO REPRODUCE:

1-a. Run the following code

Sub Main()
	Dim x As Date
	Dim y As Date
	x = #1/2/2013#
	y = 1/2/2013
	Msgbox(x <> y)
End Sub

EXPECTED RESULT:
true

ACTUAL RESULT:
false

=====================
ADDITIONAL INFORMATION:

1-b. Run the following code

REM  *****  BASIC  *****
Sub Main()
	Dim x
	#x = 1
	#If x #Then
	Msgbox(x)
	#End If
End Sub

EXPECTED RESULT:
COMPILE ERROR

ACTUAL RESULT:
1
=========================
'#' is  ignored except for some special cases

http://opengrok.libreoffice.org/xref/core/basic/source/comp/scanner.cxx#243
http://opengrok.libreoffice.org/xref/core/basic/source/comp/scanner.cxx#502
Comment 1 Buovjaga 2015-09-12 14:27:16 UTC
Reproduced.

Win 7 Pro 64-bit, Version: 5.0.1.2 (32-bit)
Build ID: 81898c9f5c0d43f3473ba111d7b351050be20261
Locale: fi-FI (fi_FI)
Comment 2 Xisco Faulí 2016-09-15 11:37:11 UTC
@Jan, a plausible easyhack?
Comment 3 jani 2016-09-15 11:41:59 UTC
(In reply to Xisco Faulí from comment #2)
> @Jan, a plausible easyhack?
yup.
Comment 4 Grzegorz Araminowicz 2017-04-01 14:50:56 UTC
I uploaded a patch to support date literals: https://gerrit.libreoffice.org/#/c/36002/

Currently, lines starting with # are treated as compiler directives and ignored. I didn't change this behavior as it could break compatibility with some existing macros.
Comment 5 himajin100000 2017-04-01 22:34:40 UTC
For My Information:

I'm unsure where I can find the official speicification of StarBasic.
At least there exists a format requirements for date literals in VBA
https://msdn.microsoft.com/en-us/library/3eaydw6e.aspx

>Format Requirements

>You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#, or yyyy-MM-dd, for example #1993-5-31#. You can use slashes when specifying the year first. This requirement is independent of your locale and your computer's date and time format settings.

>The reason for this restriction is that the meaning of your code should never change depending on the locale in which your application is running. Suppose you hard-code a Date literal of #3/4/1998# and intend it to mean March 4, 1998. In a locale that uses mm/dd/yyyy, 3/4/1998 compiles as you intend. But suppose you deploy your application in many countries. In a locale that uses dd/mm/yyyy, your hard-coded literal would compile to April 3, 1998. In a locale that uses yyyy/mm/dd, the literal would be invalid (April 1998, 0003) and cause a compiler error.
Comment 6 Grzegorz Araminowicz 2017-04-12 19:03:31 UTC
I updated the patch few days ago. Please have a look.
Comment 7 himajin100000 2017-04-12 19:12:26 UTC
Thanks for your great work. I've already taken a look on gerrit. 
(but without compiling, for the reluctance to learn about how to build the libreoffice source. I will try the software when the new build comes out.)

Also, I'm sorry not to be useful, I don't have a Mac.

(In reply to Grzegorz Araminowicz from comment #6)
Comment 8 Justin L 2017-04-14 07:58:24 UTC
Created attachment 132554 [details]
macroTest.xls: push-button tests for this bug report

(In reply to himajin100000 from comment #0)
> ADDITIONAL INFORMATION:
> 
> 1-b. Run the following code
> 
> REM  *****  BASIC  *****
> Sub Main()
> 	Dim x
> 	#x = 1
> 	#If x #Then
> 	Msgbox(x)
> 	#End If
> End Sub

This was fixed in LO5.4 on 2017-02-28 by https://cgit.freedesktop.org/libreoffice/core/commit/?id=97caa8a8a0d80baa1a77859b117a8bc2b8b952f8. x no longer is assigned the value of 1, so an empty msgbox is now displayed. (There are no compile errors, but that hardly seems important.  If you want cross-compatible VBA, do your programming modifications in MSOffice.)

Note: as suggested by OP, # is not a comment marker in VBA. Excel2003's compiler complains as you type #x = 1. Thus, these functions are invalid code.
Comment 9 Justin L 2017-04-15 10:00:41 UTC
Created attachment 132584 [details]
macroTest.xls: updated push-button tests for this bug report - all date tests work in Excel

(In reply to himajin100000 from comment #0)
> Sub Main()
> 	Dim x As Date
> 	Dim y As Date
> 	x = #1/2/2013#
> 	y = 1/2/2013
> 	Msgbox(x <> y)
> End Sub

(y = 1/2/2013) is deceptive because this is not treated by VBA as a date. It needs to be a string (y = "1/2/2013") to be treated as a locale-dependant date.

In Excel, time-only can be specified as #hh:mm:ss PM#, but not as a string apparently. ("hh:mm:ss PM" doesn't work.)
Comment 10 Justin L 2017-04-15 16:39:58 UTC
possible double # on a line from https://msdn.microsoft.com/en-us/library/aa243386(v=vs.60).aspx

Data 	                Value assigned to variable
----                    --------------------------
#NULL# 	                Null
#TRUE# or #FALSE# 	True or False
#yyyy-mm-dd hh:mm:ss# 	The date and/or time represented by the expression
#ERROR errornumber# 	errornumber (variable is a Variant tagged as an error)

And I suppose something like this would be valid.
line input #1, myString1 : line input #2, myString2
Comment 11 Grzegorz Araminowicz 2017-04-18 22:32:18 UTC
I thought this bug is only about date literals. I'm afraid supporting more complicated literals would require significant changes how scanner works.

Beside uses of # mentioned by Justin, it can be also used as type specifier: 100# means to treat 100 as double.

If date literal has spaces inside, it's difficult to differentiate it from other cases, because scanner does not know about context.
For example
#123 456#
can be treated as input parameter and double literal or as incorrect DateTime.

Maybe let's keep this bug focused on supporting dates only and open another one for other literals?
Comment 12 Xisco Faulí 2017-05-21 09:05:28 UTC Comment hidden (obsolete)
Comment 13 Grzegorz Araminowicz 2017-05-21 15:28:30 UTC
(In reply to Xisco Faulí from comment #12)
> A polite ping, still working on this bug

I had little free time recently, but I'm getting back to work on the bug this week.
Comment 14 Xisco Faulí 2017-05-21 16:14:51 UTC
(In reply to Grzegorz Araminowicz from comment #13)
> (In reply to Xisco Faulí from comment #12)
> > A polite ping, still working on this bug
> 
> I had little free time recently, but I'm getting back to work on the bug
> this week.

That's fantastic. Thanks for your help
Looking forward to seeing this bug fixed
Comment 15 Commit Notification 2017-05-24 17:00:49 UTC
Grzegorz Araminowicz committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=f45463d8e2bb0771ec1837d159ff98108b0047cf

tdf#93727 Support date literals in basic

It will be available in 5.4.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 16 Eike Rathke 2017-05-24 17:03:18 UTC
Note: commit notification has a hickup, master is for to-be 5.5.0
Comment 17 Xisco Faulí 2017-06-24 02:27:34 UTC Comment hidden (obsolete)
Comment 18 Grzegorz Araminowicz 2017-06-29 16:04:33 UTC
I'm no longer working on this bug. Generally date literals are working now, but parsing in scanner.cxx should be improved to support literals containing spaces.