From 90bfeea02c7c820ae53a55b0feeb352faaf46313 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Tue, 14 Nov 2017 12:34:52 +0100
Subject: [PATCH] let Rose::DB::Object handle dates

---
 conf/create-manager-db.sql                   |  6 +++---
 lib/IdPAccountManager/AuthenticationToken.pm |  8 ++++----
 lib/IdPAccountManager/TestAccount.pm         | 15 ++++++++-------
 3 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/conf/create-manager-db.sql b/conf/create-manager-db.sql
index 98fc34e..a62370f 100644
--- a/conf/create-manager-db.sql
+++ b/conf/create-manager-db.sql
@@ -11,7 +11,7 @@ CREATE TABLE `authenticationtokens` (
   `token` varchar(50) NOT NULL,
   `email_address` varchar(200) NOT NULL,
   `sp_entityid` varchar(200) NOT NULL,
-  `creation_date` int(11) DEFAULT NULL,
+  `creation_date` date DEFAULT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `token_2` (`token`),
   KEY `token` (`token`),
@@ -30,8 +30,8 @@ CREATE TABLE `serviceproviders` (
 CREATE TABLE `testaccounts` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT,
   `password_hash` varchar(50) NOT NULL,
-  `creation_date` int(11) DEFAULT NULL,
-  `expiration_date` int(11) DEFAULT NULL,
+  `creation_date` date DEFAULT NULL,
+  `expiration_date` date DEFAULT NULL,
   `profile` varchar(100) NOT NULL,
   `scope` varchar(100) NOT NULL,
   `sp_entityid` varchar(250) NOT NULL,
diff --git a/lib/IdPAccountManager/AuthenticationToken.pm b/lib/IdPAccountManager/AuthenticationToken.pm
index e1995df..cbc3315 100644
--- a/lib/IdPAccountManager/AuthenticationToken.pm
+++ b/lib/IdPAccountManager/AuthenticationToken.pm
@@ -6,7 +6,7 @@ use warnings;
 use base 'IdPAccountManager::DB::Object';
 
 use Digest::MD5;
-use POSIX qw(strftime);
+use DateTime;
 
 __PACKAGE__->meta->setup(
     table   => 'authenticationtokens',
@@ -16,7 +16,7 @@ __PACKAGE__->meta->setup(
         token         => { type => 'varchar', length => 50, not_null => 1 },
         email_address => { type => 'varchar', length => 200, not_null => 1 },
         sp_entityid   => { type => 'varchar', length => 200, not_null => 1 },
-        creation_date => { type => 'integer' },
+        creation_date => { type => 'date' },
     ],
 
     primary_key_columns => [ 'id' ],
@@ -35,7 +35,7 @@ sub print {
 "AuthenticationToken ID=%s; token=%s; email_address=%s; sp_entityid=%s; creation_date=%s\n",
       $self->id(), $self->token(), $self->email_address(),
       $self->sp_entityid(),
-      POSIX::strftime('%Y:%m:%d', localtime($self->creation_date()));
+      $self->creation_date()->strftime('%Y:%m:%d');
 }
 
 sub save {
@@ -43,7 +43,7 @@ sub save {
 
     # If no ID is defined, it is a new account
     if (! defined $self->id()) {
-        $self->creation_date(time);
+        $self->creation_date(DateTime->today());
         $self->token(_generate_token($self->email_address()));
     }
 
diff --git a/lib/IdPAccountManager/TestAccount.pm b/lib/IdPAccountManager/TestAccount.pm
index 824f14c..bb36912 100644
--- a/lib/IdPAccountManager/TestAccount.pm
+++ b/lib/IdPAccountManager/TestAccount.pm
@@ -5,7 +5,7 @@ use warnings;
 
 use base 'IdPAccountManager::DB::Object';
 
-use POSIX qw(strftime);
+use DateTime;
 
 __PACKAGE__->meta->setup(
     table   => 'testaccounts',
@@ -13,8 +13,8 @@ __PACKAGE__->meta->setup(
     columns => [
         id              => { type => 'bigserial', not_null => 1 },
         password_hash   => { type => 'varchar', length => 50, not_null => 1 },
-        creation_date   => { type => 'integer' },
-        expiration_date => { type => 'integer' },
+        creation_date   => { type => 'date' },
+        expiration_date => { type => 'date' },
         profile         => { type => 'varchar', length => 100, not_null => 1 },
         scope           => { type => 'varchar', length => 100, not_null => 1 },
         sp_entityid     => { type => 'varchar', length => 250, not_null => 1 },
@@ -121,8 +121,8 @@ sub print {
       $self->sp_entityid(),
       $self->profile(),
       $self->scope(),
-      POSIX::strftime('%Y:%m:%d', localtime($self->creation_date())),
-      POSIX::strftime('%Y:%m:%d', localtime($self->expiration_date()));
+      $self->creation_date()->strftime('%Y:%m:%d'),
+      $self->expiration_date()->strftime('%Y:%m:%d');
 }
 
 sub password {
@@ -140,9 +140,10 @@ sub save {
             IdPAccountManager::Tools::generate_password();
         $self->password_hash(
             IdPAccountManager::Tools::sha256_hash($self->{password}));
-        $self->creation_date(time);
+        $self->creation_date(DateTime->today());
         $self->expiration_date(
-            time + ($args{accounts_validity_period} * 3600 * 24));
+            DateTime->today()->add(days => $args{accounts_validity_period})
+        );
     }
 
     $self->SUPER::save();
-- 
GitLab