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