File: string-time.t

package info (click to toggle)
libtest-mocktime-perl 0.17-0%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 100 kB
  • sloc: perl: 239; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,736 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/perl 

use Test::MockTime();
use Test::More(tests => 18);
use Time::Local();
use strict;
use warnings;

my ($mock, $real);

# determine the correct epoch value for our test time
my $TRUE = Time::Local::timegm(19, 25, 3, 30, 6, 2006);

# set_absolute_time with a defaulted date spec
Test::MockTime::set_absolute_time('2006-07-30T03:25:19Z');
$mock = time;
ok(($mock >= $TRUE) && ($mock <= $TRUE+1), "Absolute time works");
sleep 2;
$mock = time;
ok(($mock >= $TRUE+2) && ($mock <= $TRUE+3), "Absolute time is still in sync after two seconds sleep:$mock");
$mock = Time::Local::timelocal(localtime);
$real = Time::Local::timelocal(CORE::localtime);
ok($mock <= $real, "localtime seems ok");

# set_absolute_time with an explicit date spec
Test::MockTime::set_absolute_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y');
$mock = time;
ok(($mock >= $TRUE) && ($mock <= $TRUE+1), "Absolute time with explicit date specworks");
sleep 2;
$mock = time;
ok(($mock >= $TRUE+2) && ($mock <= $TRUE+3), "Absolute time is still in sync after two seconds sleep:$mock");
$real = Time::Local::timelocal(CORE::localtime);
ok($mock <= $real, "localtime seems ok");

# try set_fixed_time with a defaulted date spec
Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z');
$real = time;
sleep 2;
$mock = time;
cmp_ok($mock, '==', $real, "time is fixed");
cmp_ok($mock, '==', $TRUE, "time is fixed correctly");
Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z');
$mock = Time::Local::timelocal(localtime());
sleep 2;
$real = Time::Local::timelocal(localtime);
cmp_ok($mock, '==', $real, "localtime is fixed");
cmp_ok($mock, '==', $TRUE, "localtime is fixed correctly");
Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z');
$mock = Time::Local::timegm(gmtime);
sleep 2;
$real = Time::Local::timegm(gmtime);
cmp_ok($mock, '==', $real, "gmtime is fixed");
cmp_ok($mock, '==', $TRUE, "gmtime is fixed correctly");

# try set_fixed_time with an explicit date spec
Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y');
$real = time;
sleep 2;
$mock = time;
cmp_ok($mock, '==', $real, "time is fixed with explicit date spec");
cmp_ok($mock, '==', $TRUE, "time is fixed correctly");
Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y');
$mock = Time::Local::timelocal(localtime());
sleep 2;
$real = Time::Local::timelocal(localtime);
cmp_ok($mock, '==', $real, "localtime is fixed");
cmp_ok($mock, '==', $TRUE, "localtime is fixed correctly");
Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y');
$mock = Time::Local::timegm(gmtime);
sleep 2;
$real = Time::Local::timegm(gmtime);
cmp_ok($mock, '==', $real, "gmtime is fixed");
cmp_ok($mock, '==', $TRUE, "gmtime is fixed correctly");