Wrong date? Just add 3½ days

More PHP date weirdness, this time in the Cargo extension for MediaWiki:

+		// 'o' is better than 'Y' because it does not add leading
+		// zeroes to years with fewer than four digits.
+		// For some reason, though, this fails for some years -
+		// returning one year lower than it's supposed to - unless you
+		// add the equivalent of 3 days or more to the number of
+		// seconds. Is that a leap day thing? Weird PHP bug? Who knows.
+		// Anyway, it's easy to get around.
+		$yearString = date( 'o', $seconds + 300000 );

Getting a proper %DATE% variable in batch files

Windows batch files are hideous things, if one is used to *nix shell scripting. Everything feels wrong! Of course, that’s mostly just because they have a different syntax… but sometimes it’s because they are wrong.

Date formatting, for instance.

There’s an environment variable, %DATE%, that holds the current date, but it’s formatted to the current locale’s ‘short date’ definition, and so does not produce a consistent output from system to system.

Something as simple as naming files becomes a hideous contortion of detecting formats, splitting strings apart, and hopefully re-joining them in the right order. Too annoying for me, today, so I’ve taken a leap out of cmd and into java:

Firstly, compile the following Java class.

import java.text.SimpleDateFormat;
import java.util.Date;

public class date {

    public static void main(String[] args) {
        Date date = new Date();
        String out = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println(out);
    }
}
$ javac thedate.java

Then, call it from a batch file with something like this:

echo off
setlocal
java date > thedate.txt
set /p THEDATE= < thedate.txt
del thedate.txt
echo The date is %THEDATE%
endlocal

This is ridiculous, yes, but it does at least work.