I am using Perl to create a script that will email password expiration notifications.
I have two dates:
The date that the users password was set
The date that the users password will expire (180 days after the password was set)
use DateTime::Format::Strptime;
my $dt_pattern = DateTime::Format::Strptime->new( pattern => '%F',);
my $displayName = $entry->get_value("displayName");
my $pwdLastSet = convertWinFileTimestamp($entry->get_value("pwdLastSet"));
# Determine password expiration date
my $pwdLastSet_dt = $dt_pattern->parse_datetime($pwdLastSet);
my $pwdExpirationDate = $pwdLastSet_dt->add( days => $maxPwdAge );
# Days until password expires
# HELP!!!!!!!
sub convertWinFileTimestamp {
my $timestamp = shift;
# Strip off nanoseconds, then adjust date from AD epoch (1601) to UNIX epoch (1970)
return POSIX::strftime( "%Y-%m-%d",
localtime( ( $timestamp / 10000000 ) - 11644473600 ) );
}
I cannot figure out how to calculate the difference between the two dates!
Below is the output for each variable:
pwdLastSet: 2015-02-12
pwdExpireDate: 2015-08-11T00:00:00
Any help much appreciated...been googling like crazy but I can't figure it out...Thanks!
I tried the following lines of code:
my $pwdDaysLeft = int(($pwdExpirationDate - $pwdLastSet) / 86400);
but got the following error:
Only a DateTime::Duration or DateTime object can be subtracted from a DateTime object. at pwdreminder.pl line 65
So, we have three dates here:
The date that the password was last set. This starts off as a string in the format YYYY-MM-DD stored in $pwdLastSet, but then you parse it into a DateTime object stored in $pwdLastSet_dt.
The date that the current password expires. This is calculated by adding $maxPwdAge days to $pwdLastSet_dt, which gives a DateTime object which is then stored in $pwdExpirationDate.
The current date. Which, in your current code, you don't calculate.
What you actually want is the difference in days a between the second and third of these two dates. We can ignore the first date as is it only used to calculate the second date. I assume that you're calculating that correctly.
Hopefully, the password expiration date will always be in the future. So the calculation we want to do is:
my $diff = $pwdExpirationDate - $current_date;
As long as both of those are DateTime objects, we'll get a DateTime::Duration object back, which we can then ask for the number of days.
DateTime has a today() method that will give the current date. So our code becomes:
# Use delta_days() to get a duration object that just contains days
my $diff = $pwdExpirationDate->delta_days(DateTime->today);
print $diff->in_units('days');
Related
I was wondering if someone could help me.
I'm very new at ASP I want to format the current date and time as follows:
yyyy-mm-dd hh:mm:ss
But all i can do is the following
Response.Write Date
Can someone help me out please.
Date formatting options are limited in Classic ASP by default, there is a function FormatDateTime() which can format your date is various ways based on the servers regional settings.
For more control over date formatting though there are built in date time functions
Year(date) - Returns a whole number representing the year. Passing Date() will give back the current year.
Month(date) - Returns a whole number between 1 and 12, inclusive, representing the month of the year. Passing Date() will return the current month of the year.
MonthName(month[, abbv]) - Returns a string indicating the specified month. Passing in Month(Date()) as the month will give back the current Month string. As suggested by #Martha
Day(date) - Returns a whole number between 1 and 31, inclusive, representing the day of the month. Passing Date() will return the current day of the month.
Hour(time) - Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Passing Time() will return the current hour.
Minute(time) - Returns a whole number between 0 and 59, inclusive, representing the minute of the hour. Passing Time() will return the current minute.
Second(time) - Returns a whole number between 0 and 59, inclusive, representing the second of the minute. Passing Time() will return the current second.
IMPORTANT:
When formatting date / time values, always store the date / time value first. Also, any needed calculations (DateAdd() etc.) should be applied before attempting to format or you will get unexpected results.
The functions Month(), Day(), Hour(), Minute() and Second() all return whole numbers. Luckily there is an easy workaround that lets you pad these values quickly Right("00" & value, 2) what it does is append 00 to the front of the value then from the right take the first two characters. This ensures that all single digit values return prefixed with a 0.
Dim dd, mm, yy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue
'Store DateTimeStamp once.
dtsnow = Now()
'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)
'Build the date string in the format yyyy-mm-dd
datevalue = yy & "-" & mm & "-" & dd
'Build the time string in the format hh:mm:ss
timevalue = hh & ":" & nn & ":" & ss
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
dtsvalue = datevalue & " " & timevalue
Call Response.Write(dtsvalue)
Note: You can build the date string in one call but decided to break it down into the three variables to make it easier to read.
How Can I Format Date
Example of Parsing a Date String (Answers provide approaches to taking a date string format and parsing it to a valid Date variable).
Format the date of the previous day format yyyymmdd with VBScript (Example of why storing date / time before performing formatting is important)
VBScript ISO8601 (Example of functions to construct an ISO 8601 compliant date string)
New to perl. I have a string that is in this form 20190123120445, i.e. YYYYMMDDHHMISS. In perl how do you turn this into a timestamp that can be used to subtract another timestamp generated from a Time::Hires time timestamp. I know the timestamps are different resolutions and will assume that the first timestamp starts at 0 ms.
I can turn the timestamp into a DateTime object, however attempting to subtract the hires timer value result in error.
How do I turn the first string into a timestamp of the same resolution as the time timestamp, so that I can subtract the values and get a delta? Or is there a more obvious solution?
use Time::Hires;
use Date::Parse;
my $parser = DateTime::Format::Strptime->new(
pattern => '%Y%m%d%H%M%S',
on_error => 'croak',
);
my $dt = $parser->parse_datetime($args->{EVENTCREATEDTIMESTAMP});
my $delta = time - $dt;
If I attempt to do this, I get this error
Bad message vendor or subscription: Cannot subtract 1548265276 from a
DateTime object (DateTime=HASH(0x28e10d98)). Only a DateTime::Duration
or DateTime object can be subtracted from a DateTime object.
To submit it as a proper answer: To get an epoch timestamp which is the same format you get from time, call the epoch method on the DateTime object. You can easily subtract epoch timestamps to get a difference in seconds, and then convert that to larger denominations. Time::Seconds provides useful constants for this if you prefer.
use strict;
use warnings;
use Time::Seconds;
my $diff = time - $dt->epoch;
my $diff_hours = $diff / ONE_HOUR;
If you want a calendar duration difference, things get complicated. This is because there is no static definition of things like "one month" or even "one day" and "one minute", because of gross things like daylight savings and leap seconds. So the difference depends on the time zone and the absolute start and end time. The simplest way to deal with this is turn your epoch timestamp into a DateTime object and have DateTime do the work for you.
my $dt_time = DateTime->from_epoch(epoch => time);
# Each of the following returns DateTime::Duration objects with different measures of calendar time
my $diff_duration = $dt_time->subtract_datetime($dt); # months, days, minutes, seconds, nanoseconds
my $diff_days = $dt_time->delta_days($dt); # full days
my $diff_ms = $dt_time->delta_ms($dt); # minutes and seconds
my $diff_abs = $dt_time->subtract_datetime_absolute($dt); # seconds and nanoseconds
The individual components of the resulting DateTime::Duration objects can be retrieved with the in_units method or by passing it to DateTime::Format::Duration. The subtract_datetime_absolute method is the only way to count leap seconds - epoch timestamps effectively ignore them, and "minutes" from the other methods may not be 60 seconds long.
I need to get the last day of a month previous from specified.
I have a string (file name), which contains a date in its end. I need to capture the date (already done) and get the last date of the preceding month. For example the string is "proemail vytvoreni_9.2017 2017-10-16", so I need to get 30th September 2017. This is what I have now:
$Report = Read-Host "File name"
$Date = [datetime]$Report.Substring($Report.get_Length()-10)
$Last_month = $Date.AddMonths(-1)
$Date_text = $Last_month.ToString().Substring(3,7)
$month_year = ($Date_text.Split("."))
$days_count = [datetime]::DaysInMonth($month_year[1],$month_year[0])
$days_count = $days_count.ToString()
$month = $month_year[0]
$year = $month_year[1]
$Date_limit = [DateTime]($month,$days_count,$year)
All works well, except for the last row, that returns this error: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.DateTime". I tried to convert $month and $year to string by .ToString() method, but it didn't help
(Get-Date).AddDays(-$(Get-Date).Day)
Saturday, September 30, 2017 2:36:19 PM
((Get-Date).AddDays(-$(Get-Date).Day)).Day
30
$filename = "proemail vytvoreni_9.2017 2017-10-16"
# Take the date from the filename
$sub = $filename.Substring($filename.Length-10)
# make it into a date format
$filedate = Get-Date -Date $sub
# take that date 2017-10-16 and substracts its own days so in this case substract 16 days, then show me the date
(($filedate).AddDays(-$filedate.Day)).Date
Output:
Saturday 30 september 2017 0:00:00
I've looked at this problem and different solution for this for awhile and here is my answer.
Create any date you want. Here is an easy way to get the month you want.
$date = [datetime]::Parse("11/2019")
Then simply add the next month and remove one day. p
$date.AddMonths(1).AddDays(-1)
Hope this helps somebody else.
In the CakePHP 3 Cookbook on Date/Time, you can compare time intervals with future/past days/weeks using IsWithinNext/WasWithinNext. You can also modify dates/times by doing a ->modify('extra time') - eg. if $date = 2016-01-01, $date->modify('+1 week') would mean $date = 2016-01-08.
These features require the use of Cake\i18n\Time. However, when I attempted to use these features, I received a Cake error:
Call to a member function isWithinNext() on string.
This is the code I used:
$date_start = \Cake\Database\Type::build('date')->marshal($data['session']['date_start'])->i18nFormat(); //before hand my dates were in the form of an array comprised of Year, Month and Day. This changes them into date format.
if($date_start->isWithinNext('1 week')){
$deposit_due = $booking->date_confirm;
$deposit_due->modify('+48 hours');
} elseif ($date_start->isWithinNext('2 weeks')){
$deposit_due = $booking->date_confirm;
$deposit_due->modify('+1 week');
} elseif ($date_start->isWithinNext('3 weeks')){
$deposit_due = $booking->date_confirm;
$deposit_due->modify('+1 week');
} else {
$deposit_due = $booking->date_confirm;
$deposit_due->modify('+2 weeks');
}
Calling i18nFormat() returns a formatted string as you can look up in the API: https://api.cakephp.org/3.4/class-Cake.I18n.DateFormatTrait.html#_i18nFormat
This, for example, should work:
$date_start = new \Cake\I18n\Time($data['session']['date_start']);
debug($date_start->isWithinNext('2 weeks'));
I'm trying to compare a file's modified date with a specific date.
What I have is this:
If FormatDateTime(objFile.DateLastModified,vbShortDate) = specificDate Then
'Do something
End if
I've tried using IsDate and a variable with a value of #11/9/2015# but always returns false. I can't figure out how to set the variable "specificDate" to 11/9/2015.
If you are comparing the date only (but not the time), then you have to cut off the fractional part of the last modified value, since integer part represents days, and fractional part - hours, minutes and seconds. After any changes convert the values back to Date type with CDate(), as well as date containing string before the comparison.
Sub Test()
dtSpecificDate = CDate("11/9/2015")
With CreateObject("Scripting.FileSystemObject").GetFile("C:\Test\tmp.txt")
dtLastModified = CDate(Int(.DateLastModified))
End With
If dtLastModified = dtSpecificDate Then
' Do something
End If
End Sub