http://www.perlmonks.org?node_id=713446


in reply to problem in Rfc2080 date of imap server

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:
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.