SSブログ

Time difference conversion II

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

Let's consider time difference conversion, again.

Suppose that there is the following data.
This data is a timetable used in U.S., odd lines are time of onset and
even lines are termination time.
This time zone is Pacific Standard Time (PST) .
Although there is no comment data in this data, it becomes two events
in ManyTime data.

input-1.txt:
06 03, 2013 7:15 AM
06 03, 2013 8:10 PM
06 05, 2013 6:12 PM
06 07, 2013 1:08 AM

Notes:Finally a new-line is required.

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

The shell script is performed and translates input-1.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_pst+17.sh
$ ./conv_pst+17.sh input-1.txt

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

Notes:\t is a tab character.

conv_pst+17.sh:
#!/bin/sh
isodd="1"
while read line;
do
	if [ "$isodd" = "1" ];
	then
		isodd="0"
		a=$(date -v+17H -j -f "%m %d, %Y %H:%M %p" "$line" "+%Y-%m-%d %H:%M:00")
	else
		isodd="1"
		b=$(date -v+17H -j -f "%m %d, %Y %H:%M %p" "$line" "+%Y-%m-%d %H:%M:00")
		printf "%s\t" "$a"
		printf "%s\n" "$b"
	fi
done < $1



The following is a Windows PowerShell version 3.0 script.
After storage in the default character code of Windows, use the PS script.
In my japanese environment of Windows7 SP1, character code is Shift JIS and
new line is \r\n. Input-1.txt is also Shift JIS and new line is \r\n.
Convert to the appropriate character code in English environment.

PS > .\conv_pst+17d.ps1 input-1.txt

conv_pst+17d.ps1:
#Read the file of two rows data set with "MM dd, yyyy h:mm tt" format.
#Be converted to the shifted data after 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

# The body of the script
$isodd = 1
#Set to 17 hours after
$offsetHours = 17
$strStartTime = ""
$strEndTime = ""
foreach($inRecord in $inRecords)
{
    if ($isodd -eq 1) {
        $isodd = 0
        #String start time of local time.
        $strStartTime = $inRecord
        #Conversion to the start time from string start time.
        $startTime = [datetime]::ParseExact($strStartTime,"MM dd, yyyy h:mm tt", [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US"))
        #Add 17 hours to the start time.
        $startTime = $startTime.AddHours($offsetHours)
        #Conversion to the string start time from the start time is added for 17 hours.
        $strStartTime = $startTime.ToString("yyyy-MM-dd HH:mm:ss")
    }
    else {
        $isodd = 1
        #String end time of local time.
        $strEndTime = $inRecord
        #Conversion to the end time from end time string.
        $endTime = [datetime]::ParseExact($strEndTime,"MM dd, yyyy h:mm tt", [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US"))
        #Add 17 hours to the end time.
        $endTime = $endTime.AddHours($offsetHours)
        #Conversion to the string end time from the end time is added for 17 hours.
        $strEndTime = $endTime.ToString("yyyy-MM-dd HH:mm:ss")
        #Output
        $newLine = $strStartTime + "`t" + $strEndTime
        $newLine
    }
}

Time difference conv..Sample data ブログトップ

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