Best scala questions in May 2012

What happened between March 28th and March 29th, 1976 with the java.util.GregorianCalendar?

21 votes

Trying to use the GregorianCalendar, I got stuck on a singularity while computing the number of days since a particular date. In the scala interpreter, I entered :

scala>import java.util.GregorianCalendar
scala>import java.util.Calendar
scala>val dateToday = new GregorianCalendar(2012,Calendar.MAY,22).getTimeInMillis()
dateToday: Long = 1337637600000
scala>val days1 = (dateToday - (new GregorianCalendar(1976,Calendar.MARCH,28).getTimeInMillis())) / (1000*3600*24)
days1: Long = 13203
scala>val days2 = (dateToday - (new GregorianCalendar(1976,Calendar.MARCH,29).getTimeInMillis())) / (1000*3600*24)
days2: Long = 13203

I don't know if the fact that 1976 is a leap year matters, but days1 and days2 should have been separated by 1. This is the only moment in history since 1970 that this singularity happens.

Wanting to know what is going on, I compute the difference between the two dates previously mentionned, and it gives me only exactly 23 hours of difference ! What happened on that date ? Wikipedia apparently says nothing about it.

And even more important, how to compute the real number of days since a particular date ?

If the option of a float division is not available, the best answer I can think of is to add one hour (1000*3600) while computing a difference between two days:

scala> (dateToday - (new GregorianCalendar(1976, Calendar.MARCH, 28).getTimeInMillis()) + 1000*3600) / (1000 * 3600 * 24)
days1: Long = 13204
scala> (dateToday - (new GregorianCalendar(1976, Calendar.MARCH, 29).getTimeInMillis()) + 1000*3600) / (1000 * 3600 * 24)
days1: Long = 13203

It should work for every date, as you do not suffer from leap hours anymore.

Can you use antixml to create xml documents?

8 votes

there are a few examples for using Anti-Xml to extract information from XML documents, but none that I could find of using Anti-Xml to create XML documents. Does Anti-Xml support creating documents, or should I use another library for this (which one?). Does anyone have an example of creating an XML document with Anti-Xml?

Yes, you can build (and serialize) XML documents:

import com.codecommit.antixml._

val doc = Elem(None, "doc", Attributes(), Map(), Group(
  Elem(None, "foo", Attributes("id" -> "bar"), Map(), Group(Text("baz")))
))

val writer = new java.io.StringWriter
val serializer = new XMLSerializer("UTF-8", true)

serializer.serializeDocument(doc, writer)

You can also use Anti-XML's zippers to do some interesting editing tricks:

val foos = doc \ "foo"
val newFoo = foo.head.copy(children = Group(Text("new text!")))
val newDoc = foos.updated(0, newFoo).unselect

Now newDoc contains the edited document:

scala> newDoc.toString
res1: String = <doc><foo id="bar">new text!</foo></doc>

The Zipper that doc \ "foo" returns is different from a NodeSeq in that it carries information about its context, which allows you to "undo" the selection operation done by \.


Update in response to ziggystar's comment below: if you want something like Scala's XML literals, you can just use convert on any scala.xml.Elem:

val test: com.codecommit.antixml.Elem = <test></test>.convert

I'd assumed the question was about programmatic creation.