how to set the dates?

New to FreeBASIC? Post your questions here.
ptitjoz
Posts: 32
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Post by ptitjoz »

Hello
I am testing other solutions...

By retrieving the system date
it should be a procedure or a function but I do not know (yet) to do it.
Is it a good idea ?
Regards

Code: Select all

'Test recupération date sous Linux
Dim DateSystem as string
shell "date +%d/%m/%Y > date.tmp"
Open "date.tmp" For Input  As #1
Line Input #1,DateSystem
Close #1
shell "rm date.tmp"

Print "date system : ";DateSystem
dodicat
Posts: 7967
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: how to set the dates?

Post by dodicat »

date is the system date according to the fb help files.

Code: Select all

'Test recupération date sous Linux

Function systemdate() As String
    Dim DateSystem As String
    Shell "  date /T > date.tmp"
    Sleep 5 'tiny time lag to ensure date.tmp is on the disc
    Open "date.tmp" For Input  As #1
    Line Input #1,DateSystem
    Close #1
    Shell "rm date.tmp"
    Return DateSystem
End Function

Print systemdate
print date
sleep
  
My result:

Code: Select all

08/04/2018
04-08-2018
  
fxm
Moderator
Posts: 12066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

Or use "OpenPipe":

Code: Select all

Dim As String s

Open Pipe "date /T" For Input As #1
Line Input #1, s
Close #1

Print s
Sleep
ptitjoz
Posts: 32
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Post by ptitjoz »

Thank you for your constructive responses!
I'm running Linux and date /T does not work (I guess it's a DOS command?)

Does Open Pipe write in memory? because there is no generated file.

with this code it works well

Code: Select all

' Test recupération date sous LINUX !!

Function systemdate() As String
    Dim s As String
    Open Pipe "date +%d/%m/%Y " For Input As #1
    'Open Pipe "date /T" For Input As #1
	Line Input #1,s
    Close #1
    Return s
End Function

Print "date origine   ";date
Print "date nouvelle ";systemdate()
fxm
Moderator
Posts: 12066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

ptitjoz wrote:Does Open Pipe write in memory? because there is no generated file.
Yes.
When using PIPE on a shell command, the output of this command is not routed to the screen, but redirected to a file buffer that can be accessed by [LINE] INPUT #.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: how to set the dates?

Post by badidea »

I'm running Linux and date /T does not work (I guess it's a DOS command?)
For linux command line, typ date --help for all options. (there are many).
I would go for a platform-independent solution however.
ptitjoz
Posts: 32
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Post by ptitjoz »

Hello
badidea wrote:
I'm running Linux and date /T does not work (I guess it's a DOS command?)
For linux command line, typ date --help for all options. (there are many).
I would go for a platform-independent solution however.
hello, that's why I put this syntax date +%d/%m/%Y that I had in man date

Regards
dodicat
Posts: 7967
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: how to set the dates?

Post by dodicat »

The most versatile is Format, but does it work in Linux?

Code: Select all

 


#include "vbcompat.bi"
#define DateTime Format(Now, "dddd-dd-mmmm-yyyy     hh-mm-ss")



print DateTime

sleep
  
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: how to set the dates?

Post by badidea »

ptitjoz wrote:hello, that's why I put this syntax date +%d/%m/%Y that I had in man date
Ok, check.
dodicat wrote:The most versatile is Format, but does it work in Linux?
Yes, I get:
Sunday-08-April-2018 22-14-36

"-" for time is a bit weird.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Post by MrSwiss »

dodicat wrote:The most versatile is Format, but does it work in Linux?
I've just done a speed test, one of the tested methods is Format( Now, "dd/MM/yyyy" )
and, the most surprising *outcome* was, Format's return = 08.04.2018 and not:
08/04/2018, as expected (after consulting FB-Doc).
It seems that, after testing with both current WIN-FBC versions x32 (GAS) and, x64 (GCC),
that Format isn't working properly, with *some of the options*, at least!!

Ohhh, btw. it was also, by far, the slowest method ... (which was expected!)

dates_timing_test.bas:

Code: Select all

' dartes_timng_test.bas -- 2018-04-08, MrSwiss
'
' compile: -s console (or use 'quick run' from IDE)
'
' IMPORTANT: never ever use compiler's optimizers, in any timing-tests !!!
'            (it might 'optimize out' parts, of the following code!)
'
#Include "vbcompat.bi"

Declare Function MyDate( ByVal nChr As UByte=0 ) ByRef As Const String

Dim As Double   t, te, tt1, tt2, tt3    ' timer var's
Dim As UInteger n, z                    ' loop counter's
Dim As String   sam, sch, sfr           ' target string's (tests)

#Define min     1                       ' timing loop start (n & z loop)
#Define max     10000                   ' timing loop end (n loop)
#Define nRun    100                     ' number of run's (z loop) end

Const As UByte  delim = Asc("/")        ' delimiter defintion

' inform user: to wait (until tests concluded)
Print "please wait ... test in progress"
Print String(32, "~")
Print

' we don't want to measure the empty loop timing <-- sensitive to 'optimizations'!
te = Timer
For n = min To max          ' optimizers 'love it', to 'kick out' empty loops, _
Next                        ' to them, they 'never have a purpose' (wrong here!)
te = Timer - te             ' establish: empty loop time (te)

' all print statements, removed from timing loop's (corrupting results)
' increase (t) start, by empty loop time (te) = decrease to 'real' runtime

For z = min To nRun         ' z loop's time isn't considered (not needed!)
    t = Timer + te          ' adding (te) to start timer (t) 
    For n = min To max
        sam = date          ' actual procedure under test
    Next
    tt1 += Timer - t        ' pure 'real' runtime totalizer (tt1)

    t = Timer + te          ' adding (te) to start timer (t)
    For n = min To max
        sch = MyDate( delim )   ' actual procedure under test
    Next
    tt2 += Timer - t        ' pure 'real' runtime totalizer (tt2)

    t = Timer + te          ' adding (te) to start timer (t)
    For n = min To max
        sfr = Format( Now, "dd/mm/yyyy" )   ' actual procedure under test
    Next
    tt3 += Timer - t        ' pure 'real' runtime totalizer (tt3)
Next

' show results to user ...
print "datatype"; Tab(31); "time average (100 run's * 10'000)"
Print String(64, "-")
print "String's" : Print String(18, "-")
' the following calculations don't influence the test itself!
print sam; Tab(30); (tt1 / nRun) * 1000; Tab(50); "mS -- Date"
print sch; Tab(30); (tt2 / nRun) * 1000; Tab(50); "mS -- MyDate( delim )"
Print sfr; Tab(30); (tt3 / nRun) * 1000; Tab(50); "mS -- Format( Now, "; """dd/mm/yyyy"""; " )"

Print String(64, "-")
Print
print "... done ... press a key, to exit ! ";
Sleep
' ===== End test/demo code =====

' ----- start implementation -----
Function MyDate( _      ' day/month swap (optional: 'user defined' delim.)
    ByVal nChr  As UByte=0 _    ' default = american delim.
    ) ByRef As Const String     ' reference to cDate (Static, read only!)
    Static As String   cDate, aDate ' local static var's
    ' check for: delimitter- or date-change (update not made otherwise!)
    If nChr <> Asc(Mid(cDate, 3, 1)) OrElse aDate <> Date Then
        aDate = Date            ' update aDate (american, for comparison!)
        If nChr > 0 Then        ' 'user defined' delim.? --> build cDate
            cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
                    Chr(nChr) + Right(aDate, 4)
        Else                    ' default, american delim. used
            nChr  = Asc(Mid(aDate, 3, 1))   ' get delim. --> build cDate
            cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
                    Chr(nChr) + Right(aDate, 4)
        End If
    End If
    ' return pre-made or newly created cDate-String (on change, only!)
    MyDate = cDate  ' the same as: Function = cDate / Return cDate
End Function
' ----- end implementation -----    ' ----- EOF -----
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: how to set the dates?

Post by adele »

hi.

@Badidea:

"Sunday-08-April-2018 22-14-36
"-" for time is a bit weird."
Not, if you want the output as part of a file name :)

@mr Swiss

Speed might be an issue if you ask maybe 100 times per second for the
current DateTime.
As soon as you write to Screen or a file, the writing of the string possibly
takes more time than the creation of the string itself.
So, we shouldn't exaggerate tuning. It is a waste of time.

Adi
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Post by MrSwiss »

@adele,

main issue in my last post, was the incorrect return of: Format( Now, "dd/MM/yyyy" )!

Speed optimisations: your opinion is, as good or bad, as mine.
(I don't consider it, a waste of time.)

My moto is: do it, as well as possible, or don't bother, to do anything, at all ...
fxm
Moderator
Posts: 12066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

MrSwiss wrote:main issue in my last post, was the incorrect return of: Format( Now, "dd/MM/yyyy" )!
No problem for me (Windows 10) with fbc-32bit (gas and gcc) and fbc-64bit (gcc).
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Post by MrSwiss »

fxm wrote:No problem for me (Windows 10) with fbc-32bit (gas and gcc) and fbc-64bit (gcc).
If you didn't use my testing code, then that means: "nothing ... "

(It is IMPORTANT: to have a *apple* vs. *apple* comparison!)
Post Reply