"date" returns wrong format

New to FreeBASIC? Post your questions here.
skogtun
Posts: 10
Joined: Dec 22, 2010 19:53

"date" returns wrong format

Postby skogtun » Mar 13, 2018 16:46

I tried this yesterday. FreeBASIC gives an ambiguous result:

Code: Select all

$ echo print date >datetest.bas
$ fbc datetest
$ ./datetest
03-12-2018


Is this 3. December or 12. March? Only Americans will see it as the former.
Same bug in the macro __DATE__, so "fbc --version" shows a wrong (or at least ambiguous) compile date.
Luckily we have __DATE_ISO__.
dkr
Posts: 20
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Re: "date" returns wrong format

Postby dkr » Mar 13, 2018 21:58

The documentation shows the output is in the format mm-dd-yyyy

DATE DOCUMENTATION
Returns the current system date as a string

Syntax
Declare Function Date ( ) As String

Usage
result = Date

Return Value
Returns the current system date, in the format mm-dd-yyyy
skogtun
Posts: 10
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Postby skogtun » Mar 13, 2018 22:41

dkr wrote:The documentation shows the output is in the format mm-dd-yyyy

DATE DOCUMENTATION
Returns the current system date as a string

Syntax
Declare Function Date ( ) As String

Usage
result = Date

Return Value
Returns the current system date, in the format mm-dd-yyyy


So the right thing to do would be to change both the program and the documentation. It should print the date according to the locale used.
dodicat
Posts: 4831
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: "date" returns wrong format

Postby dodicat » Mar 13, 2018 23:52

For curiosity, does this work on your system?

Code: Select all

#include "crt/time.bi"
Function DateString() As String
    Dim As time_t  rawtime
    time_(@rawtime)
    Dim As tm Ptr timeinfo=localtime(@rawtime)
    Return  *asctime(timeinfo)
End Function

Print DateString
sleep
 

Here I get:
Tue Mar 13 23:51:34 2018
MrSwiss
Posts: 2476
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: "date" returns wrong format

Postby MrSwiss » Mar 14, 2018 10:41

Another approach is, write your own Date-Changer Function as below:

Code: Select all

Function CHDate() ByRef As Const String ' write protected! swiss date format
    Static As String    CH_Date, cDate  ' swiss/current (american) date

    If cDate <> Date Then   ' date change or first call, only!
        cDate = Date        ' save current american date (for comparison)
        Dim As String dd = Mid(cDate, 4, 2), _  ' part strings: dd = day,
            mm = Left(cDate, 2), yy = Right(cdate, 4)   ' mm = month, yy = year
        CH_Date = dd + "." + mm + "." + yy  ' construct CH_Date <-- change here,
    End If                  ' for own/different format! rename procedure! indi-
                            ' cating the change made ... (comments too, please)
    Return CH_Date          ' swiss style formatted Date-String
End Function
Fast and compact ...
skogtun
Posts: 10
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Postby skogtun » Mar 14, 2018 13:27

dodicat wrote:For curiosity, does this work on your system?

Here I get:
Tue Mar 13 23:51:34 2018


Yes, I get the same result
skogtun
Posts: 10
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Postby skogtun » Mar 14, 2018 13:27

MrSwiss wrote:Another approach is, write your own Date-Changer Function as below:

Code: Select all

Function CHDate() ByRef As Const String ' write protected! swiss date format
    Static As String    CH_Date, cDate  ' swiss/current (american) date

    If cDate <> Date Then   ' date change or first call, only!
        cDate = Date        ' save current american date (for comparison)
        Dim As String dd = Mid(cDate, 4, 2), _  ' part strings: dd = day,
            mm = Left(cDate, 2), yy = Right(cdate, 4)   ' mm = month, yy = year
        CH_Date = dd + "." + mm + "." + yy  ' construct CH_Date <-- change here,
    End If                  ' for own/different format! rename procedure! indi-
                            ' cating the change made ... (comments too, please)
    Return CH_Date          ' swiss style formatted Date-String
End Function
Fast and compact ...


But why should I have to do that?
MrSwiss
Posts: 2476
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: "date" returns wrong format

Postby MrSwiss » Mar 14, 2018 13:42

skogtun wrote:But why should I have to do that?
Because, we aren't all americans ?!? ;-)

Joke aside: BASIC was invented in the USA, if I remember correctly ...

There are computer languages, invented elsewhere, which might have different defaults.
Examples: Pascal, Modula ... and more (swiss made! by Niklaus Wirth)
coderJeff
Site Admin
Posts: 2524
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: "date" returns wrong format

Postby coderJeff » Mar 14, 2018 14:29

skogtun wrote:But why should I have to do that?

You don't. see Format. You may also be interested in Now function.

Date (a.k.a. Date$) mm-dd-yyyy order is due to QB origins and is consistent. I don't which FB functions might respect locale, if any.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest