当前位置:网站首页>The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.
The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.
2022-07-28 18:47:00 【Chen Yan will act】
Unity And Switching languages results in an error :System.FormatException:String was not recognized as a valid DateTime.
One , Something's wrong
1.1 The problem background
Recently, when developing multilingual localization ( Simplified Chinese character - Switch back and forth in traditional Chinese ), This error occurred :System.FormatException: String was not recognized as a valid DateTime. --> The system format is abnormal : The string is not recognized as a valid datetime .
It seems that there is a logical error in passing a wrong string to DateTime.Parse(“”) Method as a parameter . What's so strange is No error occurred under the editor , There is no error when the real machine does not switch languages , Only in IOS This error occurs when switching between simple and traditional system languages .
So guess in different language environments DateTime.Now.ToString() The results are different , So there it is 2.1 Test cases for .
1.2 Error log
Error log captured by real machine switching language :
System.AggregateException: One or more errors occurred. ---> System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse (System.String s, System.Globalization.DateTimeFormatInfo dtfi, System.Globalization.DateTimeStyles styles) [0x00000] in <00000000000000000000000000000000>:0
at WZC.ActivityManager+<>c__DisplayClass45_0.<GetActivitySwitchConfig>b__0 (WZC.ActivitySwitchConfig e) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.Threading.ContextCallback callback, System.Object state, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task`1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at WZC.ApiHelper..cctor () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
--- End of inner exception stack trace ---
Two , Location problem
2.1 The test case
According to the logic of the game DateTime Usage situation , For the convenience of observing logs Scroll View Components , Put a long one inside Text, The test code and scenario construction effect are as follows :
using System;
using UnityEngine;
using UnityEngine.UI;
public class ConverTimeError : MonoBehaviour
{
public Text context;
public Button cunShiJian;
public Button quShiJian;
void Start()
{
var strRus = PlayerPrefs.GetString("Hall:RegionGetDate");
var strRusDate = DateTime.Parse(strRus);
context.text += " Conversion result :+++" + strRusDate.Year + "/" + strRusDate.Month+ "/" + strRusDate.Day + ";\n";
cunShiJian.onClick.AddListener(()=>{
context.text += " Save time :---" + DateTime.Now + ";\n";
PlayerPrefs.SetString("Hall:RegionGetDate", DateTime.Now.ToString());
context.text += " Store results :---" + PlayerPrefs.GetString("Hall:RegionGetDate") + ";\n";
});
quShiJian.onClick.AddListener(()=>{
context.text += " Take the time :+++" + DateTime.Now + ";\n";
var regionGetDateStr = PlayerPrefs.GetString("Hall:RegionGetDate");
context.text += " Get the result :+++" + regionGetDateStr + ";\n";
var regionGetDate = DateTime.Parse(regionGetDateStr);
context.text += " Get the conversion result :+++" + regionGetDate.Year + "/" + regionGetDate.Month+ "/" + regionGetDate.Day + ";\n";
});
}
}

Use this test case in macOS and IOS The test of switching languages has been carried out on , The result is nothing abnormal , Find out DateTime.Now.ToString() The result is : month / Japan / year when : branch : second This format .

2.2 Find the problem
No problem found in the test case , Then I have to fill the log in the original project , After several twists and turns, it was finally determined that the format of the string with a storage time was Japan / month / year when : branch : second
So the transformation is simulated in the editor year / month / Japan , month / Japan / year and Japan / month / year Time conversion of three formats :
Debug.Log(" year / month / Japan : " + DateTime.Parse("2022/7/25 13:11:00"));
Debug.Log(" month / Japan / year : " + DateTime.Parse("7/25/2022 13:11:00"));
// Problematic time format conversion
Debug.Log(" Japan / month / year : " + DateTime.Parse("25/7/2022 13:11:00"));

It turns out that : Japan / month / year Strings in this format really cannot be used DateTime.Parse() Method to convert .
So here comes the new question , In time storage, we use :DateTime.Now.ToString(), And in the 2.1 We have confirmed in the test cases of DateTime.Now.ToString() The result is : month / Japan / year when : branch : second This format .
At the same time, it can be determined that the storage time in the game is used DateTime.Now.ToString() This form , This is very puzzling in the logic of the game Japan / month / year How does the format string come out ?
3、 ... and , solve the problem
3.1 Try to solve
At first, I used
DateTime.ParseExact(DateTime.Now.ToString(), "yyyy/dd/MM", System.Globalization.CultureInfo.InvariantCulture);
This method is used for format conversion , Because this is the conversion of the specified format , Therefore, according to the above test results, it is not possible .
3.2 solve the problem
Fix the stored code in the format :
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
Fortunately, there are only a few places in the project DateTime.Now Assignment , It is very convenient to modify . Finally, the test passed .
Encountered such a strange problem , Make a note of , For children's shoes with similar problems .
边栏推荐
- 全新升级!《云原生架构白皮书 2022 版》重磅发布
- MySQL index usage and optimization
- LeetCode_ 1137_ Nth teponacci number
- 112. 使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误
- UE5 GAS 学习笔记 1.4属性集
- Tencent Tang Daosheng: open source is a new mode of production and collaboration in the era of industrial Internet
- Golang 打包发布到各个平台
- MYSQL入门与进阶(八)
- It is said that software testing is the worst in the IT industry. Is that so?
- 408 review strategy (strengthening stage)
猜你喜欢

@Autowired与@Resource区别

MYSQL入门与进阶(四)

1.3 linked list

1.1、稀疏数组

Error 2003 (HY000) can't connect to MySQL server on 'localhost3306' (10061) solution

Meta Q2财报:营收首次下滑,Metaverse将与苹果竞争

Introduction and advanced level of MySQL (5)

Ue5 gas learning notes 0.2 configuration plug-in

112. 使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误

Six countries in Europe and the United States launched an express line product to optimize the "end-to-end" performance service on the 5th
随机推荐
Redis缓存雪崩、穿透、击穿,布隆过滤器,分布式锁详解
实验楼----PHP大法
Devops in digital transformation -- flexible cooperation
十进制转二进制进阶版(可转化负数以及边界值)
First understanding of structure
MongoDB数据库复制表
1.3、链表
leetcode 二叉树类
MYSQL入门与进阶(一)
Golang 并发之锁
There is a special cryptology language called asn.1
2022-07-27 第四小组 修身课 学习笔记(every day)
Brief introduction: basic principle of srv6
注意力机制及代码实现
Docker builds MySQL master-slave replication
Meta Q2财报:营收首次下滑,Metaverse将与苹果竞争
One Hot编码是什么?为什么要用它,什么时候用它?
What skills do you need to master when learning software testing zero foundation?
苹果开发完整的苹果证书与描述文件创建流程
npm 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。