Dizwell Informatics

News from Nowhere

Work out Yesterday in Solaris

In Solaris, if you want to know when yesterday was, there’s no simple way of doing it, with built-in switches or command-line paramters. Instead, you have to resort to trickery. Basically, you have to work out what the time would be in a timezone that is a day behind where you are.

 

So assume the current date is 15th July, 2016:

$ date
Fri Jul 15 10:56:48 AEST 2016

That is on a server that’s configured to be in Australian Eastern Standard Time, which is 10 hours ahead of GMT. (Australia can also be 11 hours ahead at times, but leave that aside for now).

To get to ‘yesterday’, you basically need to take 24 hours off that time -and you’re already 10 hours ahead of GMT, so if you say you’re in a timezone that is 14 hours behind GMT, that would get you the necessary 24 hours backwards. So:

$ TZ=GMT+14 date
Thu Jul 14 10:59:40 GMT 2016

By artifically setting your timezone to something that adds up to 24 hours behind your current local time, you get yesterday’s date and time returned. (Incidentally, you use “+14”, not “-14”, because Solaris uses Posix conventions where “+” in this context means “west of” and “-” means “east of” …and you want a time which is west of London, so that you get a time which is behind GMT.

Obviously, I need to change to “+13” when we get 11 hours ahead of London, otherwise I’ll be taking off 25 hours from my current local time. At 10AM local time, that probably won’t matter all that much -I’d still get the right day returned. But if the maths and the local time got into the perfectly wrong alignment, you might get a day returned which is the day before yesterday!

Personally, I use these tricks in scripts which email out reports to users, with subjects that say things like ‘Banking report – 14th July 2016‘. So, provided I get the right day component, I don’t much mind if the hour component is one out either way.

In my bash scripts, therefore, I will usually have something like this:

YESTERDAY=$(TZ=GMT+14 date +%d-%m-%Y)
mailx -s "Banking Report - $YESTERDAY"

That gets me the email subject I need, without having to fuss about the precise clock time.

 

In Linux, of course, things are rather simpler, because its GNU date utility can take a ‘yesterday’ argument:

hjr@britten ~ $ date --date='yesterday'
Thursday 14 July 11:08:59 AEST 2016

You might also be able to install GNU utilities on your Solaris boxes …but I don’t have that luxury in the production environment I have to use! So fiddling with time zones it must be 🙂