-2

I'm interested in finding a solution to take the current time and date using C# and then using that value to set the time and date to set the clock in Linux. This would execute ideally on a button click in Visual Studio Windows Forms and would take the current time and date generated through C# to send that value to Linux OS via a packet. I'm just wondering how to format the value created in C# so that it translates in a way that is relevant by setting the date and time in Linux.

The date and time value is going to be sent to a separate computer running Linux that receives information from the windows based computer via a packet

DateTime now = DateTime.Now;

and then something like

date --set="now"

        
Justin
  • 357
  • 2
  • 7
  • 18
  • 1
    windows forms isn't supported on linux. – Daniel A. White Jan 07 '22 at 15:57
  • No I know. The date and time value is going to be sent to a separate computer running Linux that receives information from the windows based computer via a packet – Justin Jan 07 '22 at 16:02
  • What exactly means "send that value to Linux OS via a packet"? Do you want to run a separate c# app on the linux computer and set the time in this app or do you want to set the date from the windows machine, e.g. via SSH? – Christoph Lütjen Jan 07 '22 at 17:25
  • Related: https://stackoverflow.com/questions/30883237/how-to-run-commands-on-ssh-server-in-c – Christoph Lütjen Jan 07 '22 at 17:27

1 Answers1

1

See if this helps at all. I am assuming you already have the ability to send commands to the Linux machine using an SSH library or something similar. Using the total seconds saves you from having to format any complicated date/time string.

long unixSeconds = DateTimeOffset.Now.ToUnixTimeSeconds(); // Need someway to get the total seconds from epoch

string sshCommand =  "date --set @" + unixSeconds.ToString();
psant3
  • 41
  • 2