Sort string + date by most current date in Perl - perl

I have an array:
my #array = ( "\"Passing\" on Wed 12 Jan 2015 09:19:14 AM PST",
"\"Passing\" on Wed 12 Jan 2015 09:19:25 AM PST",
"\"Test Activation\" on Tues 14 Jan 2015 12:05:14 PM PST",
"\"Run Phase\" on Tues 14 Jan 2015 12:06:14 PM PST",
"\"Test Activation\" on Tues 13 Jan 2015 11:43:12 PM PST")
I want to remove the duplicate string line BUT keep the one that is most recent. So I want it to look like:
my #array = ("\"Passing\" on Wed 12 Jan 2015 09:19:25 AM PST",
"\"Test Activation\" on Tues 14 Jan 2015 12:05:14 PM PST",
"\"Run Phase\" on Tues 14 Jan 2015 12:06:14 PM PST")
I can't think of an easy way to do this... I was thinking about using some regex to compare the strings ( /\".*\"/ ) and have it remove duplicates it finds, but I'm not sure how to deal with the date/time.
Any suggestions are most welcome!

There's several options to parse and compare the dates. Simplest is to use the built in Time::Piece. Use strptime for parsing and compare with $time->epoch.
Unfortunately for you, abbreviated time zone names are ambiguous. PST can mean US Pacific Standard Time or Philippine Standard Time. This may cause strptime's %Z format to choke, YMMV. From my strptime man page...
The %Z format specifier only accepts time zone abbreviations of the local time zone, or the value "GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations. One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.
You may need to pre-process the date formats and convert them to time zone offsets. You can use Time::Zone for this and its distinctly North American slant.
use Time::Zone;
use Time::Piece;
my $offset = sprintf "%+d", (tz_offset("PST") / 60 / 60);
my $time = Time::Piece->strptime(
"Wed 12 Jan 2015 09:19:14 AM $offset",
"%a %d %b %Y %I:%M:%S %p %z"
);
print $time->datetime, "\n";
print $time->epoch, "\n";
But try %Z first and see if it works.
Extracting the dates is also left as an exercise.

Related

What date format is this : Mon Oct 18 10:26:10 EDT 2021"

What date format is the below and how to get this converted to check with current date in Powershell
Mon Oct 18 10:26:10 EDT 2021
EDT means Eastern Daylight Time (North America), which is 4 hours behind from the UTC universal time.
Using that, you could parse it to your local time by first adding the 4 hours offset to get the date in UTC, and finally using method .ToLocaltime() on it.
[datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4).ToLocaltime()
On my (Dutch) machine it doesn't seem to matter, but if you want to make absolutely sure the parsed-out date is regarded as being UTC (instead of its .Kind being set to 'Unspecified', use this:
[datetime]::SpecifyKind([datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4), 'UTC').ToLocaltime()

date gets increased by a day

I have a date in my mongodb database in following format
{
"_id" : ObjectId("5b8cd5e5cf36cb517c91910e"),
"date" : ISODate("2018-09-04T23:58:00.000Z")
}
When I show it by using moment on front end it gets increased by one day so 2018-09-04 becomes 2018-09-05
On front end I do this
moment(date).format('dddd MMM DD, YYYY')
I want same date extracted from the database that is 2018-09-04
Any help is appreciated.
Edit ->
When I console it on back end I see this
date: 2018-09-04T23:58:00.000Z
And on front end
date: "Thu Sep 06 2018 05:28:00 GMT+0530 (India Standard Time)"
Mongodb stores dates in ISO formats, it depends on where (in what timezones) your server and client are located. You can use the below.
The format for date you are getting is UTC format, when it will get converted to IST, it would show the difference. It means the following :-
If I get 2019-09-26T18:30:00.000Z as date in UTC. It is equivalent to Fri Sep 27 2019 00:00:00 GMT+0530 (India Standard Time). Please look at the difference 5hrs 30 mins. Incrementing/decrementing any thing on this and showing in IST would be wrong again as per the timezone is concerned.
If you do
new Date(date).toUTCString()
It will show you "Thu, 26 Sep 2019 18:30:00 GMT" In GMT which is exact.
It seems like a timezone issue.
If I run
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format('dddd MMM DD, YYYY'));
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format('dddd MMM DD, YYYY'));
I get
2018-09-04T23:58:00Z
2018-09-05T05:28:00+05:30
Tuesday Sep 04, 2018
Wednesday Sep 05, 2018
Which is off by one day

Date format toUTCString in Lua

I have a question regarding date formatting in Lua (Luajit). I need to get UTC string, for example, like I would do it in JavaScript:
var date = new Date()
console.log(date.toUTCString()) // "Fri, 06 Dec 2013 14:05:28 GMT"
Unfortunately in Lua I cannot find possibility to format date this way:
print(os.date()) -- Fri Dec 6 16:06:43 2013
From the Lua manual:
If format starts with '!', then the date is formatted in Coordinated Universal Time. [...]
If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ANSI C function strftime.
Based on this and a little documentation referencing, it's quite simple to construct a format string that resembles JavaScript's toUTCString format.
> =os.date('!%a, %d %b %Y %H:%M:%S GMT')
Fri, 06 Dec 2013 14:27:34 GMT

PHP4: Adjust date() and strtotime() for different timezones

I'm working on a podcast (on-the-road events) that contains future dates and different time zones.
<pubDate>Wed, 14 Nov 2012 19:00:00 EST</pubDate>
<pubDate>Wed, 25 Jul 2012 19:00:00 CDT</pubDate>
<pubDate>Thu, 29 Mar 2012 19:00:00 MDT</pubDate>
When I parse it with PHP4 (note PHP4), using date() and strtotime(), it adjusts the information and outputs it in the current timezone of the server (including daylight saving time changes).
Code...
date("g:ia T", strtotime($item->pubDate));
Example...
<pubDate>Thu, 29 Mar 2012 19:00:00 MDT</pubDate> outputs 8:00pm CDT
The expected output is 7:00pm MDT
Again, I'm using PHP4, so I can't do DateTime()-type stuff.
Basically, I just want it to just return the characters, instead of interpreting the characters. Maybe I shouldn't use date() or strtotime() at all, but I'm not sure of another way to turn Wed into Wednesday, 19:00:00 into 7:00pm, etc.

Convert seconds to date from Jan 01 1901 in unix/linux

im trying to convert a time stamp in seconds from Jan 01 1901 to the current date.
for example,
time stamp 3465468225 translate to a date in 2010. does anyone know of a way to do this in unix/linux? thanks.
In R, it is as simple as this:
> as.POSIXct(3465468225, origin="1901-01-01")
[1] "2010-10-25 15:03:45 CDT"
>
This uses appropriate wrappers around C-level calls gmtime() / localtime() plus time formatting via strftime().
On GNU and POSIX systems you can obtain the date string using seconds since Epoch (1970-01-01 00:00:00 UTC) as:
$ date --date=#1289495920
Thu Nov 11 12:18:40 EST 2010
You should handle the offset since Jan 01 1901 yourself.