File: Timestamp.pm

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (116 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#========================================================================
#
# Badger::Codec::Timestamp
#
# DESCRIPTION
#   Codec module for encoding/decoding a timestamp via Badger::Timestamp
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Codec::Timestamp;

use Badger::Class
    version => 0.01,
    base    => 'Badger::Codec';

use Badger::Timestamp 'Timestamp';


sub encode {
    my $self = shift;
    return Timestamp(@_)->timestamp;
}


sub decode {
    my $self = shift;
    return Timestamp(@_);
}


1;


__END__

=head1 NAME

Badger::Codec::Timestamp - encode/decode a timestamp via Badger::Timestamp

=head1 SYNOPSIS

    use Badger::Codec::Timestamp;
    use Badger::Timestamp 'Now';

    my $codec   = Badger::Codec::Timestamp->new();
    my $encoded = $codec->encode(Now);
    my $decoded = $codec->decode($encoded);

=head1 DESCRIPTION

This module implements a subclass of L<Badger::Codec> for encoded and decoding
timestamps using the Badger::Timestamp module. It is trivially simple,
existing only to provide a consistent API with other L<Badger::Codec> modules.
It is typically used as a codec for reading and writing timestamps to and from
a file via the L<Badger::Filesystem> modules.

    use Badger::Filesystem 'File';
    use Badger::Timestamp 'Now';
    
    my $stamp = Now;                     # current data/time
    my $file  = File(
        'example.ts',                   # filename
        { codec => 'timestamp' }        # specify timestamp codec
    );
    
    # write timestamp to file
    $file->data($stamp);
    
    # read timestamp from file
    $stamp = $file->data;

=head1 METHODS

=head2 encode($timestamp)

Encodes the timestamp passed as an argument. The argument can be a
L<Badger::Timestamp> object or any of the constructor parameters accepted by
L<Badger::Timestamp>. The following example demonstrates how this works in
principle, although it should be noted that it's completely pointless in
practice. It is sufficient to simply call C<Now-E<gt>timestamp> to serialise a
L<Badger::Timestamp> to text without the need for any codec module (in fact,
that's all the C<encode()> method does behind the scenes).

    use Badger::Timestamp 'Now';
    $encoded = Badger::Codec::Timestamp->encode(Now);

=head2 decode($data)

Decodes the encoded timestamp passed as the first argument.  Returns a 
L<Badger::Timestamp> object.

=head1 AUTHOR

Andy Wardley L<http://wardley.org/>

=head1 COPYRIGHT

Copyright (C) 2012 Andy Wardley. All rights reserved.

=head1 SEE ALSO

L<Badger::Codecs>, L<Badger::Codec>, L<Badger::Timestamp>

=cut

# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: