在bash中为字符串日期添加秒

时间:2019-03-11 07:39:26

标签: linux bash date

我有这样一个可变的字符串日期

 public Form1()
    {
        InitializeComponent();
        backgroundWorker1.Dispose();
        backgroundWorker1.DoWork += BackgroundWorker_DoWork;
        backgroundWorker1.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        backgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
        backgroundWorker1.WorkerReportsProgress = true;

        backgroundWorker2.DoWork += BackgroundWorker2_DoWork;
        backgroundWorker2.WorkerReportsProgress = true;
    }

    private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        File.Copy(sourcePath, targetPath);
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < fileSize; i++)
        {
            int p = (i + 1) * 100 / Convert.ToInt32(fileSize);
            backgroundWorker1.ReportProgress(p);
        }
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }

    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lbProgress.Text = e.ProgressPercentage.ToString();
        progressBar1.Value = e.ProgressPercentage;
    }

    private void btnTarget_Click(object sender, EventArgs e)
    {
        folderBrowser = new FolderBrowserDialog();
        if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            targetPath += folderBrowser.SelectedPath + @"\" + fileName;
            lbTarget.Text = targetPath;
        }
    }

    private void btnSource_Click(object sender, EventArgs e)
    {
        op = new OpenFileDialog();
        if (op.ShowDialog() == DialogResult.OK)
        {
            sourcePath += op.FileName;
            lbSource.Text = sourcePath;
            fileInfo = new FileInfo(sourcePath);
            fileSize = fileInfo.Length / 1024;
            fileName = fileInfo.Name;
            MessageBox.Show(string.Format("File size is: {0} KB", fileSize));
        }
    }

    private void btnCopy_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.RunWorkerAsync();
    }

我想将此变量加10秒,但我不知道如何。 可能是重复的帖子,但是我没有灵感去找到一个简单的答案。

1 个答案:

答案 0 :(得分:2)

转换为秒,加10,转换回日期字符串:

date +"%a %d %H:%M:%S" --date=@$(($(date +%s --date="Mar 15 09:27:26") + 10))

使用bash的$((...))算术替换和$(...)命令替换,以及GNU date的格式转换功能。 (注意:在Mac上,date的格式不同)