Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Source/CFTimeZone.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,29 @@ CFTimeZoneCreateWithName (CFAllocatorRef alloc, CFStringRef name,
CFURLRef path;
CFDataRef data;
CFTimeZoneRef new;

char cname[16];

/* A GMT offset such as GMT+05:00 has no zone file; build it directly. */
if (CFStringGetCString (name, cname, sizeof (cname), kCFStringEncodingASCII)
&& strncmp (cname, "GMT", 3) == 0
&& (cname[3] == '+' || cname[3] == '-'))
{
int sign = (cname[3] == '-') ? -1 : 1;
const char *p = cname + 4;
int hh = 0;
int mm = 0;

while (*p >= '0' && *p <= '9')
hh = hh * 10 + (*p++ - '0');
if (*p == ':')
++p;
while (*p >= '0' && *p <= '9')
mm = mm * 10 + (*p++ - '0');

return CFTimeZoneCreateWithTimeIntervalFromGMT (alloc,
sign * (hh * 3600 + mm * 60));
}

if (tryAbbrev)
{
CFDictionaryRef abbrevs;
Expand Down
43 changes: 43 additions & 0 deletions Tests/CFTimeZone/gmt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "CoreFoundation/CFTimeZone.h"
#include "CoreFoundation/CFCalendar.h"
#include "../CFTesting.h"

/* CFTimeZoneCreateWithName resolves GMT and UTC offset names, so a calendar
whose time zone is a GMT offset can be copied back. */

int main (void)
{
CFTimeZoneRef z0 = CFTimeZoneCreateWithTimeIntervalFromGMT (NULL, 0.0);
CFTimeZoneRef tz;
CFCalendarRef cal;

tz = CFTimeZoneCreateWithName (NULL, CFSTR ("GMT"), true);
PASS_CF (tz != NULL && CFTimeZoneGetSecondsFromGMT (tz, 0.0) == 0,
"GMT resolves to a zero offset.");
if (tz != NULL)
CFRelease (tz);

tz = CFTimeZoneCreateWithName (NULL, CFSTR ("UTC"), true);
PASS_CF (tz != NULL && CFTimeZoneGetSecondsFromGMT (tz, 0.0) == 0,
"UTC resolves to a zero offset.");
if (tz != NULL)
CFRelease (tz);

tz = CFTimeZoneCreateWithName (NULL, CFSTR ("GMT-05:00"), true);
PASS_CF (tz != NULL && CFTimeZoneGetSecondsFromGMT (tz, 0.0) == -18000,
"GMT-05:00 resolves to minus 18000 seconds.");
if (tz != NULL)
CFRelease (tz);

cal = CFCalendarCreateWithIdentifier (NULL, kCFGregorianCalendar);
CFCalendarSetTimeZone (cal, z0);
tz = CFCalendarCopyTimeZone (cal);
PASS_CF (tz != NULL && CFTimeZoneGetSecondsFromGMT (tz, 0.0) == 0,
"A calendar's GMT time zone can be copied back.");
if (tz != NULL)
CFRelease (tz);

CFRelease (cal);
CFRelease (z0);
return 0;
}
Loading