# EVERYWEEK.rb

# $Id: EVERYWEEK.rb,v 1.3 2002/05/04 00:58:38 kenji Exp $

# 2002/5/1  Ken-ichi HASHIMOTO <ken@po.ganseki.ne.jp>
# Copyright (C) 2002 Ken-ichi HASHIMOTO <ken@po.ganseki.ne.jp>
# This is free software with ABSOLUTELY NO WARRANTY.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


class RmpEVERYWEEK
	def setParameter( aParam )
		@parameter = aParam
	end

	def format( aTime )

		week = ['SUN','MON','TUE','WED','THU','FRI','SAT'].index(@parameter[1].upcase)

		if (aTime.wday == week) then

			sd = [aTime.year,aTime.month,aTime.day] 
			st = @parameter[2].split(/:/)
			et = @parameter[3].split(/:/)

			# omittion of second
			if st.size == 2 then
				st << 00
			end
			if et.size == 2 then
				et << 00
			end

			startTime = Time.mktime(sd[0],sd[1],sd[2],st[0],st[1],st[2])
			endTime = Time.mktime(sd[0],sd[1],sd[2],et[0],et[1],et[2])

			# date line
			if (startTime.to_i > endTime.to_i ) then
				endTime += 24*60*60
			end

			startDate2 = startTime.strftime("%Y/%m/%d")
			startTime2 = startTime.strftime("%H:%M:%S")
			endDate = endTime.strftime("%Y/%m/%d")
			endTime = endTime.strftime("%H:%M:%S")

			# auto date addition to file name 
			fileName = @parameter[6]
			today = startTime.strftime("%y%m%d")
			if (fileName =~ /(.+)\.(.+)/) then
				fileName = "#$1-#{today}.#$2"
			end

			return "#{startDate2} #{startTime2} #{endDate} #{endTime} #{@parameter[4]} #{@parameter[5]} #{fileName}"
		else
			return nil
		end
	end
end

