自动续订订阅收据日期格式

时间:2023-01-12 21:34:29

I'm struggling to figure out the formatting for the following date:

我正在努力弄清楚以下日期的格式:

2011-05-24 19:02:32 Etc/GMT

2011-05-24 19:02:32 Etc / GMT

This date is returned from Apple's receipt validation service and I need to turn it into a NSDate for some comparison operations. The real trouble is related to the timezone.

此日期从Apple的收据验证服务返回,我需要将其转换为NSDate以进行一些比较操作。真正的麻烦与时区有关。

Here's some code I've already written:

这是我已经写过的一些代码:

        NSDictionary *receiptData = [info valueForKey:@"expires_date"];

        NSDateFormatter *f = [[NSDateFormatter alloc] init];

        [f setLocale:[NSLocale currentLocale]];
        [f setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [f setDateFormat:@"yyyy-MM-dd HH:mm:ss vvvv"];

        NSLog(@"%@", [f stringFromDate:[NSDate date]]);

        NSDate *subPurchaseDate = [f dateFromString:[receiptData valueForKey:@"original_purchase_date"]];

        [f release];

I've tried all combinations of 'v's and 'Z's that I can think of. Any insight?

我已经尝试过'v'和'Z'的所有组合,我能想到的。任何见解?

3 个解决方案

#1


15  

By looking at the date format documentation I got the correct format, is works well for me:

通过查看日期格式文档,我得到了正确的格式,对我来说效果很好:

        NSDateFormatter * formatter = [NSDateFormatter new];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss VV";

        NSDate * purchaseDate = [formatter dateFromString:latestTransaction[@"original_purchase_date"]];
        NSDate * expirationDate = [formatter dateFromString:latestTransaction[@"expires_date"]];

No need to set the time zone on the formatter since it's embedded in the string.
Manipulating the date string to parse it is not a good idea.

Source: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

无需在格式化程序上设置时区,因为它嵌入在字符串中。操作日期字符串来解析它并不是一个好主意。资料来源:http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

#2


3  

After doing some more research, the code I'm using here works, but is suspect;

在做了一些更多的研究之后,我在这里使用的代码有效,但是很可疑;

   NSString *purchaseDateString = [@"2011-05-24 19:02:32 Etc/GMT" stringByReplacingOccurrencesOfString:@" Etc/GMT" withString:@""];
   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   NSLocale *POSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
   [formatter setLocale:POSIXLocale];
   [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

   NSDate *purchaseDate = [formatter dateFromString:purchaseDateString];

I don't like the assumptions I am making about the incoming date string and so I am assuming this code may break for other stores (I'm testing against the UK one). So although this kind of works for my own situation, I'd really like to see a more robust solution that actually parses the timezone string correctly as per the original question.

我不喜欢我对传入的日期字符串做出的假设,所以我假设这个代码可能会破坏其他商店(我正在测试英国的代码)。因此,虽然这种方法适用于我自己的情况,但我真的希望看到一个更强大的解决方案,它实际上按照原始问题正确解析时区字符串。

I can't quite believe Apple have used a deprecated timezone (all the Etc/* ones are officially deprecated) in these important date strings!

我无法相信Apple在这些重要的日期字符串中使用了一个已弃用的时区(所有Etc / *都被正式弃用)!

EDIT:

编辑:

I note you are using

我注意到你正在使用

NSDictionary *receiptData = [info valueForKey:@"expires_date"];

This is not actually a string according to the documentation it should be "The expiration date of the subscription receipt, expressed as the number of milliseconds since January 1, 1970, 00:00:00 GMT"

根据文档,这实际上不是一个字符串,它应该是“订阅收据的到期日期,表示为自1970年1月1日00:00:00 GMT以来的毫秒数”

However the question is still valid as you have to use the purchase_date field when working with restored subscriptions and this field is in the text format you have described.

但是,问题仍然有效,因为在使用已恢复的订阅时您必须使用purchase_date字段,并且此字段采用您所描述的文本格式。

#3


0  

This is what I do on Swift 3 to convert the date from an auto-renewable date from a receipt.

这就是我在Swift 3上所做的事情,即从收据中自动更新日期转换日期。

let expirationDate = "2017-08-16 23:10:35 Etc/GMT"
let receiptDateFormat = "yyyy-MM-dd HH:mm:ss VV"
let theDateFormat = DateFormatter.Style.medium
let theTimeFormat = DateFormatter.Style.medium
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = receiptDateFormat
let firstDate = dateFormatter.date(from: stringDate)
dateFormatter.dateStyle = theDateFormat
dateFormatter.timeStyle = theTimeFormat

let result = dateFormatter.string(from: firstDate!)

#1


15  

By looking at the date format documentation I got the correct format, is works well for me:

通过查看日期格式文档,我得到了正确的格式,对我来说效果很好:

        NSDateFormatter * formatter = [NSDateFormatter new];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss VV";

        NSDate * purchaseDate = [formatter dateFromString:latestTransaction[@"original_purchase_date"]];
        NSDate * expirationDate = [formatter dateFromString:latestTransaction[@"expires_date"]];

No need to set the time zone on the formatter since it's embedded in the string.
Manipulating the date string to parse it is not a good idea.

Source: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

无需在格式化程序上设置时区,因为它嵌入在字符串中。操作日期字符串来解析它并不是一个好主意。资料来源:http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

#2


3  

After doing some more research, the code I'm using here works, but is suspect;

在做了一些更多的研究之后,我在这里使用的代码有效,但是很可疑;

   NSString *purchaseDateString = [@"2011-05-24 19:02:32 Etc/GMT" stringByReplacingOccurrencesOfString:@" Etc/GMT" withString:@""];
   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   NSLocale *POSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
   [formatter setLocale:POSIXLocale];
   [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

   NSDate *purchaseDate = [formatter dateFromString:purchaseDateString];

I don't like the assumptions I am making about the incoming date string and so I am assuming this code may break for other stores (I'm testing against the UK one). So although this kind of works for my own situation, I'd really like to see a more robust solution that actually parses the timezone string correctly as per the original question.

我不喜欢我对传入的日期字符串做出的假设,所以我假设这个代码可能会破坏其他商店(我正在测试英国的代码)。因此,虽然这种方法适用于我自己的情况,但我真的希望看到一个更强大的解决方案,它实际上按照原始问题正确解析时区字符串。

I can't quite believe Apple have used a deprecated timezone (all the Etc/* ones are officially deprecated) in these important date strings!

我无法相信Apple在这些重要的日期字符串中使用了一个已弃用的时区(所有Etc / *都被正式弃用)!

EDIT:

编辑:

I note you are using

我注意到你正在使用

NSDictionary *receiptData = [info valueForKey:@"expires_date"];

This is not actually a string according to the documentation it should be "The expiration date of the subscription receipt, expressed as the number of milliseconds since January 1, 1970, 00:00:00 GMT"

根据文档,这实际上不是一个字符串,它应该是“订阅收据的到期日期,表示为自1970年1月1日00:00:00 GMT以来的毫秒数”

However the question is still valid as you have to use the purchase_date field when working with restored subscriptions and this field is in the text format you have described.

但是,问题仍然有效,因为在使用已恢复的订阅时您必须使用purchase_date字段,并且此字段采用您所描述的文本格式。

#3


0  

This is what I do on Swift 3 to convert the date from an auto-renewable date from a receipt.

这就是我在Swift 3上所做的事情,即从收据中自动更新日期转换日期。

let expirationDate = "2017-08-16 23:10:35 Etc/GMT"
let receiptDateFormat = "yyyy-MM-dd HH:mm:ss VV"
let theDateFormat = DateFormatter.Style.medium
let theTimeFormat = DateFormatter.Style.medium
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = receiptDateFormat
let firstDate = dateFormatter.date(from: stringDate)
dateFormatter.dateStyle = theDateFormat
dateFormatter.timeStyle = theTimeFormat

let result = dateFormatter.string(from: firstDate!)