Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-06-18 08:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.