<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-03 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.value;

import java.text.Collator;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

import org.apache.oro.text.perl.Perl5Util;
import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;

/**
 * Represents a value of type xs:dateTime.
 * 
 * TODO: This is in large parts copied from Saxon and needs to be revised.
 * 
 * @author <NAME> (<EMAIL>)
 */
public class DateTimeValue extends AbstractDateTimeValue {

	// dateTime format is: [+|-]yyyy-mm-ddThh:mm:ss[.fff*][([+|-]hh:mm | Z)]
	private static final String regex =
		"/(\\+|-)?(\\d{4})-([0-1]\\d)-(\\d{2})T([0-2]\\d):([0-6][0-9]):([0-6][0-9])(\\.(\\d{1,3}))?(.*)/";
	private static final String tzre = "/(\\+|-)?([0-1]\\d):(\\d{2})/";

	public DateTimeValue() {
		calendar = new GregorianCalendar();
		tzOffset =
			(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))
			/ 60000;
		date = calendar.getTime();
	}
	
	public DateTimeValue(GregorianCalendar cal, int timezone) {
		calendar = (GregorianCalendar)cal.clone();
		tzOffset = timezone;
		explicitTimeZone = true;
		date = calendar.getTime();
	}
	
	public DateTimeValue(long milliseconds, int timezone) {
		tzOffset = timezone;
		explicitTimeZone = true;
		SimpleTimeZone zone = new SimpleTimeZone(tzOffset * 60000, "LLL");
		calendar = new GregorianCalendar(zone);
		calendar.setLenient(false);
		calendar.setTimeInMillis(milliseconds);
		calendar.set(Calendar.ZONE_OFFSET, tzOffset * 60000);
		date = calendar.getTime();
	}
	
	public DateTimeValue(long milliseconds) {
		calendar = new GregorianCalendar();
		tzOffset =
			(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))
			/ 60000;
		calendar.setTimeInMillis(milliseconds);
		date = calendar.getTime();
	}
	
	public DateTimeValue(String dateTime) throws XPathException {
		Perl5Util util = new Perl5Util();
		if (!util.match(regex, dateTime))
			throw new XPathException(
				"Type error: string " + dateTime + " cannot be cast into an xs:dateTime");
		String part = util.group(1);
		int era = 1;
		if (part != null && part.equals("-"))
			era = -1;
		part = util.group(2);
		int year = Integer.parseInt(part) * era;
		part = util.group(3);
		int month = Integer.parseInt(part);
		part = util.group(4);
		int day = Integer.parseInt(part);
		part = util.group(5);
		int hour = Integer.parseInt(part);
		part = util.group(6);
		int minutes = Integer.parseInt(part);
		part = util.group(7);
		int seconds = Integer.parseInt(part);
		part = util.group(9);
		int millis = 0;
		if (part != null) {
			if (part.length() < 3)
				part += "00";
			if (part.length() > 3)
				part = part.substring(0, 3);
			millis = Integer.parseInt(part);
		}
		part = util.group(10);
		if (part != null && part.length() > 0 && (!part.equals("Z"))) {
			if (!util.match(tzre, part))
				throw new XPathException("Type error: error in  timezone: " + part);
			explicitTimeZone = true;
			part = util.group(2);
			tzOffset = Integer.parseInt(part) * 60;
			part = util.group(3);
			if (part != null) {
				int tzminute = Integer.parseInt(part);
				tzOffset += tzminute;
			}
			part = util.group(1);
			if (part.equals("-"))
				tzOffset *= -1;
		}
		SimpleTimeZone zone = new SimpleTimeZone(tzOffset * 60000, "LLL");
		if(explicitTimeZone)<commit_msg>Applied patch by Piotr to fix wrong timezone handling.<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-03 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.value;

import java.text.Collator;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

import org.apache.oro.text.perl.Perl5Util;
import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;

/**
 * Represents a value of type xs:dateTime.
 * 
 * TODO: This is in large parts copied from Saxon and needs to be revised.
 * 
 * @author <NAME> (<EMAIL>)
 */
public class DateTimeValue extends AbstractDateTimeValue {

	// dateTime format is: [+|-]yyyy-mm-ddThh:mm:ss[.fff*][([+|-]hh:mm | Z)]
	private static final String regex =
		"/(\\+|-)?(\\d{4})-([0-1]\\d)-(\\d{2})T([0-2]\\d):([0-6][0-9]):([0-6][0-9])(\\.(\\d{1,3}))?(.*)/";
	private static final String tzre = "/(\\+|-)?([0-1]\\d):(\\d{2})/";

	public DateTimeValue() {
		calendar = new GregorianCalendar();
		tzOffset =
			(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))
			/ 60000;
		date = calendar.getTime();
	}
	
	public DateTimeValue(GregorianCalendar cal, int timezone) {
		calendar = (GregorianCalendar)cal.clone();
		tzOffset = timezone;
		explicitTimeZone = true;
		date = calendar.getTime();
	}
	
	public DateTimeValue(long milliseconds, int timezone) {
		tzOffset = timezone;
		explicitTimeZone = true;
		SimpleTimeZone zone = new SimpleTimeZone(tzOffset * 60000, "LLL");
		calendar = new GregorianCalendar(zone);
		calendar.setLenient(false);
		calendar.setTimeInMillis(milliseconds);
		calendar.set(Calendar.ZONE_OFFSET, tzOffset * 60000);
		date = calendar.getTime();
	}
	
	public DateTimeValue(long milliseconds) {
		calendar = new GregorianCalendar();
		tzOffset =
			(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))
			/ 60000;
		calendar.setTimeInMillis(milliseconds);
		date = calendar.getTime();
	}
	
	public DateTimeValue(String dateTime) throws XPathException {
		Perl5Util util = new Perl5Util();
		if (!util.match(regex, dateTime))
			throw new XPathException(
				"Type error: string " + dateTime + " cannot be cast into an xs:dateTime");
		String part = util.group(1);
		int era = 1;
		if (part != null && part.equals("-"))
			era = -1;
		part = util.group(2);
		int year = Integer.parseInt(part) * era;
		part = util.group(3);
		int month = Integer.parseInt(part);
		part = util.group(4);
		int day = Integer.parseInt(part);
		part = util.group(5);
		int hour = Integer.parseInt(part);
		part = util.group(6);
		int minutes = Integer.parseInt(part);
		part = util.group(7);
		int seconds = Integer.parseInt(part);
		part = util.group(9);
		int millis = 0;
		if (part != null) {
			if (part.length() < 3)
				part += "00";
			if (part.length() > 3)
				part = part.substring(0, 3);
			millis = Integer.parseInt(part);
		}
		part = util.group(10);
		if (part != null && part.length() > 0) {
			explicitTimeZone = true;
			if (part.equals("Z")) {
				tzOffset = 0;
			} else {
				if (!util.match(tzre, part))
					throw new XPathException("Type error: error in  timezone: " + part);
				part = util.group(2);
				tzOffset = Integer.parseInt(part) * 60;
				part = util.group(3);
				if (part != null) {
					int tzminute = Integer.parseInt(part);
					tzOffset += tzminute;
				}
				part = util.group(1);
				if (part.equals("-"))
					tzOffset *= -1;
			}
		}
		SimpleTimeZone zone = new SimpleTimeZone(tzOffset * 60000, "LLL");
		if(explicitTimeZone)
{'fraction_non_alphanumeric': 0.14458955223880596, 'fraction_numerical': 0.02063899253731343, 'mean_word_length': 3.6437466161342718, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3511879', 'n_tokens_mistral': 3213, 'n_tokens_neox': 2836, 'n_words': 795}