Linux的庆典 - 在自定义格式解析日期 [英] linux bash - Parse date in custom format

查看:117
本文介绍了Linux的庆典 - 在自定义格式解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在%C日期格式的(可以是任何其他)的,我需要在date命令来使用它。 %C 是不是美国的格式。这是因为它是德国的服务器德国之一。这还没有一个美国服务器上正常工作。 (区域设置设置为德国或美国)

I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)

这不工作(包括误差):

This does not work (error included):

user@server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user@server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET"

(日期:ungültiges基准做19费尔南德斯2013年22点33分28秒CET= 日期:无效的日期你19费尔南德斯2013年22点33分28秒CET

困难的是,我不知道哪个区域,甚至WHCI日期格式将在以后使用,因为用户可以设置自己的格式。因此,一个简单的具体解析方案IST没有真正去上班!

The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!

但我怎么办呢?

要gerneralize问题:

To gerneralize the issue:

如果我有一个日期格式格式1 (可以是任何或者至少是一个可以逆转)的我可以用时间来获得格式化的日期。但是,如果我想将它格式化为另一日期( FORMAT2 )我该怎么办呢?结果
使用以外的任何其他coreutils的任何解决方案是没有意义的,因为我试图建立一个bash脚本尽可能多的UNIX机器越好。

If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.

DATE=$(date "+$format1")

date --date="$DATE" "+$format2" # Error in most cases!

这是必需的,因为我有一个命令,用户可以给一个日期格式。这个日期字符串是要被显示。但在后面的步骤我需要此日期字符串转换成另一种固定之一。我可以操纵whcih格式的命令将获得我可以maniplulate输出的(或者是什么,用户会看到)的。结果
因为这是非常耗时的,我不能运行命令的两倍。

This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.

我已经找到类似的解决方案:

I have found something like a solution:

# Modify $user_format so it can be parsed later
user_format="$user_format %s"

# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)

# This will only display what $user_format used to be
echo ${output% *}

# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")

这是工作,可能是帮助他人。因此,我将与您分享。

This is working and might be helpful to others. So I will share this with you.

推荐答案

你为什么不保存时间unixtime(即自1970年1月1日毫秒)像 1388198714

Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?

在试图从世界作为一个一杆bash脚本各地解析所有日期格式的要求行使无合理依赖条件是有点可笑。

The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.

这篇关于Linux的庆典 - 在自定义格式解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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