目标c,聊天消息应用程序和识别时区

时间:2014-09-02 19:44:03

标签: ios objective-c

我有一个聊天应用程序,允许全世界的用户发送消息,每条消息都有时间发送消息。例如,如果我在12点发送了一条消息,那是我当地的时间,那么当我在美国或其他国家/地区收到该消息时,我将如何显示我将消息发送到正确的时区

更新

时间仍然反映服务器时间是1小时前进,我在12:05创建时间戳,但是当我从数据库收到时间戳并显示时间我得到13:05这是不正确的,因为时间应该是12:05。

用于在表中存储时间戳的数据类型是int(11)。

这是我创建客户端时间戳的地方:

                CGRect screenRect = [[UIScreen mainScreen] bounds];
            UIActivityIndicatorView *activityIndicator;
            activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            activityIndicator.frame = CGRectMake(0.0, 0.0, screenRect.size.width, screenRect.size.height);
            activityIndicator.backgroundColor = [UIColor colorWithRed:0.0f/255 green:0.0f/255 blue:0.0f/255 alpha:0.9f];
            activityIndicator.center = self.view.center;
            [self.view addSubview: activityIndicator];

            [activityIndicator startAnimating];

            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

            // 3) Load picker in background
            dispatch_async(concurrentQueue, ^{

                NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];

                NSString *myRequestString = [NSString  stringWithFormat:@"ThreadName=%@&ThreadDesc=%@&CatId=%d&UID=%d&TimeStamp=%f", textFieldThreadName.text, textFieldThreadDesc.text, rowCategory, self.userID, timeStamp];
                NSString *response = [self setupPhpCall:myRequestString :@"insertThread.php"];

                dispatch_async(dispatch_get_main_queue(), ^{
                [self insertedThread:response];
                [activityIndicator stopAnimating];
                });

            });

这是我检索时间戳并显示它的方式:

    if((NSNull *)[dict objectForKey:@"T_Created"] != [NSNull null]){
        NSString *timestampString = [dict objectForKey:@"T_Created"];
        double timestampDate = [timestampString doubleValue];
        t_Created = [NSDate dateWithTimeIntervalSince1970:timestampDate];
    }

更新

我从数据库中检索数据:

-(void)renderThreadInfo:(NSDictionary*)dic{

        NSDictionary *thread = [dic objectForKey:@"thread"];

        if((NSNull*)thread != [NSNull null]){

        int t_ID;
        int t_U_ID;
        int t_C_ID;
        NSString *t_Name;
        NSString *t_Description;
        NSDate *t_Created;
        int t_Flagged;
        int t_Rated;
        NSString *firstName;
        NSString *lastName;
        NSString *categoryName;

        for(NSDictionary *dict in thread)
        {
        if((NSNull *)[dict objectForKey:@"T_ID"] != [NSNull null]){
            t_ID = [[dict objectForKey:@"T_ID"] intValue];
        }
        if((NSNull *)[dict objectForKey:@"T_U_ID"] != [NSNull null]){
            t_U_ID = [[dict objectForKey:@"T_U_ID"] intValue];
        }
        if((NSNull *)[dict objectForKey:@"T_C_ID"] != [NSNull null]){
            t_C_ID = [[dict objectForKey:@"T_C_ID"] intValue];
        }
        if((NSNull *)[dict objectForKey:@"T_Name"] != [NSNull null]){
            t_Name = [dict objectForKey:@"T_Name"];
        }
        if((NSNull *)[dict objectForKey:@"T_Description"] != [NSNull null]){
            t_Description = [dict objectForKey:@"T_Description"];
        }
        if((NSNull *)[dict objectForKey:@"T_Created"] != [NSNull null]){
            NSString *timestampString = [dict objectForKey:@"T_Created"];
            double timestampDate = [timestampString doubleValue];
            t_Created = [NSDate dateWithTimeIntervalSince1970:timestampDate];
        }
        if((NSNull *)[dict objectForKey:@"T_Flagged"] != [NSNull null]){
            t_Flagged = [[dict objectForKey:@"T_Flagged"] intValue];
        }
        if((NSNull *)[dict objectForKey:@"T_Rated"] != [NSNull null]){
            t_Rated = [[dict objectForKey:@"T_Rated"] intValue];
        }
        if((NSNull *)[dict objectForKey:@"U_FirstName"] != [NSNull null]){
            firstName = [dict objectForKey:@"U_FirstName"];
        }
        if((NSNull *)[dict objectForKey:@"U_LastName"] != [NSNull null]){
            lastName = [dict objectForKey:@"U_LastName"];
        }
        if((NSNull *)[dict objectForKey:@"C_Name"] != [NSNull null]){
            categoryName = [dict objectForKey:@"C_Name"];
        }

        ThreadInfo *threadObj = [ThreadInfo new];
        threadObj.iD = t_ID;
        threadObj.userId  = t_U_ID;
        threadObj.catId = t_C_ID;
        threadObj.name = t_Name;
        threadObj.description = t_Description;
        threadObj.timeStampCreated = t_Created;
        threadObj.flagged = t_Flagged;
        threadObj.rated = t_Rated;
        threadObj.firstName = firstName;
        threadObj.lastName = lastName;
        threadObj.category = categoryName;

        [threadsArray addObject:threadObj];

        [tableViewThreads reloadData];

        }

        }

    }

UI显示日期:

    -(UIView*)setupThreadItem:(ThreadInfo*)threadInfo{

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        UIView *threadInfoView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, screenRect.size.width - 10, 112)];
        threadInfoView.backgroundColor = [UIColor colorWithRed:188.0f/255 green:188.0f/255 blue:188.0f/255 alpha:1.0f];
        threadInfoView.tag  = threadInfo.iD;

        UILabel *labelFirstName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 16)];
        labelFirstName.textColor = [UIColor colorWithRed:136.0f/255 green:135.0f/255 blue:135.0f/255 alpha:1.0f];
        labelFirstName.text = [NSString stringWithFormat:@"%@ %@", threadInfo.firstName,threadInfo.lastName];
        //labelFirstName.textAlignment = NSTextAlignmentCenter;
        labelFirstName.font = [UIFont fontWithName:@"Helvetica" size:13];
        labelFirstName.userInteractionEnabled = YES;
        [threadInfoView addSubview:labelFirstName];

        UILabel *labelTimestamp = [[UILabel alloc] initWithFrame:CGRectMake(140, 10, 220, 16)];
        labelTimestamp.textColor = [UIColor blackColor];
        labelTimestamp.text = [NSString stringWithFormat:@"%@", threadInfo.timeStampCreated];
        //labelFirstName.textAlignment = NSTextAlignmentCenter;
        labelTimestamp.font = [UIFont fontWithName:@"Helvetica" size:13];
        //labelFirstName.userInteractionEnabled = YES;
        [threadInfoView addSubview:labelTimestamp];

        return threadInfoView;

    }

1 个答案:

答案 0 :(得分:4)

发送信息时,请记录此时间戳并将其保存到服务器中。

// This is an absolute time based on GMT
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];

在客户端:

NSDate *sentAt = [NSDate dateWithTimeIntervalSince1970:timeStamp];
// Format sentAt appropriately for current user.