Greenwich Mean Time to Indian Standard Time - perl

How to convert given Greenwich Mean Time to Indian Standard Time in perl?
Thanks

It helps to know what format your desired input/output are, but:
use DateTime;
$datetime = DateTime->new( year => 2010, month => 12, day => 27, hour => 15, minute => 45, second => 15, time_zone => 'GMT' );
$datetime->set_time_zone( 'Asia/Kolkata' );
print $datetime->ymd, ' ', $datetime->hms;

Have you tried using the DateTime::TimeZone module? I believe it's got some pretty easy-to-use methods for time-manipulation.

Related

Parsing timestamp string to integer and subtract hires timestamp

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.

nvd3 (d3.js) date format returns incorrect month

My data looks like this:
[{ x="2013-06-01", y=3}, { x="2013-07-01", y=7 }, { x="2013-08-01", y=3 }]
Chart x-axis is formatted as so:
chart.xAxis
.axisLabel('Date')
.tickFormat(function(d) { return d3.time.format('%b %Y')(new Date(d)); })
;
%b returns May, Jun, July respectively for the dates 2013-06-01, 2013-07-01, 2013-08-01
Why is it returning the previous month, and how can I fix it?
EDIT: If the date is formatted as 2013-06-02, it will return the correct month... does someone know what is happening to cause this?
#Amelia is correct it's because of timezone difference and because Date defaults to 24:00:00 if you don't specify a time. So, in case of EDT, which is -4:00, you lose 4 hours which puts you in the previous day (May 31 2013 20:00:00) and because the days in your dates are 01, this puts you in the previous month.
To bypass this you could append a time to your date if that is allowable in your case.
chart.xAxis
.axisLabel('Date')
.tickFormat(function(d) {
d = d.split('-')
// Create new date by using new Date(year, month, day, hour, second, ms)
// Subtracting 1 is necessary since Javascript months are 0 - 11.
return d3.time.format('%b %Y')(new Date(d[0], +d[1] - 1, d[2], 12, 0, 0));
});
Here is a working Fiddle

Convert string date to Perl DateTime

I'm a newbie in Perl, so please be patient with me:
I am writing a log parser and have successfully parsed "Dec 1 17:45:36.185" into it's individual units (month, day, hour, minute, seconds, milliseconds). I want to convert this to Perl's DateTime object.
I'm having trouble with the milliseconds portion: .185.
I hope to use DateTime::Format::Strptime like such:
my $strp = DateTime::Format::Strptime(
pattern => "%b %d %H:%M:%S" # how do I add the milliseconds part?
)
If you want to display milliseconds, use this format %3N:
my $strp = DateTime::Format::Strptime(
pattern => "%b %d %H:%M:%S.%3N" # now we have the milliseconds part
)
The number jut before the N means the number of digits that will be displayed.
The number displayed is truncated, not rounded.
I might be missunderstanding you. But if you want to have an object of this: http://metacpan.org/pod/DateTime and know the individual numbers, why not use the constructor like so:
use DateTime;
$dt = DateTime->new(
year => 1964,
month => 10,
day => 16,
hour => 16,
minute => 12,
second => 47,
nanosecond => 500000000,
time_zone => 'Asia/Taipei',
);
Or do you wonder how to format that information into a string later? In that case, you could just use sprintf and DateTimes get methods to produce any format you want.
edit: I think i understood you now. DataTime does not have ms, only ns. When constructing, that is no problem, as you can just put nanosecond => ($ms*1000000) but i see how that can be a problem when using ::Strptime.
I cannot install DateTime here to test it, but the CPAN does say
%N
Nanoseconds. For other sub-second values use %[number]N.
So when you have a DateTime object with nanoseconds, you could play with that [number] value to see what it does and when you have found a way to tell it that you like ms, it should even work for parsing.

How do I use Date::Manip to create a date usable by Date::ICal?

I need to take the result of a Date::Manip ParseDateString() and use it as the bast for an Date::ICal::Event
Date::ICal::Event has the following date creating inputs:
use Date::ICal;
$ical = Date::ICal->new( ical => '19971024T120000' );
$ical = Date::ICal->new( epoch => time );
$ical = Date::ICal->new( year => 1964,
month => 10, day => 16, hour => 16,
min => 12, sec => 47 );
Date::Manip ParseDateString() returns a standard date value.
How should I use that date in the ICal date? Convert to epoch? Can it easily be converted to the ical format? I feel like this should be easier than it is.
Here's what I'd do:
sub date2ical
{
my $date = shift;
Date::ICal->new( ical => UnixDate($date, '%QT%H%M%S'), # %Q means %Y%m%d
offset => UnixDate($date, '%z'));
} # end date2ical
# Usage:
my $ical = date2ical(ParseDateString('today'));
This should properly handle timezones (provided Date::Manip gets the timezone right).
I did some further CPAN hunting and came up with the module DateTime::Format::DateManip
Using this I was able to convert it to a DateTime representation and then get the epoch from that using the epoch method available in DateTime:
my $cagedate = ParseDateString($cagewatch);
my $cagedatetime = DateTime::Format::DateManip->parse_datetime($cagedate);
$vevent->add_properties(
summary => $cagemovie,
description => $cagemovie,
dtstart => Date::ICal->new( epoch => $cagedatetime->epoch )->ical,
);
Just in case you are curious about the CAGE variables. I was parsing the list of movies for the Year of the Cage. All Nick Cage, all year. Oh yeah.
Your life will be so much easier if you dump Date::Manip and switch to DateTime for all your date and time processing. There's even a DateTime::Format::ICal to help with this specific task.
It seems that, if you've got a date into Date::Manip successfully, you can just use its printf directives to output it in any format you want.
It looks like %Y%m%dT%H%M%S is what you want for iCal.

date difference and match with value in javascript

Hi I have the Drop down with values as "One year ", "Two year",...etc.. Ok? also i have two ajax textbox with calender extender . I want to popup alert message if dropdown selected value is "One year" and duration between the both textbox value Means dates not matches. getting what i mean ? please help me.
How can i get this scenario in javascript ??
Algorithm :
1.Get the both date from the text box.
2. The find the epcoch time for each date. //
3. subtract the both dates.
4. subtract_result = 365*24*60*60 // Finding the 1 year timestamp values
5. So, it the difference exceed than above calculation , you could sure that the date is mis matching.
Javascript:
// This is for first date
first = new Date(2010, 03, 08, 15, 30, 10); // Get the first date epoch object
document.write((first.getTime())/1000); // get the actual epoch values
second = new Date(2012, 03, 08, 15, 30, 10); // Get the first date epoch object
document.write((second.getTime())/1000); // get the actual epoch values
diff= second - first ;
one_day_epoch = 24*60*60 ; // calculating one epoch
if ( diff/ one_day_epoch > 365 ) // check , is it exceei
{
alert( 'date is exceeding one year');
}