Unix 中 shell 脚本中的 YYYYMMDDHH 日期操作 [英] YYYYMMDDHH date manipulation in shell script in Unix

查看:46
本文介绍了Unix 中 shell 脚本中的 YYYYMMDDHH 日期操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为YYYYMMDDHH"的日期,我想从中增加或减少小时数.GNU 日期(date -d 选项)在 Linux 中就像一个魅力,但在 Solaris 等非 GNU 环境中无法做到这一点.有人可以帮助我如何做到这一点吗?

I have a date in format "YYYYMMDDHH" and I want to add or subtract hours from it. The GNU date (date -d option) works like a charm in Linux but cant do this in non-GNU env like Solaris. Can someone please help me in how I can do this?

推荐答案

你应该检查 gdate 是否已经安装在你的 Solaris 版本下(可能在 /usr/gnu/bin/date, /usr/sfw/bin/[g]date, /usr/local/bin/[g]date, /usr/csw/bin/[g]date 或只是 /usr/bin/gdate 取决于版本).如果没有,应该很容易找到包含 GNU 日期的包并安装它.

You should check if gdate is not already installed under your release of Solaris (might be in /usr/gnu/bin/date, /usr/sfw/bin/[g]date, /usr/local/bin/[g]date, /usr/csw/bin/[g]date or just /usr/bin/gdate depending on the version). If not, it should be easy to find a package containing GNU date and install it.

不管怎样,这里有一个 shell 函数,它应该只在 Solaris 发行版下工作,并且可以做我认为你想要的:

Anyway, here is a shell function that should just work under a stock Solaris release and that do what I believe you want:

f()
{
  echo $1 | perl -MTime::Local -nle '
  use POSIX 'strftime';
  $op='$2'*3600;
  $sec=timelocal(0,0,$4,$3,$2-1,$1) if /(d{4})(d{2})(d{2})(d{2})/;
  $sec=$sec+$op;
  print strftime "%Y%m%d%H
", localtime($sec);'
}

<小时>

$ f 2014010112 -24
2013123112

这篇关于Unix 中 shell 脚本中的 YYYYMMDDHH 日期操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆