4

I've read just about every blog post on this topic that I can get my hands on, but just getting no joy.

I have a site running on a server in Perth, Australia (shared hosting, so server time is set to Perth time). But I need all activity to be recorded in Melbourne, Australia timezone. For example, if a record is written to the database, I want to record what time that occured in Melbourne, not perth (e.g. forum topic posted). I can't do a straight DateDiff as Melbourne has Daylight Savings, and Perth doesn't. Plus I figured while I am at this, I may as well come up with an approach that can give me the Current Melbourne (or anywhere) time regardless of the timezone of the server it is running on.

This task has turned into a much, much bigger challenge than I would have expected.

What I have come to is this:

    timezoneClass = createObject( "java", "java.util.TimeZone" );
    melbourneTimezone = timezoneClass.getTimeZone(javaCast( "string", "Australia/Melbourne" ));
    melbourneCalendar = createObject( "java", "java.util.GregorianCalendar" ).init(melbourneTimezone);
    melbourneTime = melbourneCalendar.getTime();

    writeDump(melbourneTime);

.. and from what I can tell, this should give me what I need, but it doesn't.. when I dump out 'melbourneTime', it just gives me the server time.

Can anyone give me any pointers. Am I on the right path, or is ther a much simpler way of acheiving the desired outcome.

Many Thanks

Jason

Jason
  • 1,957
  • 2
  • 20
  • 34
  • Have you seen this solution? http://stackoverflow.com/questions/13470830/how-to-change-timezone-for-a-java-util-calendar-date – Michaël Benjamin Saerens Oct 30 '13 at 09:30
  • 1
    Hey Jason, you're better off using [joda-time](http://www.joda.org/joda-time/) for this. It's a much better java implemetation of times and dates. You'll have an overhead of having to recompile it with new timezones when libya suddenly changes DST, but generally it's really useful.. – Jarede Oct 30 '13 at 09:32

1 Answers1

5

If you'll use rip747 / TimeZone-CFC, you can easily find your answer using the code below:

timezoneObj = createObject("component", "timezone").init();
melbourneTime = timezoneObj.castFromServer(now(), "Australia/Melbourne");

writeDump(melbourneTime);
Edy Ionescu
  • 1,665
  • 1
  • 12
  • 7
  • Edy, Thank you! That was perfect. That CFC has been around since 2003.. I can't beleive I never found it earlier. Thanks Heaps! – Jason Oct 30 '13 at 11:07
  • @Jason, my pleasure - I always use this CFC when dealing with time zones. Check out the [demo page](https://github.com/rip747/TimeZone-CFC/blob/master/index.cfm) too, lots of useful information in there. – Edy Ionescu Oct 30 '13 at 11:20
  • thats a neat find, but you'll have to be careful about updating it for when timezones change and govts stop switching to DST on certain dates or even change the duration of DST. – Jarede Oct 30 '13 at 11:49