使用 NSDateFormatter 或 mktime 创建格式化的 NSDate

发布于 2024-10-16 22:02:00 字数 1972 浏览 8 评论 0原文

我只是想创建一个 NSDate 对象,字符串如下:2011-02-10 4:30:45

看一下代码:

Approach OBJC :

-(NSDate *)get_date_df:(NSString *)dstr
{
    NSLog(@"1> %@",dstr);

    NSDateFormatter *df_in = [[[NSDateFormatter alloc] init]autorelease];
    [df_in setDateFormat:@"yyyy-MM-dd HH:mm:ss"];   
    NSDate *d = [df_in dateFromString:dstr];

    NSLog(@"2> %@",d);

    NSLog(@"3> %@",[df_in stringFromDate:d]);

    return d;
}

这是输出:


1> 2011-02-10 4:30:45
2> 2011-02-09 23:00:45 +0000
3> 2011-02-10 04:30:45

那么为什么 2> 打印错误?
假设在2011-02-10 4:20:45,我尝试设置一个具有该日期的 LocalNotification,它会立即触发,因为处理器假设时间已经过期!


UILocalNotification *lnf = [[UILocalNotification alloc] init];
[lnf setFireDate:[self get_date_df:@"2011-02-10 4:30:45"]];
[lnf setAlertBody:@"WTF"];
[[UIApplication sharedApplication]scheduleLocalNotification:lnf];
[lnf release];

而且,我已经尝试为 en_US、en_FR 设置各种 TimeZonesNSLocale。没有帮助。

方法 C:

仅供参考,现在我正在切换到 C,尝试使用以下内容:


-(NSDate *)get_date_c:(const char *)date_str
{
    char date[10][6] = {0};
    sscanf(date_str,"%[0-9] - %[0-9] - %[0-9] %[0-9] : %[0-9] : %[0-9]",date[0],date[1],date[2],date[3],date[4],date[5]);   

    struct tm *t_info = (struct tm *)malloc(sizeof(struct tm));
    t_info->tm_sec = atoi(date[5]);
    t_info->tm_min = atoi(date[4]);
    t_info->tm_hour = atoi(date[3]);
    t_info->tm_mday = atoi(date[2]);
    t_info->tm_mon = atoi(date[1]);
    t_info->tm_year = atoi(date[0]);
    NSLog(@"ans: %f",(double)mktime(t_info));
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:mktime(t_info)];
    free(t_info);
    return d;
}

输出: 答:-1.000000
我再次陷入将 time_t 转换为 NSTimeInterval 的困境。
所以,如果您能通过任何方法帮助我,请提前致谢:(

I'm just trying to create a NSDate object with string as:2011-02-10 4:30:45

Take a look at the code:

Approach OBJC:

-(NSDate *)get_date_df:(NSString *)dstr
{
    NSLog(@"1> %@",dstr);

    NSDateFormatter *df_in = [[[NSDateFormatter alloc] init]autorelease];
    [df_in setDateFormat:@"yyyy-MM-dd HH:mm:ss"];   
    NSDate *d = [df_in dateFromString:dstr];

    NSLog(@"2> %@",d);

    NSLog(@"3> %@",[df_in stringFromDate:d]);

    return d;
}

And here's the output:


1> 2011-02-10 4:30:45
2> 2011-02-09 23:00:45 +0000
3> 2011-02-10 04:30:45

So why is 2> getting printed wrong?
Suppose at 2011-02-10 4:20:45, I try setting up a LocalNotification with that date, it fires instantly because the processor assumes the time is already expired!


UILocalNotification *lnf = [[UILocalNotification alloc] init];
[lnf setFireDate:[self get_date_df:@"2011-02-10 4:30:45"]];
[lnf setAlertBody:@"WTF"];
[[UIApplication sharedApplication]scheduleLocalNotification:lnf];
[lnf release];

And, I've already tried setting up various TimeZones and NSLocale for en_US, en_FR. No help.

Approach C:

And FYI now I'm switching to C, trying something with:


-(NSDate *)get_date_c:(const char *)date_str
{
    char date[10][6] = {0};
    sscanf(date_str,"%[0-9] - %[0-9] - %[0-9] %[0-9] : %[0-9] : %[0-9]",date[0],date[1],date[2],date[3],date[4],date[5]);   

    struct tm *t_info = (struct tm *)malloc(sizeof(struct tm));
    t_info->tm_sec = atoi(date[5]);
    t_info->tm_min = atoi(date[4]);
    t_info->tm_hour = atoi(date[3]);
    t_info->tm_mday = atoi(date[2]);
    t_info->tm_mon = atoi(date[1]);
    t_info->tm_year = atoi(date[0]);
    NSLog(@"ans: %f",(double)mktime(t_info));
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:mktime(t_info)];
    free(t_info);
    return d;
}

Output:
ans: -1.000000
There again I'm stuck with converting time_t to NSTimeInterval.
So, thanks in advance if you could help me out with any approach :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2024-10-23 22:02:00

在您的输入字符串和格式 yyyy-MM-dd HH:mm:ss 中,您没有指定时区。所以默认为UTC。

In your input string and format yyyy-MM-dd HH:mm:ss you didn't specify the timezone. So it defaults to UTC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文