访问当前系统时区 [英] Access Current System Time Zone

查看:242
本文介绍了访问当前系统时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我可以使用以下代码检测系统时区列表:

Basically I can able to detect list of system time zones using following code:

foreach(TimeZoneInfo info in tz)
    Debug.Log("time zone id : " + info.Id + " display name : " + info.DisplayName);

运行此代码,我在控制台中显示以下输出.

Running this code, I have following output in console.

在此列表中,我想知道,我当前使用的系统是哪个? 因为我想要有关该时区的特定信息.就像下面的代码一样,我被清晰地写成亚洲/加尔各答".

In this list, I want to know, which one is my current system is following? Because I want specific information about that time zone. As like in following code, I was written clear "Asia/Kolkata".

TimeZoneInfo infos = System.TimeZoneInfo.FindSystemTimeZoneById("Asia/Kolkata");

通过运行哪个代码,我可以得到亚洲/加尔各答"的输出吗?

By running which code, I can able to get "Asia/Kolkata" as output?

编辑:我已经在这里使用NodaTime并为此运行了代码.

I was already using NodaTime here and code running for this purpose.

  // Create a NodaTime DateTimeZoneSource object for IANA time zone names
    var dateTimeZoneSource = TzdbDateTimeZoneSource.Default;

    // Get the Windows time zone by name (or use TimeZoneInfo.Local)
    var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

    // Convert between Windows and IANA time zone names
    var tzdbTimeZoneInfo = dateTimeZoneSource.MapTimeZoneId(timeZoneInfo);

    Console.WriteLine(tzdbTimeZoneInfo);

但是在这方面,我遇到了例外,

But in this, I am getting exception regarding,

引发了类型为'System.TimeZoneNotFoundException'的异常.

Exception of type 'System.TimeZoneNotFoundException' was thrown.

我将Mac系统和编辑器用作Mono Develop-Unity,而我的目标设备是iPhone.

I am using Mac system and editor as Mono Develop - Unity and my target devices are iPhones.

推荐答案

使用下面的代码,您可以获得本地TimeZoneInfo对象.

Using the code below you can get local TimeZoneInfo object.

TimeZoneInfo infos = TimeZoneInfo.Local;

这篇关于访问当前系统时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆