-
-
Notifications
You must be signed in to change notification settings - Fork 947
Fix SFTP file UTC time handling #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Improved SFTP file UTC time handling
This is not only "improve" - I would say it is "correct" UTC time handling, see #415 |
@drieseng Is there any way you could bump up the priority on merging in this pull request please? I originally said "improved", but I actually agree with @stefer that this is the correct UTC time handling. I'd really like to stop having to maintain and ship my own fork because correct UTC time support is extremely important in my app. Thanks! |
@bmenees I first want to have an integration test to validate these changes. I hope to have time for that this weekend. |
Thanks! I really appreciate it and all of your hard work on this project! |
@drieseng Have you had a chance to validate these changes? I'd really like to get this pull request merged in. Thanks! |
@menees Sorry to inform you, but our policy is to not accept PRs from developers that prefer tabs over spaces. |
We may reconsider :) |
I'm really gonna try to find time tomorrow to create an integration test for this PR. |
Thanks. Regarding tabs vs. spaces, all of my changes in this PR use spaces for indentation. That's why I added the .editorconfig file. My "global" preference in VS is tabs, but the .editorconfig I added at the solution-level makes VS use and respect the solution-level setting of spaces (instead of my or anyone else's global preference). So, that's another advantage to merging this PR. In the future, anyone editing the SSH.NET code in VS 2017 or later (or any of the 20+ other editors that support .editorconfig) will automatically use spaces for indentation since that's what the .editorconfig declares as the solution's preference. Then you'll have fewer PRs using tabs (or none, hopefully). |
@menees I should've been more explicit: the fact that I closed the PR due to the tabs vs. spaces battle was a joke! Can you move the .editorconfig to a separate PR? I'd like to review/discuss the config (and again, I'm not talking about tabs vs. spaces) before we merge it. |
Removed .editorconfig from PR at [@drieseng's request](sshnet#356 (comment)).
I deleted .editorconfig from this PR because GitHub makes that easy. I really only need the fixes for UTC time handling merged in. |
@menees, thanks!! |
I changed SftpFileAttributes to preserve the last access and modified times as UTC values rather than always forcing a local conversion. This primarily affected the FromBytes(SshDataStream) method, but the changes cascade out from there to make sure that SftpFileAttributes and SftpFile can be used without ever forcing a (potentially lossy) conversion to local time. In the SSH data stream the atime and mtime values are "Unix times" in seconds, so they're already in UTC. We need to preserve that fact by returning and storing DateTime values with Kind == Utc.
Previously, the code called DateTime.FromFileTime. Internally, FromFileTime calls DateTime.ToLocalTime(), which is a lossy function. For example, any local time >= 1am and < 2am during the "fall back" hour of a daylight saving time shift in the fall could have come from two different UTC times. Avoiding this ambiguity is why file systems store times in UTC, so the SFTP classes need to preserve UTC times too. Here's a simple example to see this in VS's C# interactive window:
.NET also has an issue where it caches the current local time zone offset, so it can incorrectly calculate local times after a DST shift if nothing has called CultureInfo.ClearCachedData or TimeZoneInfo.ClearCachedData. This is discussed more at https://stackoverflow.com/a/297189/1882616, and it can be a big problem in long-running services if not handled correctly. By making SSH.NET internally preserve UTC times correctly, the library can be immune to problems with DST shifts for callers that only work with UTC times.
I also cleaned up some warnings in the unit tests and added an .editorconfig file so VS 2017 will know to use spaces for indentation in this solution (even though my global preference is set to tabs).