Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Serialzed Date Recurrence

by Boldra (Deacon)
on Mar 09, 2017 at 10:28 UTC ( [id://1184017]=perlquestion: print w/replies, xml ) Need Help??

Boldra has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to save an event recurrence pattern into a database, such that after I fetch a row, I can ask "->next" ? To see the next event.

Specifically, I am expecting users to store "weekly", "quarterly" and "quarterly, but one week later" in the field. I then need to query the database for "any resource available in date range a--b," but I'll actually be fetching the whole table and querying it in Perl, because we're trying to stay database agnostic.

It looks like the most widely recognised formats are cron and iCal. I can't seem to find good CPAN support for iCal recurrences. The DateTime::Event::Cron module looks very good, but cron won't directly store quarters; you need two entries. There's also DateTime::Event::Recurrence, but that seems to have no serialization/inflation. Maybe I could dump the object as perl and store that in the database, but yuck.

Does anyone have any good suggestions for this? Thanks



- Boldra

Replies are listed 'Best First'.
Re: Serialzed Date Recurrence
by haukex (Archbishop) on Mar 11, 2017 at 10:40 UTC
    There's also DateTime::Event::Recurrence, but that seems to have no serialization/inflation.

    I think that what the AM was trying to suggest was that you could indeed serialize the objects in a similar fashion as the clone/copy methods. However, looking into an object's internals is usually a brittle solution, as the internals might change across versions without being reflected in the change logs. Instead, I'd suggest serializing based on the object's API, which is less likely to change (at least until someone implements serialization natively for those objects :-) ). Here's a simple idea, just store the values from %params in whatever form you like in the database:

    use DateTime::Event::Recurrence; my %params = ( recur=>'daily', hours=>10, minutes=>30 ); my $recur = delete $params{recur}; my $event = DateTime::Event::Recurrence->$recur(%params); # same as DT::E::Recurrence->daily( hours=>10, minutes=>30 )
Re: Serialzed Date Recurrence
by Anonymous Monk on Mar 09, 2017 at 12:34 UTC

    There's also DateTime::Event::Recurrence, but that seems to have no serialization/inflation. Maybe I could dump the object as perl and store that in the database, but yuck.

    huh?

    sub clone { my $self = shift; my $new = $self->SUPER::clone( @_ ); $new->set_ical( $self->get_ical ); $new; } sub clone { my $self = bless { %{ $_[0] } }, ref $_[0]; $self->{set} = $_[0]->{set}->copy; return $self; } sub copy { my $self = shift; my $copy = $self->empty_set(); ## return $copy unless ref($self); # constructor! foreach my $key (keys %{$self}) { if ( ref( $self->{$key} ) eq 'ARRAY' ) { @{ $copy->{$key} } = @{ $self->{$key} }; } else { $copy->{$key} = $self->{$key}; } } return $copy; }
    https://metacpan.org/pod/Set::Infinite#as_string
      You're saying that because "clone" works by serializing/deserializing, I could use that?

        You're saying that because "clone" works by serializing/deserializing, I could use that?

        huh?

        I think I'm saying, its open source, so copy/paste the source and modify as required and fill in the blanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1184017]
Approved by haukex
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-18 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found