Subtracting a Minute in Perl - perl

I have a variable in Perl that I initialized as $invoice_date = '1/6/14' (June 1st 2014). How can I determine the datatype that Perl considers this variable to be?
I'd like to subtract a minute from the invoice date to get May 31 2014 11:59PM. How can I do this with or without declaring $invoice_date to be a certain datatype?
Update: Thanks for the comments and answers. Since it is a string, I am going to try to concatenate the time portion. I have a another variable $period_end_date which is set to May 31, 2014. I'm going to try to concatenate the 11:59PM to it.
The string is subsequently sent in a SQL statement. If I can figure out what SQL expects for the string, it should be possible to insert the time portion.

You need some date manipulation module as '1/6/14' is plain string, and two digit years were abandoned prior to Y2K event.
use Time::Piece;
use Time::Seconds;
my $t = Time::Piece->strptime("1/6/2014", "%d/%m/%Y");
$t -= ONE_MINUTE;
print $t;
output
Sat May 31 23:59:00 2014

Related

Date Increment Using Autohotkey

I'm looking for a way to set an arbitrary date, and every time I press a key it will print the day after it (tomorrow).
global jDate = "June 1, 1986"
^+z::
;Output our date in LongDate format
FormatTime, TimeString, %jDate%, LongDate
SendInput, %TimeString%
;Increment the date by a single day
jDate += 1, Days
Return
Unfortunately, it the code keeps starting jDate as today's current date/time rather than the past date I specify in the initial variable assignment. Not sure why. The incrementing works fine, it just increments starting from todays date rather that the 1986 date.
FormatTime is expecting any date/time input to be in the "YYYYMMDD..." format. Since what you've assigned to jDate doesn't fit that criterion, it assumes it's invalid and uses today's date. To make it work how you expect, just modify your jDate input.
jDate := "19860601" ; 1986 -> YYYY, 06 -> MM, 01 ->DD
A couple of things to note: (1) global is not needed in this context; (2) I would recommend getting out of the habit of assigning variables using the = comparator (use := assignment operator instead). It only works for legacy reasons but generates more confusion than it's worth. In the context that you're using it, the quotes would need to be removed.

Referring to an exact date in calculation

I have a date variable (dd/mm/yyyy).
I need to create a similar variable that is equivalent to Dec. 31 2016 to use it in a calculation.
How would I do this?
You need to use the daily() function and then format the numeric variable accordingly:
clear
set obs 1
generate date = daily("31Dec2016", "DMY")
format %tdMonDDCCYY date
list
+-----------+
| date |
|-----------|
1. | Dec312016 |
+-----------+
Type help daily() and help format from Stata's command prompt for details.
I take it that you have a numeric daily date variable. Some people hold dates as strings, which isn't very useful in Stata, and there are other kinds of numeric date variable.
A date like 31 December 2016 is a constant which can be calculated as
. di mdy(12, 31, 2016)
20819
and for display could be
. di %td mdy(12, 31, 2016)
31dec2016
You can get the same result in other ways, such as
. di daily("31 Dec 2016", "DMY")
20819
Nothing stops you putting this constant in a variable, but that just copies the same value as many times as you have observations, and is for most purposes pointless. Either use it directly or make your code easier to understand by using some evocative macro or scalar name:
. local Dec_31_2016 = mdy(12, 31, 2016)
. local today = mdy(8, 7, 2018)
. di `today' - `Dec_31_2016'
584
I have guessed that the most likely use for a date constant is to calculate time elapsed since some benchmark date.

Perl workweek conversion is incorrect

I'm faced a weird problem.
I have date in form of Tue Feb 25 00:20:13 2014.
my task is to calculate the week number and the week day.
I tried the following
use Time::Piece;
my $date="Tue Feb 25 00:20:13 2014";
my $db_date=Time::Piece->strptime($date, "%a %b %d %H:%M:%S %Y");
my $ww=$db_date->strftime("%W.%w-%Y);
print $ww;
When I run the script I get the output as
08.2-2014
which is wrong, the expected output is
09.2-2014
I want to know where did i go wrong?
pls help...
You're using the "%W" strftime() conversion. Time::Piece doesn't specify the meaning of "%W", but the documentation for the equivalent C function says that "%W" starts counting with the first week that contains a Monday. It sounds like you want the ISO 8601 week number, which starts counting with the first week that contains at least four days, in which case the "%V" conversion should do what you want.

Time::Piece is returning a wrong value

I am trying to convert date from yymmdd to YYYY-MM-DD with Time::Piece module. With the input as Nov 31, 2000 (20001131), I am getting output as 2000-12-01. In reality, Nov 31 doesn't even exists.
use Time::Piece;
my $dt_str = Time::Piece->strptime('20001131', '%Y%m%d')->strftime('%Y-%m-%d');
print $dt_str;
Am I missing something here?
Internally, it does only rough validation and error reporting, and then performs the same transformations as POSIX::mktime does; any days beyond the end of a month will just cause it to advance the produced date into the next month. This does seem a little inconsistent; since it allows that for days, I'd also expect it to treat '20005931' as '2004-12-01', but instead it errors out.

What's the opposite of the localtime function in Perl?

In Perl, localtime takes a Unix timestamp and gives back year/month/day/hour/min/sec etc. I'm looking for the opposite of localtime: I have the parts, and I'd like to build a unix timestamp from them.
You can use the timelocal function in the Time::Local CPAN module.
NAME
Time::Local - efficiently compute time
from local and GMT time
SYNOPSIS
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
$time = timegm($sec,$min,$hour,$mday,$mon,$year);
DESCRIPTION
This module provides functions that
are the inverse of built-in perl
functions localtime() and gmtime().
They accept a date as a six-element
array, and return the corresponding
time(2) value in seconds since the
system epoch (Midnight, January 1,
1970 GMT on Unix, for example). This
value can be positive or negative,
though POSIX only requires support for
positive values, so dates before the
system's epoch may not work on all
operating systems.
It is worth drawing particular
attention to the expected ranges for
the values provided. The value for the
day of the month is the actual day (ie
1..31), while the month is the number of months since January (0..11). This
is consistent with the values returned
from localtime() and gmtime().
Note: POSIX::mktime is a just a wrapper around your C library's mktime() function. Time::Local is a pure-Perl implementation, and always returns results matching Perl's localtime. Also, Time::Local offers gmtime, while mktime only works in local time. (Well, you could try changing $ENV{TZ}, but that doesn't work on some systems.)
POSIX::mktime
DateTime on CPAN might of of some use. It also has a lot of time manipulation/translation methods.
Just create the DateTime using your parts and call $datetime->formatter("%s") ;