Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Edited src/com/gamestamper/graph/utils/GamestamperDataUtils.as via Gi…

…tHub
  • Loading branch information...
commit a58ba2de39e01a59a9f76d2a018a3c4079d396f0 1 parent 9b5c3c7
GameStamper authored

Showing 1 changed file with 213 additions and 1 deletion. Show diff stats Hide diff stats

  1. +213 1 src/com/gamestamper/graph/utils/GamestamperDataUtils.as
214 src/com/gamestamper/graph/utils/GamestamperDataUtils.as
... ... @@ -1 +1,213 @@
1   -/* Copyright (c) 2010, Adobe Systems Incorporated All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Adobe Systems Incorporated nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.gamestamper.graph.utils { import flash.net.URLVariables; /** * Utility class used * to parse Gamestamper data into corresponding ActionScript types. * */ public class GamestamperDataUtils { /** * Attempts to convert the various Gamestamper date formats * to a Date object. * * Supported formats are: * 1270706413 (unix timestamp), in Seconds * 2010-03-20T22:46:41+0000 (ISO 8601) * 11/30/1973 * */ public static function stringToDate(value:String):Date { if (value == null) { return null; } //########################### //Check for different date formats //########################### //If we have unix timestamp, parse it first //We need to convert this time to milliseconds, from seconds. if (/[^0-9]/g.test(value) == false) { return new Date(parseInt(value)*1000); } //Parse dates in this format: 11/30/1973 or 10/30 //Most likey will be a users birthday. if (/(\d\d)\/(\d\d)(\/\d+)?/ig.test(value)) { var datePeices:Array = value.split('/'); return new Date(datePeices[2], datePeices[1], datePeices[0]); } /* Parse dates in this format: 2010-03-20T22:46:41+0000 (ISO 8601) http://www.w3.org/TR/NOTE-datetime Note this does not match the entire date, just that significant portion exists. Also does not match smaller format dates, only full date / times with time zones */ if (/\d{4}-\d\d-\d\d[\sT]\d\d:\d\d(:\d\d)?[\.\-Z\+]?(\d{0,4})?(\:)?(\-\d\d:)?/ig.test(value)) { return iso8601ToDate(value); } //We don't know the format, let Date try and parse it. return new Date(value); } /** * @private * */ protected static function iso8601ToDate(value:String):Date { var parts:Array = value.toUpperCase().split('T'); var date:Array = parts[0].split('-'); var time:Array = (parts.length <= 1) ? [] : parts[1].split(':'); var year:uint = date[0]=='' ? 0 : Number(date[0]); var month:uint = date[1]=='' ? 0 : Number(date[1] - 1); var day:uint = date[2]=='' ? 1 : Number(date[2]); var hour:int = time[0]=='' ? 0 : Number(time[0]); var minute:uint = time[1]=='' ? 0 : Number(time[1]); var second:uint = 0; var millisecond:uint = 0; if (time[2] != null) { var index:int = time[2].length; if (time[2].indexOf('+') > -1) { index = time[2].indexOf('+'); } else if (time[2].indexOf('-') > -1) { index = time[2].indexOf('-'); } else if (time[2].indexOf('Z') > -1) { index = time[2].indexOf('Z'); } if (isNaN(index)) { var temp:Number = Number(time[2].slice(0, index)); second = temp<<0; millisecond = 1000 * ((temp % 1) / 1); } if (index != time[2].length) { var offset:String = time[2].slice(index); var userOffset:Number = new Date(year, month, day).getTimezoneOffset() / 60; switch (offset.charAt(0)) { case '+' : case '-' : hour -= userOffset + Number(offset.slice(0)); break; case 'Z' : hour -= userOffset; break; } } } return new Date(year, month, day, hour, minute, second, millisecond); } /** * Utility method to convert a Date object * to seconds since Jan 1, 1970 for use on Gamestamper. * */ public static function dateToUnixTimeStamp(date:Date):uint { return date.time/1000; } /** * Converts a multidimensional array into a one dimensional array. * Used to ensure that ExtendedPermissions * passed to Gamestamper are correct. * * @param source Array to flatten. * */ public static function flattenArray(source:Array):Array { if (source == null) { return []; } return GamestamperDataUtils.internalFlattenArray(source); } /** * * Obtains the query string from the current HTML location * and returns its values in a URLVariables instance. * */ public static function getURLVariables(url:String):URLVariables { var params:String; if (url.indexOf('#') != -1) { params = url.slice(url.indexOf('#')+1); } else if (url.indexOf('?') != -1) { params = url.slice(url.indexOf('?')+1); } var vars:URLVariables = new URLVariables(); vars.decode(params); return vars; } /** * @private * */ private static function internalFlattenArray(source:Array, destination:Array = null ):Array { if (destination == null) { destination = []; } var l:uint = source.length; for (var i:uint=0;i<l;i++) { var item:Object = source[i]; if (item is Array) { GamestamperDataUtils.internalFlattenArray(item as Array, destination ); } else { destination.push(item); } } return destination; } } }
  1 +/*
  2 + Copyright (c) 2010, Adobe Systems Incorporated
  3 + All rights reserved.
  4 +
  5 + Redistribution and use in source and binary forms, with or without
  6 + modification, are permitted provided that the following conditions are
  7 + met:
  8 +
  9 + * Redistributions of source code must retain the above copyright notice,
  10 + this list of conditions and the following disclaimer.
  11 +
  12 + * Redistributions in binary form must reproduce the above copyright
  13 + notice, this list of conditions and the following disclaimer in the
  14 + documentation and/or other materials provided with the distribution.
  15 +
  16 + * Neither the name of Adobe Systems Incorporated nor the names of its
  17 + contributors may be used to endorse or promote products derived from
  18 + this software without specific prior written permission.
  19 +
  20 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22 + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23 + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31 +*/
  32 +
  33 +package com.gamestamper.graph.utils {
  34 + import flash.net.URLVariables;
  35 +
  36 + /**
  37 + * Utility class used
  38 + * to parse Gamestamper data into corresponding ActionScript types.
  39 + *
  40 + */
  41 + public class GamestamperDataUtils {
  42 +
  43 + /**
  44 + * Attempts to convert the various Gamestamper date formats
  45 + * to a Date object.
  46 + *
  47 + * Supported formats are:
  48 + * 1270706413 (unix timestamp), in Seconds
  49 + * 2010-03-20T22:46:41+0000 (ISO 8601)
  50 + * 11/30/1973
  51 + *
  52 + */
  53 + public static function stringToDate(value:String):Date {
  54 + if (value == null) { return null; }
  55 +
  56 + //###########################
  57 + //Check for different date formats
  58 + //###########################
  59 +
  60 + //If we have unix timestamp, parse it first
  61 + //We need to convert this time to milliseconds, from seconds.
  62 + if (/[^0-9]/g.test(value) == false) {
  63 + return new Date(parseInt(value)*1000);
  64 + }
  65 +
  66 + //Parse dates in this format: 11/30/1973 or 10/30
  67 + //Most likey will be a users birthday.
  68 + if (/(\d\d)\/(\d\d)(\/\d+)?/ig.test(value)) {
  69 + var datePeices:Array = value.split('/');
  70 + return new Date(datePeices[2], datePeices[1], datePeices[0]);
  71 + }
  72 +
  73 + /*
  74 + Parse dates in this format: 2010-03-20T22:46:41+0000 (ISO 8601)
  75 + http://www.w3.org/TR/NOTE-datetime
  76 +
  77 + Note this does not match the entire date,
  78 + just that significant portion exists.
  79 +
  80 + Also does not match smaller format dates,
  81 + only full date / times with time zones
  82 + */
  83 + if (/\d{4}-\d\d-\d\d[\sT]\d\d:\d\d(:\d\d)?[\.\-Z\+]?(\d{0,4})?(\:)?(\-\d\d:)?/ig.test(value)) {
  84 + return iso8601ToDate(value);
  85 + }
  86 +
  87 + //We don't know the format, let Date try and parse it.
  88 + return new Date(value);
  89 + }
  90 +
  91 + /**
  92 + * @private
  93 + *
  94 + */
  95 + protected static function iso8601ToDate(value:String):Date {
  96 + var parts:Array = value.toUpperCase().split('T');
  97 +
  98 + var date:Array = parts[0].split('-');
  99 + var time:Array = (parts.length <= 1) ? [] : parts[1].split(':');
  100 +
  101 + var year:uint = date[0]=='' ? 0 : Number(date[0]);
  102 + var month:uint = date[1]=='' ? 0 : Number(date[1] - 1);
  103 + var day:uint = date[2]=='' ? 1 : Number(date[2]);
  104 + var hour:int = time[0]=='' ? 0 : Number(time[0]);
  105 + var minute:uint = time[1]=='' ? 0 : Number(time[1]);
  106 +
  107 + var second:uint = 0;
  108 + var millisecond:uint = 0;
  109 +
  110 + if (time[2] != null) {
  111 + var index:int = time[2].length;
  112 +
  113 + if (time[2].indexOf('+') > -1) {
  114 + index = time[2].indexOf('+');
  115 + } else if (time[2].indexOf('-') > -1) {
  116 + index = time[2].indexOf('-');
  117 + } else if (time[2].indexOf('Z') > -1) {
  118 + index = time[2].indexOf('Z');
  119 + }
  120 +
  121 + if (isNaN(index)) {
  122 + var temp:Number = Number(time[2].slice(0, index));
  123 + second = temp<<0;
  124 + millisecond = 1000 * ((temp % 1) / 1);
  125 + }
  126 +
  127 + if (index != time[2].length) {
  128 + var offset:String = time[2].slice(index);
  129 + var userOffset:Number =
  130 + new Date(year, month, day).getTimezoneOffset() / 60;
  131 +
  132 + switch (offset.charAt(0)) {
  133 + case '+' :
  134 + case '-' :
  135 + hour -= userOffset + Number(offset.slice(0));
  136 + break;
  137 + case 'Z' :
  138 + hour -= userOffset;
  139 + break;
  140 + }
  141 + }
  142 + }
  143 +
  144 + return new Date(year, month, day, hour, minute, second, millisecond);
  145 + }
  146 +
  147 + /**
  148 + * Utility method to convert a Date object
  149 + * to seconds since Jan 1, 1970 for use on Gamestamper.
  150 + *
  151 + */
  152 + public static function dateToUnixTimeStamp(date:Date):uint {
  153 + return date.time/1000;
  154 + }
  155 +
  156 + /**
  157 + * Converts a multidimensional array into a one dimensional array.
  158 + * Used to ensure that ExtendedPermissions
  159 + * passed to Gamestamper are correct.
  160 + *
  161 + * @param source Array to flatten.
  162 + *
  163 + */
  164 + public static function flattenArray(source:Array):Array {
  165 + if (source == null) { return []; }
  166 + return GamestamperDataUtils.internalFlattenArray(source);
  167 + }
  168 +
  169 + /**
  170 + *
  171 + * Obtains the query string from the current HTML location
  172 + * and returns its values in a URLVariables instance.
  173 + *
  174 + */
  175 + public static function getURLVariables(url:String):URLVariables {
  176 + var params:String;
  177 +
  178 + if (url.indexOf('#') != -1) {
  179 + params = url.slice(url.indexOf('#')+1);
  180 + } else if (url.indexOf('?') != -1) {
  181 + params = url.slice(url.indexOf('?')+1);
  182 + }
  183 +
  184 + var vars:URLVariables = new URLVariables();
  185 + vars.decode(params);
  186 + return vars;
  187 + }
  188 +
  189 + /**
  190 + * @private
  191 + *
  192 + */
  193 + private static function internalFlattenArray(source:Array,
  194 + destination:Array = null
  195 + ):Array {
  196 +
  197 + if (destination == null) { destination = []; }
  198 +
  199 + var l:uint = source.length;
  200 + for (var i:uint=0;i<l;i++) {
  201 + var item:Object = source[i];
  202 + if (item is Array) {
  203 + GamestamperDataUtils.internalFlattenArray(item as Array,
  204 + destination
  205 + );
  206 + } else {
  207 + destination.push(item);
  208 + }
  209 + }
  210 + return destination;
  211 + }
  212 + }
  213 +}

0 comments on commit a58ba2d

Please sign in to comment.
Something went wrong with that request. Please try again.