2
4

2 回答 2

1

You probably will have to tinker with some environment variables (ex: TZ, LC_ALL, etc).

See this page showing you most of the common environnement variables, and their meanings

To try some: you can force the value to change just for the duration of the following command by putting them on the same line, before the command itself:

TZ=.... LC_LANG=..... date -d .......

will invoke date -d .... with the 2 environment variables TZ and LC_LANG set to a temporary value.

Some interresting pointers (I can't right now tell if there is a program that will take as input any locale's date and translate that to the relevant Epoch or Unix Timestamp... BUt there seems to be hope following that (looking quite standard) trail of online docs:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02

which talks, amongst many other, about:

LC_TIME
    This variable shall determine the locale category for date and time formatting information. It affects the behavior of the time functions in strftime(). Additional semantics of this variable, if any, are implementation-defined.

Which points to: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdate.html

which says in the middle:

The match between the template and input specification performed by getdate() shall be case-insensitive.

The month and weekday names can consist of any combination of upper and lowercase letters. The process can request that the input date or time specification be in a specific language by setting the LC_TIME category (see setlocale ).

and points to: http://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html

... I wish you an happy reading ! Let us know what you find!

于 2013-01-08T18:12:22.863 回答
1

I finally figured this out with the aid of the coreutils mailing list. However, the example they give there uses perl. They specifically rely on the POSIX::strptime module, which does not come with a standard installation of perl. Therefore, I solved this with python, which has the time module. This module should be available in most installations of python2 and python3.

Here's how to use it programmatically:

Python solution:

$ timestamp='2013年1月8日 20時19分'
$ time_format='%Y年%m月%d日 %H時%M分'
$ gdate -u -R -d "$(python -c 'import sys; from time import strptime; t=strptime(sys.argv[-1],"'$time_format'"); print("%d-%d-%d %d:%d"%(t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min))' $timestamp)"
Tue, 08 Jan 2013 20:19:00 +0000

This works with both python2 and python3. You can substitute any timestamp and format as you like.

Perl solution

To document the answer given to me on coreutils, the perl solution is this (requires POSIX::strptime)

$ gdate -u -R -d "$(perl -MPOSIX::strptime -le 'my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("$ARGV[0]","%Y年%m月%d日 %H時%M分");$year+=1900;$mon+=1;printf("%04d-%02d-%02d %0d:%02d\n",$year,$mon,$mday,$hour,$min);' "2013年1月8日 20時19分")"
于 2013-01-23T04:47:17.887 回答