"date" returns wrong format

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

"date" returns wrong format

Post by skogtun »

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: 40
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Re: "date" returns wrong format

Post by dkr »

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: 13
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Post by skogtun »

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: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: "date" returns wrong format

Post by dodicat »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: "date" returns wrong format

Post by MrSwiss »

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: 13
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Post by skogtun »

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: 13
Joined: Dec 22, 2010 19:53

Re: "date" returns wrong format

Post by skogtun »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: "date" returns wrong format

Post by MrSwiss »

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: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: "date" returns wrong format

Post by coderJeff »

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.
Post Reply