#!/usr/bin/perl # This is the TimeChannels Artificial System Clock implementation class for Perl. # Everywhere in your application codebase that obtains the date/time directly, replace with a call # to the getSystemDate() sub in this file. # # For simple unit-testing, you can call setToStaticTestState() and pass it a specific date-time value. # # For system, integration, acpt testing, call setToTestState() and pass it a specific channel id you have setup in # the TimeChannels.com control panel. # # Return to normal time by calling setToProductionState() # # NOTE: Requires LWP bundle in order to work. # Use this line: # # perl -MCPAN -e 'install Bundle::LWP' # # to install it. package TimeChannels; require LWP::UserAgent; use strict; use Time::Local; my $tcURL = "http://www.timechannels.com/GetChannel"; my $tcURLms = "http://www.timechannels.com/GetChannelMS"; my $tcAscState = "TimeChannels.state.Production"; my $tcChannel = "0"; my $tcAscDateTime = 0; my $tcServiceURL = "http://www.timechannels.com/GetChannel"; my $tcMillisecondMode = "false"; sub getState { return $tcAscState; } sub setToProductionState { $tcAscState = "TimeChannels.state.Production"; } sub setToStaticTestState { my ($parm1, $parm2) = @_; $tcAscState = "TimeChannels.state.TestDate"; $tcAscDateTime = $parm2; } sub setToTestState { my ($parm1, $parm2) = @_; $tcAscState = "TimeChannels.state.TestChannel"; $tcChannel = $parm2; } sub setToMillisecondMode { $tcMillisecondMode = "true"; $tcServiceURL = $tcURLms; } sub setToStandardMode { $tcMillisecondMode = "false"; $tcServiceURL = $tcURL; } sub getSystemDate { if ($tcAscState eq "TimeChannels.state.Production") { return time; } if ($tcAscState eq "TimeChannels.state.TestDate") { return $tcAscDateTime; } if ($tcAscState eq "TimeChannels.state.TestChannel") { return TimeChannels->getChannelFromTimeChannelsService(); } return time; } sub getChannelFromTimeChannelsService { my $url = $tcServiceURL . '?channel=' . $tcChannel; use LWP::Simple; my $channelValue = get $url; die "Could not get date-time channel from $url" unless defined $channelValue; my $retVal = time; if ($tcMillisecondMode eq "true") { $retVal = (1 * $channelValue) / 1000; } else { my $year = substr($channelValue, 0, 4); my $month = (1 * substr($channelValue, 5, 2)) - 1; my $day = 1* substr($channelValue, 8, 2); my $hour = 1 * substr($channelValue, 11, 2); my $minute = 1 * substr($channelValue, 14, 2); my $ampm = substr($channelValue, 17, 2); if ($ampm eq "PM") { $hour += 12; } $retVal = timelocal(0, $minute, $hour, $day, $month, $year); } return $retVal; }