ISO-Date provider? (in next FBC-release)

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

ISO-Date provider? (in next FBC-release)

Post by MrSwiss »

Hi,

after including new intrinsics like: __BUILD_DATE_ISO__ how about including a ISO-Date provider ...
This means, the user has the choice to either use: Date (USA-Date format) or: ISOD (ISO-Date format)
to do as s/he pleases, in their own code.

Advantages (it is of course "syntactic sugar" to a certain extent):
especially beginners might profit from such implementation however,
after a long time of having to exclusively deal with a clear minority favoring imlementation
I'm of the opinion, that it's high time to really go: international.

I'm proposing the following code (the "name" itself might be a point of debate, however):

Code: Select all

' ISOD_Func.bas -- (c) 2021-03-10, MrSwiss
'
' compile: -s console
'

Declare Function ISOD() ByRef As Const ZString  ' ISO-Date provider

' ===== main =====
Print "USA-Date:  "; Date               ' just show the difference
Print "ISO-Date:  "; ISOD               ' REPLACEMENT for 'built-in' USA-Date
Print Chr(10)                           ' 2 x LineFeed (aka: LF)
Print "... done ... ";                  ' no LF (cursor remains at line end)
Sleep
' ===== end main =====

' implementation ...
Private Function ISOD() ByRef As Const ZString  ' ISO-Date provider
    Static As ZString * 11  idt, adt    ' keeps data in-between calls
    
    If Date <> adt Then                 ' only update on change or first call
        adt = Date                      ' update comparator content
        idt = Right(adt, 4) + "-" + _   ' year (first in ISO)
              Left(adt, 2) + "-" +  _   ' month (second in ISO)
              Mid(adt, 4, 2)            ' day (last/third in ISO)
    End If
    
    Return idt                          ' reference to internal static variable
End Function
' end implementation
' ----- EOF -----
This is clearly without the code in the main - end main section (just for testing).
Instead of ISOD it could alternatively and more verbose be called: ISODate, or similar.

Thoughts? Comments?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: ISO-Date provider? (in next FBC-release)

Post by grindstone »

What about an appropriate preprocessor?

Code: Select all

#DATE US
#DATE ISO
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ISO-Date provider? (in next FBC-release)

Post by MrSwiss »

This is NOT about preprocessor (those we've done already, reread first post).

It's about a alternative to the: Date (statement, gives US-Date); ISOD (statement, gives ISO-Date)

For general use, in ordinary code ...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: ISO-Date provider? (in next FBC-release)

Post by jj2007 »

On Windows, GetDateFormat is worth a look:

Code: Select all

  PrintLine "Yesterday was ", fDate$(-1, "MMMM dd, yyyy")
  PrintLine "Today is ", fDate$(0, "dddd dd MMMM yyyy")
  PrintLine "In the USA, that is ", fDate$(0, "yyyy/MM/dd")
  PrintLine "Tomorrow will be ", fDate$(1, "ddd dd.M.yy")
  PrintLine "One week from now: ", fDate$(7, "dd.MM.yy")
  PrintLine fDate$(0, "dddd dd MMMM yyyy"), ", exact time: ", fTime$(0, "HH:mm:ss.fff")	; long format including milliseconds
  Print fDate$(0, "dddd dd MMMM yyyy ", russian), fTime$(0), Str$(" (мы находимся в ИСО неделе %i)", IsoWeek())

Code: Select all

Yesterday was March 10, 2021
Today is Thursday 11 March 2021
In the USA, that is 2021/03/11
Tomorrow will be Fri 12.3.21
One week from now: 18.03.21
Thursday 11 March 2021, exact time: 16:10:35.418
четверг 11 марта 2021 16:10:35 (мы находимся в ИСО неделе 10)
Last edited by jj2007 on Mar 11, 2021 15:24, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ISO-Date provider? (in next FBC-release)

Post by MrSwiss »

This is supposed to be OS independent ...
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: ISO-Date provider? (in next FBC-release)

Post by coderJeff »

FORMAT? It's supposed to work on all targets.

Code: Select all

#include "vbcompat.bi"
print format( now(), "yyyy-mm-dd" )
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ISO-Date provider? (in next FBC-release)

Post by MrSwiss »

Well, not exactly the intended replacement (alternative) to Date ...

(should be "built-in" like Date, IMO)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: ISO-Date provider? (in next FBC-release)

Post by jj2007 »

FORMAT seems pretty close to the GetDateFormat() results shown above, with one significant difference:

Code: Select all

PrintLine "In the USA, that is ", fDate$(0, "yyyy/MM/dd") -> In the USA, that is 2021/03/12
print "In the USA, that is "; format(now(), "yyyy/MM/dd") -> In the USA, that is 2021.03.12
Post Reply