VO code translation to X#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO code translation to X#

Post by wriedmann »

Hello,
I have such code in my Visual Objects code library:

Code: Select all

local nCast as dword
local dDate as Date

dDate := Today()
nCast := dword( _cast, dDatum )
I don't like that code, but how can I translate it to a more correct form?
Strange enough that calling it in X# code it returns the same values as in VO....
Thank you very much!
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

VO code translation to X#

Post by robert »

Wolfgang,

what do you want to do with nCast ?
And why is it strange that we are returning the same numeric value ?
We are returning the Julian date number with the same algorithm that VO uses:

https://github.com/X-Sharp/XSharpPublic ... e.prg#L291

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO code translation to X#

Post by wriedmann »

Hi Robert,

thank you very much!
And why is it strange that we are returning the same numeric value ?
Casts, specially from non-numeric values to numeric one are really a bad thing, full of side effects, when it comes to 32/64 bit code, so I try to remove them whenever possible.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO code translation to X#

Post by wriedmann »

Hi Robert,

sorry, this is the entire VO code:

Code: Select all

function Week( dDate as date ) as int pascal
  local nJ as dword
  local nD4 as dword
  local nL as dword
  local nD1 as dword
  local nDiff as int

  nJ  := dword( _cast, dDate )
  nD4 := ( ( ( nJ + 31741 - ( nJ % 7 ) ) % 146097 ) % 36524 ) % 1461
  nL := Integer( nD4 / 1460 )
  nD1 := ( ( nD4 - nL ) % 365 ) + nL
  nDiff := Integer( nD1 / 7 ) + 1
  return nDiff
and this the X# version (not using the X# runtime, but compatible to my VO code):

Code: Select all

static method Week( self dDatum as DateTime ) as int
  local nJ as int
  local nD4 as int
  local nL as int
  local nD1 as int
  local nDiff as int

  nJ	:= ( dDatum:Date - DateTime{ 1901, 1, 1 } ):Days + 2415386
  nD4 := ( ( ( nJ + 31741 - ( nJ % 7 ) ) % 146097 ) % 36524 ) % 1461
  nL := nD4 / 1460
  nD1 := ( ( nD4 - nL ) % 365 ) + nL
  nDiff := ( nD1 / 7 ) + 1
  return nDiff
Thank you again!
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

VO code translation to X#

Post by robert »

Wolgang,

In this case both DWORD(_CAST, dDate) and (DWORD) dDate will call the same code and are quite harmless.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO code translation to X#

Post by wriedmann »

Hi Robert,

thank you very much!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply