我有一个POSIXct变量,读取2016-12-15 20:11:29 PST,但PST不正确。它实际上应该是2016-12-15 20:11:29 GMT
我需要使时区正确,GMT,然后我需要将其转换为纽约时间
所以我得到2016-12-15 20:11:29太平洋标准时间。这真的应该是2016-12-15 20:11:29 GMT然后我需要将2016-12-15 20:11:29转换为GMT到“America / New_York”所以最终结果将是16:11:29 America / New_York,因为纽约时间是格林尼治标准时间之后。
我尝试了很多方法,但无法让它发挥作用。有什么想法吗?
bool isstoreTapped = false;
bool iskoleksiTapped = false;
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
loadingLogin.IsActive = true;
FailedMessage.Visibility = Visibility.Collapsed;
ProsesLogin();
if(isstoreTapped==true)
{
this.Frame.Navigate(typeof(Store));
}
if(iskoleksiTapped==true)
{
this.Frame.Navigate(typeof(koleksibuku.KolesiPage));
}
}
private async void ProsesLogin()
{
FailedMessage.Visibility = Visibility.Collapsed;
loadingLogin.IsActive = true;
try
{
var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("BSE_Win10(1)", "mahonidatastream", "Maho1019");
var client = new Windows.Web.Http.HttpClient(filter);
string urlPath = "https://...m/user/auth";
var values = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("email", emailBox.Text),
new KeyValuePair<string, string>("password",passwordBox.Password)
};
var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
//RequestException();
loadingLogin.IsActive = false;
}
string jsonText = await response.Content.ReadAsStringAsync();
JsonObject jsonObject = JsonObject.Parse(jsonText);
bool error = jsonObject["error"].GetBoolean();
//string message = jsonObject["message"].GetString();
if (error == false)
{
LoginDialog.IsOpen = false;
FailedMessage.Visibility = Visibility.Collapsed;
((App)(App.Current)).UserName = emailBox.Text;
loadingLogin.IsActive = false;
}
else
{
FailedMessage.Visibility = Visibility.Visible;
FailedMessage.Text = "Email atau password tidak sesuai/belum terdaftar";
loadingLogin.IsActive = false;
}
}
catch (HttpRequestException ex)
{
//ConnectionException();
loadingLogin.IsActive = false;
}
}
private void store_Tapped(object sender, TappedRoutedEventArgs e)
{
if (((App)(App.Current)).UserName == "Sign in to your account")
{
LoginDialog.IsOpen = true;
loginDetail.Visibility = Visibility.Collapsed;
loginEnter.Visibility = Visibility.Visible;
emailBox.Text = "";
passwordBox.Password = "";
isstoreTapped = true;
iskoleksiTapped = false;
}
else
{
this.Frame.Navigate(typeof(Store));
}
}
private void koleksi_Tapped(object sender, TappedRoutedEventArgs e)
{
if (((App)(App.Current)).UserName == "Sign in to your account")
{
LoginDialog.IsOpen = true;
loginDetail.Visibility = Visibility.Collapsed;
loginEnter.Visibility = Visibility.Visible;
emailBox.Text = "";
passwordBox.Password = "";
iskoleksiTapped = true;
isstoreTapped = false;
}
else
{
this.Frame.Navigate(typeof(koleksibuku.KolesiPage));
}
}
**********更新********* 我的真实代码使用变量,建议的解决方案不起作用。见下文
p = as.POSIXct("2016-12-15 20:11:29 PST")
p
***这也是骗局工作
p = as.POSIXct("2016-12-15 20:11:29 PST")
out = as.POSIXct(p, tz="GMT")
out
attr(out,"tzone") <- "America/New_York"
out
> out
[1] "2016-12-15 20:11:29 PST"
> attr(out,"tzone") <- "America/New_York"
> out
[1] "2016-12-15 23:11:29 EST" ### this is incorrect
答案 0 :(得分:1)
我可以在那里找到你的大部分路,但R认为你对时差有误:
> p = as.POSIXct("2016-12-15 20:11:29 PST", usetz=TRUE, tz="GMT")
> p
[1] "2016-12-15 20:11:29 GMT"
> (format(p, tz="America/New_York") )
[1] "2016-12-15 15:11:29"
对编辑的回应。我没有看到问题。 R中时间的数字表示总是GMT。您可以使用参数输入来删除错误的tz,但是format
或strftime
的输出只需要为不同的时区指定参数。 (使用变量应该没有效果。)
p = "2016-12-15 20:11:29 PST"
pp = as.POSIXct(p, usetz=TRUE, tz="GMT")
pp
[1] "2016-12-15 20:11:29 GMT"
答案 1 :(得分:1)
您的问题是您的p
默认位于您的系统时区:
p = as.POSIXct("2016-12-15 20:11:29 PST")
p
# I'm in Australian Eastern Standard Time, AEST
#[1] "2016-12-15 20:11:29 AEST"
as.numeric(p)
#[1] 1481796689
将此本地日期时间转换为UTC
,通过格式化并将时区重置为GMT来更改基础数据(除非您已经在GMT中,否则它当然不会做任何事情):
p2 <- as.POSIXct(format(p),tz="UTC")
p2
#[1] "2016-12-15 20:11:29 UTC"
as.numeric(p2)
#[1] 1481832689
p2 - p
#Time difference of 10 hours
# ...which is the difference between AEST and GMT
然后你可以调整时区属性,这只会改变日期/时间对象的显示。它不会影响基础数字数据:
attr(p2,"tzone") <- "America/New_York"
p2
#[1] "2016-12-15 15:11:29 EST"
as.numeric(p2)
#[1] 1481832689