Skip to content

Commit

Permalink
refactor: conversion from DateTimeOffset to unix time, ref #52
Browse files Browse the repository at this point in the history
  • Loading branch information
julienmoumne committed Apr 20, 2017
1 parent eaa79ea commit bbf5fb9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 95 deletions.
20 changes: 8 additions & 12 deletions Piwik.Tracker.Tests/PiwikTrackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,17 @@ public void SetAttributionInfo_WhenCampaignNameSpecified_IsAddedToRequest(string
[TestCase("")]
public void SetAttributionInfo_WhenReferrerTimestampSpecified_IsAddedToRequest(string referrerDateTime)
{
if (string.IsNullOrEmpty(referrerDateTime))
var expectedTs = "1486223506"; // cf http://xmillis.com/l1c9bu4i.9e
var timestampProvided = !string.IsNullOrEmpty(referrerDateTime);
if (timestampProvided)
{
// Assert
var actual = _sut.GetRequest(SiteId);
Assert.That(actual, Does.Not.Contain("&_refts="));
}
else
{
// Arrange, Act
var referrerTimestamp = DateTimeOffset.Parse(referrerDateTime, CultureInfo.InvariantCulture);
_sut.SetAttributionInfo(new AttributionInfo { ReferrerTimestamp = referrerTimestamp });
// Assert
var actual = _sut.GetRequest(SiteId);
Assert.That(actual, Does.Contain("&_refts=" + referrerTimestamp.ToUnixTimeSeconds()));
var attrInfo = new AttributionInfo {ReferrerTimestamp = referrerTimestamp};
Assert.That(attrInfo.ToArray()[2], Is.EqualTo(expectedTs));
_sut.SetAttributionInfo(attrInfo);

}
Assert.That(_sut.GetRequest(SiteId), timestampProvided ? Does.Contain("&_refts=" + expectedTs) : Does.Not.Contain("&_refts="));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Piwik.Tracker/AttributionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public string[] ToArray()
var infos = new string[4];
infos[0] = CampaignName;
infos[1] = CampaignKeyword;
infos[2] = (ReferrerTimestamp - Constants.UnixEpoch).TotalSeconds.ToString(CultureInfo.InvariantCulture);
infos[2] = DateTimeUtils.ConvertToUnixTime(ReferrerTimestamp);
infos[3] = ReferrerUrl;
return infos;
}
Expand Down
12 changes: 0 additions & 12 deletions Piwik.Tracker/Constants.cs

This file was deleted.

16 changes: 16 additions & 0 deletions Piwik.Tracker/DateTimeUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Globalization;

namespace Piwik.Tracker
{
using System;

internal static class DateTimeUtils
{
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static string ConvertToUnixTime(DateTimeOffset date)
{
return (date - UnixEpoch).TotalSeconds.ToString(CultureInfo.InvariantCulture);
}
}
}
116 changes: 58 additions & 58 deletions Piwik.Tracker/Piwik.Tracker.csproj
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C3BB0E94-F45C-4691-81FF-061FC20E3209}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Piwik.Tracker</RootNamespace>
<AssemblyName>Piwik.Tracker</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Piwik.Tracker.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\Piwik.Tracker.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AttributionInfo.cs" />
<Compile Include="BrowserPlugins.cs" />
<Compile Include="Constants.cs" />
<Compile Include="CryptoExtensions.cs" />
<Compile Include="CustomVar.cs" />
<Compile Include="Enums.cs" />
<Compile Include="PiwikTracker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TrackingResponse.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C3BB0E94-F45C-4691-81FF-061FC20E3209}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Piwik.Tracker</RootNamespace>
<AssemblyName>Piwik.Tracker</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Piwik.Tracker.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\Piwik.Tracker.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AttributionInfo.cs" />
<Compile Include="BrowserPlugins.cs" />
<Compile Include="DateTimeUtils.cs" />
<Compile Include="CryptoExtensions.cs" />
<Compile Include="CustomVar.cs" />
<Compile Include="Enums.cs" />
<Compile Include="PiwikTracker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TrackingResponse.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
-->
</Project>
17 changes: 5 additions & 12 deletions Piwik.Tracker/PiwikTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public class PiwikTracker
private bool _configCookiesDisabled;
private string _configCookiePath = DefaultCookiePath;
private string _configCookieDomain = "";
private readonly long _currentTs = (long)(DateTime.UtcNow - Constants.UnixEpoch).TotalSeconds;
private readonly long _currentTs = (long)(DateTime.UtcNow - DateTimeUtils.UnixEpoch).TotalSeconds;
private long _createTs;
private long? _visitCount = 0;
private long? _currentVisitTs;
Expand Down Expand Up @@ -1329,7 +1329,7 @@ public AttributionInfo GetAttributionInfo()

if (arraySize > 2 && !string.IsNullOrEmpty(cookieDecoded[2]))
{
attributionInfo.ReferrerTimestamp = Constants.UnixEpoch.AddSeconds(Convert.ToInt32(cookieDecoded[2]));
attributionInfo.ReferrerTimestamp = DateTimeUtils.UnixEpoch.AddSeconds(Convert.ToInt32(cookieDecoded[2]));
}

if (arraySize > 3 && !string.IsNullOrEmpty(cookieDecoded[3]))
Expand Down Expand Up @@ -1526,7 +1526,7 @@ internal string GetRequest(int idSite)
(!_localTime.Equals(DateTimeOffset.MinValue) ? "&h=" + _localTime.Hour + "&m=" + _localTime.Minute + "&s=" + _localTime.Second : "") +
((_width != 0 && _height != 0) ? "&res=" + _width + "x" + _height : "") +
(_hasCookies ? "&cookie=1" : "") +
(!_ecommerceLastOrderTimestamp.Equals(DateTimeOffset.MinValue) ? "&_ects=" + FormatTimestamp(_ecommerceLastOrderTimestamp) : "") +
(!_ecommerceLastOrderTimestamp.Equals(DateTimeOffset.MinValue) ? "&_ects=" + DateTimeUtils.ConvertToUnixTime(_ecommerceLastOrderTimestamp) : "") +

// Various important attributes
// todo _customData is never assigned!
Expand All @@ -1548,7 +1548,7 @@ internal string GetRequest(int idSite)
// Campaign keyword
((_attributionInfo != null && !string.IsNullOrEmpty(_attributionInfo.CampaignKeyword)) ? "&_rck=" + UrlEncode(_attributionInfo.CampaignKeyword) : "") +
// Timestamp at which the referrer was set
((_attributionInfo != null && !_attributionInfo.ReferrerTimestamp.Equals(DateTimeOffset.MinValue)) ? "&_refts=" + FormatTimestamp(_attributionInfo.ReferrerTimestamp) : "") +
((_attributionInfo != null && !_attributionInfo.ReferrerTimestamp.Equals(DateTimeOffset.MinValue)) ? "&_refts=" + DateTimeUtils.ConvertToUnixTime(_attributionInfo.ReferrerTimestamp) : "") +
// Referrer URL
((_attributionInfo != null && !string.IsNullOrEmpty(_attributionInfo.ReferrerUrl)) ? "&_ref=" + UrlEncode(_attributionInfo.ReferrerUrl) : "") +

Expand Down Expand Up @@ -1709,7 +1709,7 @@ protected void SetCookie(string cookieName, string cookieValue, long cookieTtl)
if (HttpContext.Current != null)
{
var cookieExpire = _currentTs + cookieTtl;
HttpContext.Current.Response.Cookies.Add(new HttpCookie(GetCookieName(cookieName), cookieValue) { Expires = Constants.UnixEpoch.AddSeconds(cookieExpire), Path = _configCookiePath, Domain = _configCookieDomain });
HttpContext.Current.Response.Cookies.Add(new HttpCookie(GetCookieName(cookieName), cookieValue) { Expires = DateTimeUtils.UnixEpoch.AddSeconds(cookieExpire), Path = _configCookiePath, Domain = _configCookieDomain });
}
}

Expand All @@ -1732,13 +1732,6 @@ private string FormatDateValue(DateTimeOffset date)
return date.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
}

private string FormatTimestamp(DateTimeOffset date)
{
TimeSpan diff = date - Constants.UnixEpoch;
double seconds = Convert.ToInt32(diff.TotalSeconds);
return seconds.ToString(CultureInfo.InvariantCulture);
}

private string FormatMonetaryValue(double value)
{
return value.ToString("0.##", new CultureInfo("en-US"));
Expand Down

0 comments on commit bbf5fb9

Please sign in to comment.