From f0d8ff740ef6c450dcf190b8fc5f1edd2fb6fd8d Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 13 Dec 2007 07:26:44 +0000
Subject: [PATCH] Utilities: Added parseSAML2Time($time) which parses SAML2
 timestamps int UNIX timestamps.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@91 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 42 ++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index c9c40a423..3cf6ee6e5 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -174,6 +174,48 @@ class SimpleSAML_Utilities {
 			$depth++;
 		}
 	}
+
+
+	/* This function converts a SAML2 timestamp on the form
+	 * yyyy-mm-ddThh:mm:ssZ to a UNIX timestamp.
+	 *
+	 * Parameters:
+	 *  $time     The time we should convert.
+	 *
+	 * Returns:
+	 *  $time converted to a unix timestamp.
+	 */
+	public static function parseSAML2Time($time) {
+		$matches = array();
+
+
+		/* We use a very strict regex to parse the timestamp. */
+		if(preg_match('/^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)' .
+		              'T(\\d\\d):(\\d\\d):(\\d\\d)Z$/D',
+		              $time, $matches) == 0) {
+			throw new Exception(
+				'Invalid SAML2 timestamp passed to' .
+				' parseSAML2Time: ' . $time);
+		}
+
+		/* Extract the different components of the time from the
+		 * matches in the regex. intval will ignore leading zeroes
+		 * in the string.
+		 */
+		$year = intval($matches[1]);
+		$month = intval($matches[2]);
+		$day = intval($matches[3]);
+		$hour = intval($matches[4]);
+		$minute = intval($matches[5]);
+		$second = intval($matches[6]);
+
+		/* We use gmmktime because the timestamp will always be given
+		 * in UTC.
+		 */
+		$ts = gmmktime($hour, $minute, $second, $month, $day, $year);
+
+		return $ts;
+	}
 }
 
 ?>
\ No newline at end of file
-- 
GitLab