Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeltaUnit for YearsBC not resolving offset correctly? #383

Closed
benhutchison opened this issue Nov 12, 2022 · 8 comments · Fixed by #388
Closed

DeltaUnit for YearsBC not resolving offset correctly? #383

benhutchison opened this issue Nov 12, 2022 · 8 comments · Fixed by #388

Comments

@benhutchison
Copy link

To experiment with the DeltaQuantity feature, I created a DeltaUnit for years BC (obviously only approximate given all the date irregularities in calendars). This differs from the Fahrenheit example in that both the sign and the offset of the unit are negative. When I convert 100BC back into an Instant, it shows as 1870. So. it appears as if the conversion factor, but not the offset, worked as expected.

import coulomb.*
import coulomb.syntax.*
import algebra.instances.all.given
import coulomb.ops.algebra.all.given
import coulomb.policy.standard.given
import scala.language.implicitConversions
import coulomb.units.time.{*, given}

object bc:
  final type YearsBC
  given ctx_unit_YearsBC: define.DeltaUnit[YearsBC, -31536000 * Second, -62133800000L, "years BC", "BC"] = define.DeltaUnit()

import bc.*
import coulomb.units.javatime.*
import coulomb.units.javatime.conversions.all.given

@main def testBC =
  println(new java.text.SimpleDateFormat("dd MM yyyy G").parse("01 01 100 BC").toInstant)

  //1870-01-25T00:00:00Z
  println(100.withEpochTime[YearsBC].toInstant)
@erikerlandson
Copy link
Owner

deleted my previous comment, it might be wrong

@erikerlandson
Copy link
Owner

@benhutchison
Copy link
Author

benhutchison commented Nov 12, 2022 via email

@erikerlandson
Copy link
Owner

yes, the 1870 part is because I was (incorrectly) side-stepping the proper DeltaUnit conversions, which include offset. So it is subtracting 100 years from 1970. instead of 2070 years.

One other thing, the offset in your definition should be in years, so 1970:

given ctx_unit_YearsBC: define.DeltaUnit[YearsBC, -31536000 * Second, 1970, "years BC", "BC"] = define.DeltaUnit()

I have another minor type bug I think I need to fix as well. I may add your example as a unit test in a separate PR, to ensure this is working.

@benhutchison
Copy link
Author

benhutchison commented Nov 12, 2022 via email

@erikerlandson
Copy link
Owner

I'm not very familiar with java time structures, or what their behavior is for "BC" but I think this might be right:

scala> 100d.withEpochTime[YearsBC].toInstant
val res1: java.time.Instant = -0099-05-18T00:00:00Z

@benhutchison
Copy link
Author

Looks right to me. Is the only change here the use of Double, ie 100d?

@erikerlandson
Copy link
Owner

The main change is that I had to fix some of my earlier hackery. It works for Int as well.

scala> 100.withEpochTime[YearsBC].toInstant
val res2: java.time.Instant = -0099-05-18T00:00:00Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants