2013-04-24 31 views
1

我试图在iOS应用程序中使用NSMutableURLRequest从表单www.myurl.com/a?timezone=+n.n的网址请求数据。 +n.n-n.n指的是从中请求设备的时区。从iOS应用程序请求的网址中的时区

如何在url中填充此时区参数?

我周围搜索,没有发现任何与时区有关的任何w /上述格式。

+2

首先你要明白,这将不是一个实际的时区。这是与UTC的偏移量,但这不足以确定实际时区。几个不同的时区可以与UTC有相同的*当前*偏移量,但是当它们改变*偏移量时会有不同的规则。 – 2013-04-24 18:57:29

+1

这个点很奇怪。你通常使用'+ 0200'或'+02:00'格式。而这两个将很容易解决。 – DarkDust 2013-04-24 18:58:08

+0

@JonSkeet +1好吧,这是有道理的。我使用术语时区,因为我有这样的稀疏文档引用它。 – SundayMonday 2013-04-24 18:59:14

回答

2

我们需要逐步解决这个问题。首先,让我们,这将是几乎什么你想(要小心,未经测试)的字符串:

NSDateFormatter *formatter; 
NSString *almostThere; 

formatter = [[NSDateFormatter alloc] initWithDateFormat:@"ZZZZZ" allowNaturalLanguage:NO]; 
almostThere = [formatter stringFromDate:[NSDate date]]; 
[formatter release]; 

现在almostThere应根据date format string documentation是这样-08:00(NSDateFormatter使用Unicode格式,并引用此文件)。

因此,所有现在剩下的工作就是用一个点来替代冒号:

NSString *tz; 

tz = [almostThere stringByReplacingOccurrencesOfString:@":" withString:@"."]; 

最后,网址:

NSURL *theURL; 
NSString *urlString; 

urlString = [NSString stringWithFormat:@"http://www.myurl.com/a?timezone=%@", tz] 
theURL = [NSURL URLWithString:urlString]; 
+0

非常感谢!我只需要在日期格式化程序中调用'init'并设置'dateFormat'属性。看起来像'initWithDateFormat:allowNaturalLanguage:'仅可用在OS X上。 – SundayMonday 2013-04-24 19:13:39

相关问题