Code covered by the BSD License  

Highlights from
Convert between world time zones with daylight saving times


4.0 | 6 ratings Rate this file 34 Downloads (last 30 days) File Size: 5.38 KB File ID: #27953
image thumbnail

Convert between world time zones with daylight saving times



This is a very simple yet powerful function to convert a datenum from one timezone to another.

| Watch this File

File Information

As I could find no built-in function nor reliable contribution to achieve that, I had no choice but to write my own, leveraging the GregorianCalendar Java class.
Usage is quite simple:
    targetDST = TimezoneConvert( dn, fromTimezone, toTimezone )

The datenum dn corresponds to the datetime you want to convert. Just specify from which timezone it comes and the target timezone, and you should get the expected result, that takes into account daylight saving time.

Feel free to let me know if I can extend it to support more situations (like Julian Calendar, obviously ;-).

You can get the name of the available timezones using this command:
after importing the relevant java packages.
    import java.lang.String
    import java.util.* java.awt.*
    import java.util.Enumeration


This file inspired Timezone Convert By Erwin Mayer With Vector Support.

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Java
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
26 Dec 2013 Ricardo

Here's a suggestion for a vectorized version of this function:

function time = convertTZone(time, TZoneOld, TZoneNew)

OffSet = now - TimezoneConvert(now, TZoneOld, TZoneNew);
time = time - OffSet;


10 Oct 2013 Dennis Wouters

@Scott, I applied your 'fix', but do not observe your issue.

t1 = datestr(TimezoneConvert(datenum('31-Mar-2013 00:59:59'),'UTC','Europe/Berlin'))
t2 = datestr(TimezoneConvert(datenum('31-Mar-2013 01:00:00'),'UTC','Europe/Berlin'))

Results in:
t1 =
31-Mar-2013 01:59:59
t2 =
31-Mar-2013 03:00:00

as expected.
So I think your fix is ok.

02 Aug 2013 Justinas

Hi, currently the function supports only one date conversion, due to Calendar.set method limitations.
How could I modify it to make fast conversions for multiple dates? i.e.if dn would be a vector.

03 Jun 2013 janez

Great tool. A word of warning though: as noticed by Matt Aldrich it doesn't accept multidimensional arrays. But if you loop sequentially over all array elements (datenumbers) it works like a charm.

27 Mar 2013 Erwin Mayer

Hello Scott, I would be happy to help more, unfortunately I don't have Matlab installed right now, and I would greatly suggest to use the Joda Time library instead, to perform the same task as in my original script:
I have been using the C# port Noda Time lately and it is perfectly suited for this kind of tasks.

@Benjamin, this library would also be more reliable to avoid the kind of rounding errors you have observed.

22 Mar 2013 Scott

Hi Erwin, thanks for the reply.

I tried to remove the dependency on the financial toolbox and this almost worked: for Germany, the transition from/to daylight savings times happens at midnight, which isn't correct:

But since I'm not totally certain about what the dependency-causing financial toolbox functions do, the problem might lie with my hacks. Here's the change I made to TimezoneConvert.m:

% Remove dependence on financial toolbox
% t1.set(year(dn), month(dn)-1, day(dn), hour(dn), minute(dn), second(dn))

[Y, M, D, H, MN, S] = datevec(dn);
t1.set(Y, M-1, D, H, MN, S);

Does this hack give the same answer as when using the FT functions?

22 Mar 2013 Erwin Mayer

Scott: Yes, it requires the financial toolbox.

21 Mar 2013 Scott

When I run it, I get the error:

??? Undefined function or method 'year' for input arguments of type 'double'.

Error in ==> TimezoneConvert at 11
t1.set(year(dn), month(dn)-1, day(dn), hour(dn), minute(dn), second(dn))

Does this require the financial toolbox?

16 Dec 2012 Benjamin

Sorry, I did not finish the comment but the Matlab comment already got submitted.

Using the same code if we ignored the seconds difference the time conversion is incorrect as well for historical daylight savings:

nDates1 = datenum([2012 6 21 6 0 0]);
tDates1 = TimezoneConvert(nDates1, 'EST', 'Hongkong');


ans = 2012 06 21 19 0 0.34

6am EST on June 21 does not equate to 7pm China Standard Time on June 21st (as there is no daylight savings then).

16 Dec 2012 Benjamin

There seems to be a rounding error. Using following code generates 0.34 seconds difference. I believe you need to floor some of the calculations when performing this calculation as the precision isn't correct.

nDates1 = datenum([2012 6 21 6 0 0]);
tDates1 = TimezoneConvert(nDates1, 'EST', 'Hongkong');


ans =

1.0e+03 *

2.0120 0.0060 0.0210 0.0190 0 0.0003

23 Apr 2012 Matt Aldrich

What about vectorized support?

29 Mar 2012 Yuxiao Qin

Thank you so much for the file... I was always working on APIs, which cannot display historical DST... Never thought matlab itself can do the job... Much appreciation!

19 Jan 2012 David

thank you!

04 Aug 2011 Erwin Mayer

Hi Justinas, it should adjust historical time for the DST, that was the purpose. At least provided the functions from the Java package support them (probably not for dates very far away) Let me know if it does not work with a special case.

04 Aug 2011 Justinas

This function does not seem to adjust historical time for the DST, does it?

14 Feb 2011 David Goldsmith

"Feel free to let me know if I can extend it to support more situations"

Well, since K E wasn't explicit: can you make it Financial-Toolbox-free (i.e., not dependent on having that)?

09 Jul 2010 K E

OK, that's why - I don't have the Financial Toolbox.
Thanks, KE

07 Jul 2010 Erwin Mayer

Hi K E, from what I could see these functions are normally part of the Financial Toolbox. Aren't they enabled by default?

07 Jul 2010 K E

Seems to crash because the following mfiles are not included:
Or have I misunderstood how to run this?
t1 = TimezoneConvert(now, 'UTC', 'US/Eastern')

Contact us