Pregunta

Sólo estoy tratando de crear un NSDate objeto de la cadena como:2011-02-10 4:30:45

Echa un vistazo a el código:

Enfoque 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;
}

Y aquí está el resultado:


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

Entonces, ¿por qué 2> llegar imprime mal?
Supongamos que en 2011-02-10 4:20:45, Trataré de establecer un LocalNotification con esa fecha, que se activa al instante, ya que el procesador asume que el tiempo ya está vencido!


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];

Y, ya he probado la configuración de diversos TimeZones y NSLocale para en_US, en_FR.No hay ayuda.

Enfoque C:

Y para tu INFORMACIÓN, ahora estoy cambiando a C, tratando de hacer algo con:


-(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;
}

Salida:ans: -1.000000
Allí de nuevo estoy atascado con la conversión de time_t a NSTimeInterval.
Así que, gracias de antemano si me pudieran ayudar con cualquier enfoque :(

¿Fue útil?

Solución

En su cadena de entrada y el formato yyyy-MM-dd HH:mm:ss no especificar la zona horaria.De modo predeterminado a la hora UTC.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top