SSブログ

Time difference conversion

post:2013-09-05
update:2014-01-15

Let's consider time difference conversion.
Suppose that there is the following ManyTime data.
This time zone is Japan Standard Time (JST).

input-ja.txt:
2013-01-04 00:15:00\t2013-01-04 13:10:00\tComment1
2013-01-06 11:12:00\t2013-01-07 18:08:00\tComment2

Notes:\t is a tab character. Finally a new-line is required.

This data can be changed into ManyTime data of the
Pacific Standard Time (PST) 17 hours before JST by
the shell script.

The shell script is performed and translates input-ja.txt to
the output. It works in the terminal on Mac OS X 10.8.4.
The date command attached to Mac is used for conversion of
the time difference in the shell script. This is the date
command of BSD system and options differ from SystemV system.

$ chmod u+x conv_jst-17.sh
$ ./conv_jst-17.sh input-ja.txt

output:
2013-01-04 00:15:00\t2013-01-04 13:10:00\tComment1
2013-01-06 11:12:00\t2013-01-07 18:08:00\tComment2

2013-01-03 07:15:00\t2013-01-03 20:10:00\tComment1
2013-01-05 18:12:00\t2013-01-07 01:08:00\tComment2

conv_jst-17.sh:
#!/bin/sh

cat $1
printf "\n"

while read line;
do
	st=`echo "$line" | cut -f1`
	et=`echo "$line" | cut -f2`
	cm=`echo "$line" | cut -f3`
	a=$(date -v-17H -j -f "%Y-%m-%d %H:%M:%S" "$st" "+%Y-%m-%d %H:%M:00")
	b=$(date -v-17H -j -f "%Y-%m-%d %H:%M:%S" "$et" "+%Y-%m-%d %H:%M:00")
	printf "%s\t" "$a"
	printf "%s\t" "$b"
	printf "%s\n" "$cm"
done < $1



The following is a Windows PowerShell version 3.0 script.
After storage in the default character code of Windows,
please use the PS script. In my japanese enviroment of Windows7 SP1,
the character code is Shift JIS, and new line is \r\n.
Convert to the appropriate character code in English environment.
Input-ja.txt is ManyTime data, character code is UTF-8 without BOM,
new line is \n. Replace \t to the tab character.

PS > .\conv_jst-17b.ps1 input-ja.txt

conv_jst-17b.ps1:
#Read the ManyTime data file, and be converted to the shifted data before 17 hours.

#Creating the input file path
$filepath = Split-Path $MyInvocation.MyCommand.Path
$inputFilePath = Join-Path $filepath $args[0]

#Get the input file contents
$inRecords = Get-Content $inputFilePath -Encoding UTF8

#The output of the input file contents
$inRecords
echo ""

#Set to 17 hours before
$offsetHours = -17
foreach($inRecord in $inRecords)
{
    $anArray = $inRecord.split("`t")
    #String start time of local time.
    $strStartTime = $anArray[0]
    $startTime = [datetime]::ParseExact($strStartTime,"yyyy-MM-dd HH:mm:ss", $null)
    #String end time of local time.
    $strEndTime = $anArray[1]
    $endTime = [datetime]::ParseExact($strEndTime,"yyyy-MM-dd HH:mm:ss", $null)
    #Comment
    $comment = $anArray[2]

    #Draw for 17 hours from the start time
    $startTime = $startTime.AddHours($offsetHours)
    $strStartTime = $startTime.ToString("yyyy-MM-dd HH:mm:ss")
    #Draw for 17 hours from the end time
    $endTime = $endTime.AddHours($offsetHours)
    $strEndTime = $endTime.ToString("yyyy-MM-dd HH:mm:ss")
    #Output
    $newLine = $strStartTime + "`t" + $strEndTime + "`t" + $comment
    $newLine
}

Creation of a garbag..Time difference conv.. ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。