#!/usr/bin/env python
#coding: utf-8
#
# calclock UNIX時間計算（Open usp Tukubai版）
# 
# designed by USP lab.
# written by Ryuichi Ueda
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "[-r] <f1> <f2> <f3> ... filename"
_version = "Thu Mar 21 21:57:55 JST 2013"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4+, 3.1, 3.2/UTF-8)"

import os
import sys
import time
import calendar
import codecs

def die(msg):
	sys.stderr.write( 'Error[calclock] : %s\n' % msg)
	sys.exit(1)

def usage():
	sys.stderr.write( "Usage     :%s\n" % _usage );
	sys.stderr.write( "Version   :%s\n" % _version );
	sys.stderr.write( "	   %s\n" % _code );
	sys.exit(1)


def openReadFile(file_name):
	if file_name != "-":
		try:    return codecs.open(file_name,'r','utf-8')
		except: die("ファイルを開けません。")

	if sys.version_info[0] < 3:
		return codecs.getreader('utf-8')(sys.stdin)

	return codecs.getreader('utf-8')(sys.stdin.detach())

def resetStdout():
	if sys.version_info[0] < 3:
		return codecs.getwriter('utf-8')(sys.stdout)

	return codecs.getwriter('utf-8')(sys.stdout.detach())

def resetStderr():
	if sys.version_info[0] < 3:
		return codecs.getwriter('utf-8')(sys.stderr)

	return codecs.getwriter('utf-8')(sys.stderr.detach())

def revConv(field):
	#tm = time.localtime(float(field))
	tm = time.gmtime(float(field))

	return '%04d%02d%02d%02d%02d%02d' % (tm.tm_year, tm.tm_mon, tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec)

def conv(field,length):
	y = int(field[0:4])
	m = int(field[4:6])
	s = int(field[6:8])
	H, M, S = 0, 0, 0

	if length >= 12:
		H = int(field[8:10])
		M = int(field[10:12])

	if length == 14:
		S = int(field[12:14])

	if length == 8 or length == 12 or length == 14:
		return str(int(calendar.timegm((y, m, s, H, M, S, 0, 0, 0))))

	die("日付の指定が不正です。")

def normalMode(in_file,targets):
	for line in in_file:
		fs = line.rstrip().split(" ")

		for t in targets:
			fs[t] = fs[t] + " " + conv(fs[t],len(fs[t]))

		print(" ".join(fs))

def revMode(in_file,targets):
	for line in in_file:
		fs = line.rstrip().split(" ")

		for t in targets:
			fs[t] = fs[t] + " " + revConv(fs[t])

		print(" ".join(fs))

def getTargetFields(args):
	ans = []

	for e in args:
		try:
			target = int(e) - 1
		except:
			die("不正なフィールド指定です。")

		ans.append(target)

	return list(set(ans))
	

#メイン関数
if __name__ == '__main__':

	if len(sys.argv) < 2 :		usage()
	if sys.argv[1] == '--help' :	usage()
	if sys.argv[1] == '--version':	usage()

	# 標準入出力utf8化
	sys.stdout = resetStdout()

	# ファイル名の省略は許さない
	# 標準入力はハイフンで
	in_file = openReadFile(sys.argv[-1])

	if sys.argv[1] == "-r":
		target_fields = getTargetFields(sys.argv[2:-1])
		revMode(in_file,target_fields)
	else:
		target_fields = getTargetFields(sys.argv[1:-1])
		normalMode(in_file,target_fields)


