gpt4 book ai didi

ios - objective-c ,聊天消息应用程序和识别时区

转载 作者:行者123 更新时间:2023-11-28 19:01:04 25 4
gpt4 key购买 nike

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

更新

时间仍然反射(reflect)服务器时间向前 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;

}

最佳答案

发送消息时,记录这个时间戳并保存到你的服务器。

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

在客户端:

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

关于ios - objective-c ,聊天消息应用程序和识别时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com