From b70f75e38baa1f1eeded9eea54920fd405a990eb Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Fri, 9 May 2025 22:56:03 +0200 Subject: [PATCH] use extension member for DateTime.UnixEpoch --- .../Abstractions/DateTimeExtensions.cs | 19 +++++++++++++++++++ src/Renci.SshNet/ScpClient.cs | 8 -------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 src/Renci.SshNet/Abstractions/DateTimeExtensions.cs diff --git a/src/Renci.SshNet/Abstractions/DateTimeExtensions.cs b/src/Renci.SshNet/Abstractions/DateTimeExtensions.cs new file mode 100644 index 00000000..5cf7cfad --- /dev/null +++ b/src/Renci.SshNet/Abstractions/DateTimeExtensions.cs @@ -0,0 +1,19 @@ +#nullable enable +namespace System +{ + internal static class DateTimeExtensions + { + extension(DateTime) + { +#if !NET + public static DateTime UnixEpoch + { + get + { + return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + } + } +#endif + } + } +} diff --git a/src/Renci.SshNet/ScpClient.cs b/src/Renci.SshNet/ScpClient.cs index 35ce5f5c..b95f361d 100644 --- a/src/Renci.SshNet/ScpClient.cs +++ b/src/Renci.SshNet/ScpClient.cs @@ -675,11 +675,7 @@ namespace Renci.SshNet /// The file or directory to upload. private void UploadTimes(IChannelSession channel, Stream input, FileSystemInfo fileOrDirectory) { -#if NET var zeroTime = DateTime.UnixEpoch; -#else - var zeroTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); -#endif var modificationSeconds = (long)(fileOrDirectory.LastWriteTimeUtc - zeroTime).TotalSeconds; var accessSeconds = (long)(fileOrDirectory.LastAccessTimeUtc - zeroTime).TotalSeconds; SendData(channel, string.Format(CultureInfo.InvariantCulture, "T{0} 0 {1} 0\n", modificationSeconds, accessSeconds)); @@ -856,11 +852,7 @@ namespace Renci.SshNet var mtime = long.Parse(match.Result("${mtime}"), CultureInfo.InvariantCulture); var atime = long.Parse(match.Result("${atime}"), CultureInfo.InvariantCulture); -#if NET var zeroTime = DateTime.UnixEpoch; -#else - var zeroTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); -#endif modifiedTime = zeroTime.AddSeconds(mtime); accessedTime = zeroTime.AddSeconds(atime); continue;