Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

problem in Rfc2080 date of imap server

by luckypower (Beadle)
on Sep 24, 2008 at 11:25 UTC ( #713400=perlquestion: print w/replies, xml ) Need Help??
luckypower has asked for the wisdom of the Perl Monks concerning the following question:

Hellow monks, I am writing a perl script to connect IMAP server.
i am using Mail::IMAPClient module.

This function will return the date & time of server.

but the problem comes here.
when i run this script, time() function will calculate seconds from my systems current date.
now i use this seconds as parameter of Rfc2060_datetime function so it will calculate the date & time of server form that seconds.

Any help ....??

Replies are listed 'Best First'.
Re: problem in Rfc2080 date of imap server
by jethro (Monsignor) on Sep 24, 2008 at 12:54 UTC

    Why do you think that Rfc2060_datetime returns the time of the server? The man page of Mail::IMAPClient says different:

    "The Rfc822_date method accepts one input argument, a number of seconds since the epoch date. It returns an RFC822 compliant date string for that date (without the 'Date:' prefix). Useful for putting dates in message strings before calling append, search, etcetera."

      Surely it does/should return the time on the server if the supplied parameter is time() ?

      .sig : File not found.

        He seems to program a client (executed on the client machine), so time() should get the date and time of the client not the server.
Re: problem in Rfc2080 date of imap server
by broomduster (Priest) on Sep 24, 2008 at 15:14 UTC
    Most of this has been said in one way or another in other replies, but I still sense a bit of confusion, so here goes....

    Mail::IMAPClient documentation (and the actual code) say that Rfc2060_datetime returns a date string that is properly formatted for use in various requests to the IMAP server (e.g., delete messages older than N days). It does not report the server's notion of current date and time. The following (untested; I don't have Mail::IMAPClient available to test) should work:

    use Mail::IMAPClient; my $now = time(); my $num_days = 5; # season to taste my $timestamp_n_daysago = $now - $num_days * 86400; #86400 secs/day my $zone = '-0400'; # EDT, season to taste, defaults to '+0000' (GMT0 +) my $date = Mail::IMAPClient->Rfc2060_datetime($timestamp_n_daysago, $z +one);
    Now $date should be a properly formatted date string for SEARCH, etc. commands to the IMAP server, with the following caveats:
    • it is important that the clock on the local system is set correctly; a few seconds are not crucial, but if the date is off by weeks there could be some ugly surprises... :-)
    • $zone should really be the time zone used by the IMAP server. Many mail servers run with GMT0, but YMMV.
    • computation of $timestamp_ndaysago ignores DST changes and such; a better (i.e., more accurate) solution would use one of the DateTime modules to compute date offsets (all of which is irrelevant if the IMAP server is running it's clock in GMT0);

    Update: Now that I'm at $HOME, where I have Mail::IMAPClient and can run the above code, it needs a few tweaks and some additional explanation based on re-reading the docs. First the code:
    use strict; use warnings; use Mail::IMAPClient; my $now = time(); my $zone = '-0400'; # EDT, season to taste, defaults to '+0000' (GMT0 +) my $zone_correction = 3600 * $zone / 100; # timezone correction in s +ecs print "zone_correction: $zone_correction\n"; $now += $zone_correction; my $date = Mail::IMAPClient->Rfc2060_datetime($now, $zone); print "date_time now: $date\n"; my $num_days = 5; # season to taste my $timestamp_n_daysago = $now - $num_days * 86400; #86400 secs/day $date = Mail::IMAPClient->Rfc2060_datetime($timestamp_n_daysago, $zone +); print "date_time $num_days days ago: $date\n";

    From the shell:

    -> date && ./test Wed Sep 24 16:01:32 EDT 2008 zone_correction: -14400 date_time now: 24-Sep-2008 16:01:32 -0400 date_time 5 days ago: 19-Sep-2008 16:01:32 -0400

    And by way of additional explanation:

    The documentation for Mail::IMAPClient->Rfc2060_datetime says:

    The timestamp follows the definition of the output of the platforms specific "time", usually in seconds since Jan 1st 1970. However, you have to correct the number yourself for the zone.
    In other words, if I want the resulting string to give the date/time in my current time zone (EDT, which is GMT-0400), I need to adjust the output of time appropriately. Hence the changes to compute a $zone_correction and adjust $now before calling Mail::IMAPClient->Rfc2060_datetime.
Re: problem in Rfc2080 date of imap server
by wol (Hermit) on Sep 24, 2008 at 12:54 UTC
    Unfortunately, I don't understand what the problem is you're describing. The code looks fine. Your interpretation looks fine.

    Could you post the time on your system, and the string returned by Rfc2060_datetime(time) ?

    .sig : File not found.

      Actully I want do delete msgs older than x days from server date not from client date. so i need the server date so i can calculate the timelimit.
        OK - I've found the same song sheet as everyone else now.

        As far as I can see, the IMAP protocol doesn't include an operation to ask the server what it thinks the time is. Perhaps I've missed something?

        I can think of a couple of other ideas, but I don't really like them...

        • Use NTP (Network Time Protocol) to ask the server, which should be fine so long as the server is running an NTP server as well as IMAP server and the firewall doesn't block it. Are you able to influence these factors at all?
          Timezone issues? Dunno.
        • Create a message on the server, query it's date, and delete the message again. Sorry.
        If you excuse me, I feel a little nauseous...

        .sig : File not found.

        Normally a difference of seconds, minutes or even hours should be negligable when you want to delete messages older than 5 days for example. Do you need to be exact or do you want to prevent accidents from a faulty HW-clock on the client (usually an imap-server should use ntp already and have the exact time) ? In the second case something like Net::NTP might help

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://713400]
Approved by toolic
[haukex]: here's the code that uses it
[Corion]: haukex: Hmm - maybe I can reuse that. I see that it uses Pod::Parser, which I think was one of the more fragile parsers. But if I'm statically (re)generating the tests instead of doing that "live" on the client/tester machines, that's a much smaller...
[Corion]: ... problem space than trying to cater to all versions of Pod::Parser(s)
[haukex]: I think you're right, I think Pod::Simple is the preferred parser now
[haukex]: But I was just using it as an author test anyway
[Corion]: haukex: Aaah - I thought you were still running these tests on every machine, but you only run these as author or Devel::Cover tests
[Corion]: haukex: Yeah, I think back then I used Test::Inline, which used a pod parser that was going through some changes and I didn't want to cater for all the various versions and thus stopped testing the Pod completely
[choroba]: I usually do this with presentations
[Corion]: But now I think statically (re)generating the Pod tests is a saner approach, and likely I'll regenerate the tests either in Makefile.PL or from xt/ but have them live below t/
[choroba]: I keep the snippets in files of their own, and use a Makefile to syntax highlight them and insert them into slides, while also running them and inserting the output if required

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-02-27 12:22 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.