首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSwagStudio生成的C#客户端偏移日期

NSwagStudio生成的C#客户端偏移日期
EN

Stack Overflow用户
提问于 2022-09-21 22:06:42
回答 1查看 79关注 0票数 0

我正在使用NSwagStudio生成一个C#客户端,一般来说,它运行得很好。然而,我刚刚发现它没有正确地识别日期字符串中的时区。我将从API获得一个到期的令牌。JSON中的过期字符串如下所示:2022-09-21T22:09:34.722Z。使用Z修饰符,它应该知道它是UTC。当我创建一个新的应用程序,只使用DateTime.Parse()并放入该字符串时,它会给出一个在本地时区(GMT-6)中正确反映的日期。但是,当客户端解析该对象时,date值显示的方式就好像这是我的本地时间(例如,示例日期将显示为10:09PM我的时间,而它实际上应该是10:09PM UTC,即4:09PM我的时间)。

这是我的nswag.json文件:

代码语言:javascript
复制
{
  "runtime": "Net60",
  "defaultVariables": null,
  "documentGenerator": {
    "fromDocument": {
      "json": "",
      "url": "*my/swagger/json/location*",
      "output": null,
      "newLineBehavior": "Auto"
    }
  },
  "codeGenerators": {
    "openApiToCSharpClient": {
      "clientBaseClass": "ClientBase",
      "configurationClass": null,
      "generateClientClasses": true,
      "generateClientInterfaces": true,
      "clientBaseInterface": "IClient",
      "injectHttpClient": true,
      "disposeHttpClient": true,
      "protectedMethods": [],
      "generateExceptionClasses": true,
      "exceptionClass": "ApiException",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": false,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": true,
      "useBaseUrl": true,
      "generateBaseUrlProperty": true,
      "generateSyncMethods": false,
      "generatePrepareRequestAndProcessResponseAsAsyncMethods": false,
      "exposeJsonSerializerSettings": false,
      "clientClassAccessModifier": "internal",
      "typeAccessModifier": "public",
      "generateContractsOutput": false,
      "contractsNamespace": null,
      "contractsOutputFilePath": null,
      "parameterDateTimeFormat": "s",
      "parameterDateFormat": "yyyy-MM-dd",
      "generateUpdateJsonSerializerSettingsMethod": true,
      "useRequestAndResponseSerializationSettings": false,
      "serializeTypeInformation": false,
      "queryNullValue": "",
      "className": "{controller}Client",
      "operationGenerationMode": "MultipleClientsFromFirstTagAndOperationId",
      "additionalNamespaceUsages": [],
      "additionalContractNamespaceUsages": [],
      "generateOptionalParameters": true,
      "generateJsonMethods": true,
      "enforceFlagEnums": false,
      "parameterArrayType": "System.Collections.Generic.IEnumerable",
      "parameterDictionaryType": "System.Collections.Generic.IDictionary",
      "responseArrayType": "System.Collections.Generic.ICollection",
      "responseDictionaryType": "System.Collections.Generic.IDictionary",
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "namespace": "my.namespace",
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "jsonConverters": null,
      "anyType": "object",
      "dateTimeType": "System.DateTime",
      "timeType": "System.TimeSpan",
      "timeSpanType": "System.TimeSpan",
      "arrayType": "System.Collections.Generic.ICollection",
      "arrayInstanceType": "System.Collections.ObjectModel.Collection",
      "dictionaryType": "System.Collections.Generic.IDictionary",
      "dictionaryInstanceType": "System.Collections.Generic.Dictionary",
      "arrayBaseType": "System.Collections.ObjectModel.Collection",
      "dictionaryBaseType": "System.Collections.Generic.Dictionary",
      "classStyle": "Poco",
      "jsonLibrary": "NewtonsoftJson",
      "generateDefaultValues": true,
      "generateDataAnnotations": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateImmutableArrayProperties": false,
      "generateImmutableDictionaryProperties": false,
      "jsonSerializerSettingsTransformationMethod": null,
      "inlineNamedArrays": false,
      "inlineNamedDictionaries": false,
      "inlineNamedTuples": true,
      "inlineNamedAny": false,
      "generateDtoTypes": true,
      "generateOptionalPropertiesAsNullable": false,
      "generateNullableReferenceTypes": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "Client.cs",
      "newLineBehavior": "Auto"
    }
  }
}

我可以在调试器中逐步遍历客户端,并看到它使用设置创建序列化程序(目前我还没有公开这些设置)。以下是序列化程序的相关属性:

据我所知,这应该是正确的,但实际上不是这样的.

有人能告诉我我在这里做错了什么或如何纠正吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-21 23:03:57

来自序列化程序的DateTime将具有DateTimeKind of DateTimeKind.Utc。不幸的是,将其与另一个DateTime (即使是DateTimeKind.Local类型)进行比较,将忽略这类,而只是按原样进行比较。如果这是一个问题,您可以与DateTime.UtcNow进行比较。或者,您可以反序列化为DateTimeOffset,这将在进行比较时考虑到偏移量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73807405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档