{"record":{"id":"c4888faf4122ae6e","repo":"XINCGer/Unity3DTraining","slug":"conversion-from-datetime-to-timestamp-requires-the","errorCode":null,"errorMessage":"Conversion from DateTime to Timestamp requires the DateTime kind to be Utc","messagePattern":"Conversion from DateTime to Timestamp requires the DateTime kind to be Utc","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/WellKnownTypes/TimestampPartial.cs","lineNumber":152,"sourceCode":"        /// <returns>This timestamp as a <c>DateTimeOffset</c>.</returns>\n        /// <exception cref=\"InvalidOperationException\">The timestamp contains invalid values; either it is\n        /// incorrectly normalized or is outside the valid range.</exception>\n        public DateTimeOffset ToDateTimeOffset()\n        {\n            return new DateTimeOffset(ToDateTime(), TimeSpan.Zero);\n        }\n\n        /// <summary>\n        /// Converts the specified <see cref=\"DateTime\"/> to a <see cref=\"Timestamp\"/>.\n        /// </summary>\n        /// <param name=\"dateTime\"></param>\n        /// <exception cref=\"ArgumentException\">The <c>Kind</c> of <paramref name=\"dateTime\"/> is not <c>DateTimeKind.Utc</c>.</exception>\n        /// <returns>The converted timestamp.</returns>\n        public static Timestamp FromDateTime(DateTime dateTime)\n        {\n            if (dateTime.Kind != DateTimeKind.Utc)\n            {\n                throw new ArgumentException(\"Conversion from DateTime to Timestamp requires the DateTime kind to be Utc\", \"dateTime\");\n            }\n            // Do the arithmetic using DateTime.Ticks, which is always non-negative, making things simpler.\n            long secondsSinceBclEpoch = dateTime.Ticks / TimeSpan.TicksPerSecond;\n            int nanoseconds = (int)  (dateTime.Ticks % TimeSpan.TicksPerSecond) * Duration.NanosecondsPerTick;\n            return new Timestamp { Seconds = secondsSinceBclEpoch - BclSecondsAtUnixEpoch, Nanos = nanoseconds };\n        }\n\n        /// <summary>\n        /// Converts the given <see cref=\"DateTimeOffset\"/> to a <see cref=\"Timestamp\"/>\n        /// </summary>\n        /// <remarks>The offset is taken into consideration when converting the value (so the same instant in time\n        /// is represented) but is not a separate part of the resulting value. In other words, there is no\n        /// roundtrip operation to retrieve the original <c>DateTimeOffset</c>.</remarks>\n        /// <param name=\"dateTimeOffset\">The date and time (with UTC offset) to convert to a timestamp.</param>\n        /// <returns>The converted timestamp.</returns>\n        public static Timestamp FromDateTimeOffset(DateTimeOffset dateTimeOffset)\n        {\n            // We don't need to worry about this having negative ticks: DateTimeOffset is constrained to handle","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/WellKnownTypes/TimestampPartial.cs#L134-L170","documentation":"Timestamp.FromDateTime only accepts DateTime values whose Kind is DateTimeKind.Utc; anything Local or Unspecified is rejected with ArgumentException because the conversion to Unix-epoch seconds would silently misinterpret the wall-clock time. The library forces callers to convert explicitly rather than guessing a time zone.","triggerScenarios":"Calling FromDateTime (directly or via FromDateTimeOffset with a non-UTC offset) with dateTime.Kind == Local (e.g. DateTime.Now) or Unspecified (e.g. parsed from a string without offset info).","commonSituations":"Passing DateTime.Now instead of DateTime.UtcNow; strings parsed with DateTime.Parse producing Unspecified kind; values read from databases configured with local time; legacy code that stores local wall-clock times.","solutions":["Use DateTime.UtcNow or call dateTime.ToUniversalTime() before FromDateTime","For Unspecified values you know are UTC, use DateTime.SpecifyKind(dt, DateTimeKind.Utc)","For local wall-clock times with a known zone, convert with TimeZoneInfo.ConvertTimeToUtc(dt, zone) first","Prefer DateTimeOffset and Timestamp.FromDateTimeOffset when offsets are available"],"exampleFix":"// before\nvar ts = Timestamp.FromDateTime(DateTime.Now); // throws: Kind is Local\n// after\nvar ts2 = Timestamp.FromDateTime(DateTime.UtcNow);\n// or for a parsed value known to be UTC:\nvar parsed = DateTime.Parse(\"2024-01-01T12:00:00Z\").ToUniversalTime();\nvar ts3 = Timestamp.FromDateTime(parsed);","handlingStrategy":"try-catch","validationCode":"static DateTime EnsureUtc(DateTime dt) => dt.Kind == DateTimeKind.Utc ? dt : dt.ToUniversalTime();","typeGuard":"static bool IsUtc(DateTime dt) => dt.Kind == DateTimeKind.Utc;","tryCatchPattern":"try { return Timestamp.FromDateTime(dt); } catch (ArgumentException ex) { log.Warn(ex, \"Non-UTC DateTime Kind={Kind}\", dt.Kind); return Timestamp.FromDateTime(dt.ToUniversalTime()); }","preventionTips":["Use DateTime.UtcNow everywhere internally","After DateTime.Parse, call .ToUniversalTime() immediately","Use DateTimeOffset end-to-end to preserve offset information"],"tags":["protobuf","c-sharp","timestamp","datetime","timezone"],"backgroundTag":"invalid-argument-value","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}