I'm confused with DateTime

Hello,
I have a validation system by date that works very well, but I don’t know how to do it with the new DateTime, my code is as I put it below, could someone tell me how to redo this so I can go ahead ?

For better understanding, it records a file with the Maximum validation date and checks and writes the current date every time the software is opened in the .dll file so that the validation does not exceed the predicted time and so that turning the computer clock back does not work either.

Thanks in Advance

dim f, f2 as folderitem
dim readbin as binaryStream
dim writebin as binarystream
dim dataexpira as date
dim dataregistro as date
dim teste as boolean
dim datahoje as date
dim datareal as date
dim v1, v2, v3, v4 as string
dim parsedate1, parsedate2, parsedate3 as boolean
dim i as integer
dim b as boolean


f=specialfolder.Desktop.child("Dateok.dll")

if f.exists then
  
  'new dates
  datareal=new date
  dataexpira=new date
  dataregistro=new date
  datahoje=new date
  
  'Folderitens
  readbin=BinaryStream.open(f, false)                                
  'Read
  v1=HexRollDecypher(readbin.readpstring)
  v2=HexRollDecypher(readbin.readpstring)
  v3=HexRollDecypher(readbin.readpstring)
  v4=datareal.SQLDate
  readbin.close
  
  dataexpira.SQLDate=V1
  dataregistro.SQLDate=V2
  datahoje.SQLDate=V3
  
  expirastring="Validation: " + dataexpira.shortdate
  
  
  if v4>=v1 then 'if the date is bigger tan validated
    msgbox "Date error, contact administrator."
    writebin=f.createbinaryFile("javascript")
    writebin.writepstring HexRollEncypher(dataexpira.SQLDate)
    writebin.writepstring HexRollEncypher(dataregistro.SQLDate)
    writebin.writepstring HexRollEncypher(datareal.SQLDate)
    writebin.close
    window1.deleteaction2
    quit
  elseif v4<v3 then
    msgbox "Please Modify the date to corrent date."
    window1.deleteaction2
    quit
  else
    writebin=f.createbinaryFile("javascript")
    writebin.writepstring HexRollEncypher(dataexpira.SQLDate)
    writebin.writepstring HexRollEncypher(dataregistro.SQLDate)
    writebin.writepstring HexRollEncypher(datareal.SQLDate)
    expirastring="validation: " + dataexpira.shortdate
    writebin.close
    window2.expirastring.text=expirastring
    window2.show
  end if
  
else
  Msgbox "App dont validated"
  window1.deleteaction2
  quit
  
end if

Hi Paulo,

  1. Replace your declarations for Dates to DateTime.
  2. datareal = DateTime.Now (replaces datareal=new date)
  3. Remove the other new date lines under 'new dates
  4. Where you are using .SQLDate = use FromString e.g. dataexpira = DateTime.FromString(V1)

If you have issues post your updated code here & I’ll take a look.

HTH
Wayne

2 Likes

Hi Wayne,

I didn’t understand the part of:

  1. Remove the other new date lines under 'new dates

Could you explain that to me

You don’t need these lines as DateTime.FromString() creates a new instance of DateTime.

sorry for my ignorance but if I delete these lines how do I compare date actual, date final and date of the last opening of the App to save the .dll with these new dates ?

These 3 lines become

dataexpira = DateTime.FromString(V1)
dataregistro = DateTime.FromString(V2)
datahoje = DateTime.FromString(V3)

FromString is a Shared Method that returns a DateTime object.

1 Like

Thanks, tomorrow I will try this.

Try to move your variable declarations to the location where you actually use the variable. A declaration block hasn’t been needed for years and years.

2 Likes

Yes Beatrix,

I used this code for over 7/8 years and it always worked, so I didn’t worry about it, I will do some tests today or tomorrow and I will tell you how the problem was, I hope I can work around the problem.

Thanks for the tip !

This code should work.

dim f, f2 as folderitem
dim readbin as binaryStream
dim writebin as binarystream
dim dataexpira as DateTime
dim dataregistro as DateTime
dim teste as boolean
dim datahoje as DateTime
dim datareal as DateTime
dim v1, v2, v3, v4 as string
dim parsedate1, parsedate2, parsedate3 as boolean
dim i as integer
dim b as boolean
Var expirastring As String


f=specialfolder.Desktop.child("Dateok.dll")

if f.exists then
  
  'new dates
  datareal= DateTime.Now
  REM dataexpira=new date
  REM dataregistro=new date
  REM datahoje=new date
  
  'Folderitens
  readbin=BinaryStream.open(f, false)                                
  'Read
  v1=HexRollDecypher(readbin.readpstring)
  v2=HexRollDecypher(readbin.readpstring)
  v3=HexRollDecypher(readbin.readpstring)
  v4=datareal.SQLDate
  readbin.close
  
  dataexpira = DateTime.FromString(V1)
  dataregistro = DateTime.FromString(V2)
  datahoje = DateTime.FromString(V3)
  
  expirastring="Validation: " + dataexpira.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
  
  
  if v4>=v1 then 'if the date is bigger tan validated
    msgbox "Date error, contact administrator."
    writebin=f.createbinaryFile("javascript")
    writebin.writepstring HexRollEncypher(dataexpira.SQLDate)
    writebin.writepstring HexRollEncypher(dataregistro.SQLDate)
    writebin.writepstring HexRollEncypher(datareal.SQLDate)
    writebin.close
    window1.deleteaction2
    quit
  elseif v4<v3 then
    msgbox "Please Modify the date to corrent date."
    window1.deleteaction2
    quit
  else
    writebin=f.createbinaryFile("javascript")
    writebin.writepstring HexRollEncypher(dataexpira.SQLDate)
    writebin.writepstring HexRollEncypher(dataregistro.SQLDate)
    writebin.writepstring HexRollEncypher(datareal.SQLDate)
    expirastring="validation: " + dataexpira.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
    writebin.close
    window2.expirastring.text=expirastring
    window2.show
  end if
  
else
  Msgbox "App dont validated"
  window1.deleteaction2
  quit
  
end if

Hello Wayne,

First I would like to thank you very much for your help in this case, I am very grateful!

In the code you sent me I’m having a problem with line :
dataexpira = DateTime.FromString(V1)

Gave me an error:
Xojo.Core.BadDataException - String must have a known encoding

Would you know why that ?

You need to set the encoding in HexRollDecypher with DefineEncoding. After you decypher the bytes (which is what I assume you are doing based on the function name), call DefineEncoding on the return string before you return it.

1 Like

Hello Tim,

Is there a place where I look for documentation to see how to do this because I don’t know how to proceed ?

I generated this code many years ago and I don’t know where to look now.

Have a look at http://docs.xojo.com/DefineEncoding

1 Like

What is the return statement from your HexRollDecypher function? Post that one line of code.

1 Like

I am very grateful to Wayne Golding, Tim Hare and Beatrix who helped me, works fine with the Wayne code with the changes indicated.

Thank you very much, I am immensely grateful !