Liquid Digital Lab

Director Tutorial - Counting Down To A Launch Date

Posted 3 years, 3 months ago by Simon

Counting down to a date, such as a product or event launch, in Director is pretty easy using the global systemdate property, however if you want to express the time remaining as anything other than ‘X days to go’ you need to do some number crunching of your own.

This tutorial assumes some basic knowledge of using Director (any version) and fairly basic lingo.

Just to go over the basics, the systemdate property will return something like:

put the systemDate
– date( 2007, 5, 29 )

The date shows year, month, and date. There are a few variations on the command:

put the systemDate.year
– 2007

put the systemDate.month
– 5

put the systemDate.day
– 29

put the systemDate.seconds
– 37400

37400 seconds, which is to 10:23.

Simple differences in dates can be calculated with commands such as:

launchDate = date (2010, 1, 20)
put launchDate - the systemDate
– 967

This tells me that, at the time of writing this, there are only 967 days until a ’significant’ birthday is upon me on 20th Jan 2010.

Of course, 967 days doesn’t mean a great deal to most of us. We’re used to expressing time spans of this length in terms of years, months and days. And, if we want to be really precise, in hours, minutes and seconds.

This isn’t as straightforward as you’d first think, however, due to the Gregorian calendar system that we use, which doesn’t have the year broken into months of equal lengths. If months were equal we could simply divide the number of days to go by the number of days in each months and we’d have our answer. So how do we take 967 days and turn it into years, months and days? We can’t even divide it by 365 to see how many years there are due to the inconvenience of leap years.

When faced with a task like this in Director, or any other language, where there isn’t a built in function ready to solve the problem for you, I always find the best policy is to think how you’d do the maths manually, and then apply that process to your programming.

If I wanted to work this out in my head, I’d work out the years first by adding one each time until I reach the target year, knowing that today’s month is already past the target month, I know to discount this year, so 2008, 2009, 2010 - that’s 2 whole years. Months? I know that today’s date (the 29th) is after the 20th so I can’t include May in my counting, so from June to January - July, August, September, October, November, December, January - 7 months. Days?  Add the 2 days left in this month to the 20 in January. That’s 2 years, 7 months and 22 days to go.

Simply converting the target time to seconds after midnight and subtracting the current number of seconds after midnight, and then converting into hours and minutes, we can work the hours and minutes.

Some factors that need consideration are whether there’s only part of a day left to go - i.e. is the time now later in the day than the target time, and is today’s day number later in the month than the target day number. We take care of the latter by storing the days left until the end of this month in a variable, and then counting up the months from the 1st of each one - this will avoid lingo flipping out when you add a month to something like 30th Jan and having a resulting date that simply can’t exist. We also have to consider that the code must deal with the scenario of less than a day to go or the time has already passed. And to finish the job, we should try and provide an answer that’s grammatically correct - outputting “1 days and 1 hours to go” can look pretty stupid.

All very interesting, but how do we do this in lingo? Quite simply, we let the computer do the counting for us, following the same logical approach, and throw in a few checks to make cover all the angles. The handler below may seem a little long-winded, but I’ve done this to make it easier to understand. The target date and time is hard-coded, whereas normally you’d allow scope to pass the date to the handler to make it more flexible. To test this script n Director, just type

Put doCountdown ()

into the message window (having first pasted the script into a movie script in Director, of course). It’ll return the answer in your message window. You can also try a version in your shockwave-enabled browser which allows you to put in your own date (there is only limited error trapping!).

on DoCountdown ()�
  — we’ll use the variable pms so we can include the length of time taken to work out the answer in our result
  pms = the milliseconds�
  launch = date (2010, 1, 20) — my XXth birthday
  launchHour = 19 — 7 pm
  launchMinutes = 30 — 30 mins past the hour, when I’ll get home from work and celebrate
  nowDate = the systemDate — the current date
  — look at the hours/mins first
  startSecs = (launchHour * 60 * 60) + (launchMinutes * 60)
  nowSecs = the systemdate.seconds�
  if nowSecs > startSecs then
    nowDate = nowDate + 1  — move on a day to compensate for being after launch time
    — 86400 is the number of seconds in a day
    diffSecs = startSecs  + (86400 - nowSecs) — add 19:30’s worth of seconds to the secs remaining today til midnight
    hrs = diffSecs / 3600
    mins = (diffSecs - (hrs * 3600)) / 60
  else
    diffSecs = startSecs - nowSecs
    hrs = diffSecs / 3600
    mins = (diffSecs - (hrs * 3600)) / 60
  end if
  timeString = hrs & ” hours and ” & mins & ” mins”�
  — now the years/months/days
  daysLeft = launch - nowDate — the number of days to go�
  if daysleft >= 0 and diffSecs >= 0 then
    yrs = 0
    mnths = 0
    days = 0
    prevYr = nowDate.year
    prevMnth = nowDate.month
    prevDay = nowDate.day  �
    — if the current day is greater than the target day, then we need to store the days remaiming in the current month
    — and begin the count forwards from day 1 of the next month
    daysToAdd = 0
    if nowDate.day > launch.day then    �
      repeat while (nowdate.day <> 1) and (nowDate < launch)�
        nowDate = nowDate + 1 — add one day
        daysToAdd = daysToAdd + 1
      end repeat
    end if
    repeat while nowDate < launch
      tempdate = duplicate (nowDate) — we’ll increase this tempory variable,
      — increase it and see if we overstep the target date    �
      if tempDate.month < 12 then
        tempDate.month = tempDate.month + 1
      else
        tempDate.month = 1
        tempDate.year = tempDate.year + 1
      end if
      if tempDate < launch then
        nowDate = duplicate (tempDate) — we’re still over a month before the target date so we can continue the count
        mnths = mnths + 1 — keep a tally of months counted so far
      else
        — less than a month to go, so work out how many days remain
        days = (launch - nowDate) + daysToAdd
        exit repeat
      end if �
    end repeat �
 if days < daysToAdd then
    days = days + daysToAdd
  end if
    yrs = mnths/12 — should be obvious! The number of years to go
    mnths = mnths - (yrs*12)  — again, the obvious way to see how many months we have to go�
    — the maths is now done, and we just need to formulate a gramatically correct answer
    — make sure the plural usage is correct
    case (yrs) of
      0: yrString = “”
      1: yrString = “1 year “
      otherwise: yrString = yrs & ” years “             �
    end case
    case (mnths) of
      0: mnthString = “”
      1: mnthString = “1 month “
      otherwise: mnthString = mnths & ” months “             �
    end case
    case (days) of
      0: dayString = “”
      1: dayString = “1 day “
      otherwise: dayString = days & ” days “              �
    end case  �
    return ( yrstring &  mnthstring & daystring & timeString & ” to go. Worked out in ” & the milliseconds - pMs & ” milliseconds”)
  else
    return (”The date has passed!”)
  end if
end

Leave a Comment

Three D Clock

Classic clock built in processing

Posted by Lloyd - Comments (0)

This Happened #5

Lloyd gets chance to go along to the 5th gathering of This Happened

Posted by Lloyd - Comments (0)

Tom survives in one piece!

Tom makes it back to the office through 19 peaks over 149 miles, 60 flood warnings, 60mph gusts and 48hours of torrential rain, all in the name of MS Society, Wheels4Life and a weekend to remember!

Posted by Tom - 3 Comments (3)

Liquid goes Coast to Coast

Inspired by his managers previous fundraising wetsuit exploits, our newest designer Tom will be tackling the coast to coast bike ride this September.

Posted by Tom - Comments (1)

Liquid Artwork

Have a look at the artwork we created for the front office

Posted by Lloyd - 3 Comments (3)

Decisions, decisions… A Bit More Game AI

Game AI goes a few steps forwards, and 2000 years into the past…

Posted by Simon - 3 Comments (3)

Ever Won The Lottery?

Winning The Big One!
I recently read somewhere that winning the UK national lottery is easy. All you have to do is …

Posted by Simon - Comments (1)

3DS Max - Basics and Blueprints

The start of a series of tutorials in 3ds max from Liquids latest addition to the creative team.

Posted by Lloyd - 9 Comments (9)

Sound Reactive Sketch

We created this experimental Music Visualiser in processing & minim sound library.

Posted by Lloyd - 2 Comments (2)

Pathfinding in Games - Using A* in Director

Discover the joys of the A* pathfinding algorithm in our retro Director maze

Posted by Simon - Comments (1)

Colour Clock Screensaver

We’ve made an animated clock screensaver. You can have it too.

Posted by Ben - 2 Comments (2)

Using the Flash Tween Class

Trying to create realistic bouncing balls or elastic objects in Flash? Ben Stevens shows us the easy way to code motion using the Flash Tween Class.

Posted by Ben - 6 Comments (6)

A Jungle Jumble Using Lists in Director

What do you get if you cross a monkey, an elephant and a giant chicken-thing? Simon Edwards tries to explain using lists in Director.

Posted by Simon - 3 Comments (3)

Lloyd Survives the 2007 London Triathlon

Liquid’s very own budding triathlete successfully completes the Michelob ULTRA London Triathlon in a time of 2 hours and 42 minutes. Find out how he got on.

Posted by Lloyd - Comments (1)

Simple AI in Director

Simon Edwards discusses some of the techniques that developers call upon when programming games, particularly for defining logic that the computer will use for taking a turn in a 2-player game scenario.

Posted by Simon - Comments (0)

Ruby on Rails Tutorial - An Online DVD Catalogue

John Cove takes us through the process of making an online catalogue using RoR.

Posted by John - Comments (0)

Why write an ActiveX control?

Howard Peters discusses the advantages of using ActiveX, and explains how to create your own simple ActiveX control.

Posted by Howard - Comments (0)

Nichola’s Placement Year

Nichola, our resident student placement, has now been with us for a whole year and will be leaving us soon. So we asked her what it’s been like to work with the Liquid team.

Posted by Nichola - Comments (0)

Kinetica Soundwaves Exhibition

Exhibition visit

Posted by Ben - Comments (0)

PHP Tutorial - Creating a Quiz

John Cove explains how to use HTML and PHP to create an online quiz.

Posted by John - Comments (0)

Director Tutorial - Vector Font Outlines

Simon Edwards demonstrates an undocumented Director feature enabling you to map out a string of text in a vector cast member.

Posted by Simon - Comments (0)

HDR Photography

HDR (High Dynamic Range), is a software based, photographic technique that extracts tonal information from multiple exposures of the same shot. If you exposure bracket the same shot, you can merge all 3 images into one, then, using the data from all 3, create one output file which preserves accurate details from all exposures, bringing out highlights from the shadows.

Posted by Darren - Comments (0)

Happy Birthday To Us!

Liquid Digital Ltd has reached the end of its first financial year. There’s no time for cake and a candle though - there are some big things on the horizon for the first quarter of Year 2. Watch this space for news soon.

Posted by Simon - Comments (0)

Director Tutorial - Counting Down To A Launch Date

Tutorial written by our resident Director Guru, Simon Edwards.

Posted by Simon - Comments (0)

Lloyd in a wetsuit

Our creative leader will be braving the elements by competing in the Michelbob ULTRA London Triathlon on 4th August 2007.

Posted by Ben - Comments (0)