convert Oracle DATE to linux time - linux

I'm having trouble converting an Oracle DATE field to a linux time format in perl.
I'm pulling the date field like this:
my $query = "SELECT RESPONSE_DATE FROM TABLENAME";
my $sth = $dbh->prepare($query);
$sth->execute() or die "Couldn't execute statement: " . $sth->errstr;
my #results = $sth->fetchrow_array();
printf "response_date=%s",$results[0];
printf "localtime(time)=%s",localtime(time);
Output:
response_date=14-OCT-14 08.35.00.000000 PM
localtime(time)=Tue Aug 18 23:35:13 2015
I can get all the pieces of the local time like this:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
But now I need to compare those pieces with similar pieces of response_date. Any ideas?

I presume you need all the localtime fields for some sort of date calculation?
The Time::Piece module is the usual solution to this. It allows you to parse your database time and also overloads localtime to that it returns a Time::Piece object in scalar context
Unfortunately Time::Piece won't parse the fractional seconds field, so if it's not always zero then you will need to remove it from the date-time string
Clearly other calculations are possible once you have both the database value and the current date as Time::Piece objects
use strict;
use warnings;
use Time::Piece;
use Time::Seconds 'ONE_DAY';
my $s = '14-OCT-14 08.35.00.000000 PM';
my $dt = Time::Piece->strptime($s, '%d-%b-%y %H.%M.%S.000000 %p');
my $now = localtime;
printf "%s was %.3f days ago\n",
$dt->strftime('%a %d-%b-%Y at %H:%M:%S'),
($now - $dt)->days;
output
Tue 14-Oct-2014 at 20:35:00 was 308.190 days ago

Related

How to convert "PT5H" to ticks using powershell?

I'm trying to convert "PT5H" to ticks format using powershell. But facing below error:
System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'SuppressionDuration'. Cannot convert value "PT5H" to type "System.TimeSpan". Error: "String was not recognized as a valid TimeSpan."
Can anyone help me out in converting "PT5H"(String) to ticks ?
You can use the XmlConvert.ToTimeSpan() method to parse ISO8601 duration strings:
$duration = [System.Xml.XmlConvert]::ToTimeSpan('PT5H')
Command-Name -SuppressionDuration $duration

Time data with gmt does not match format input for pandas conversion

How to convert date time string with GMT into a pandas date time format ?
Here is an example :
#date_time is like 12/Dec/2015:18:25:11 +0100
df['date_time'] = pd.to_datetime(df['date_time'], format="%d/%b/%Y:%I:%M:%S %Z")
Here is the error :
ValueError: time data '12/Dec/2015:18:25:11 +0100' does not match
format '%d/%b/%Y:%I:%M:%S %Z' (match)
You'd better check the formatted string:
https://docs.python.org/3/library/datetime.html
Use '%d/%b/%Y:%H:%M:%S %z' instead of '%d/%b/%Y:%I:%M:%S %Z'.

How to change a given date in "yyyy-MM-dd HH:mm:ss.SSS" format to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" format in groovy

How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Date of 7 days before today

I give current day in a string in MATLAB. For example if today is '20180703', I need 7 strings containing:
'20180702'
'20180701'
'20180630'
'20180629'
'20180628'
'20180627'
'20180626'
Simple:
t = datetime('20180703', 'InputFormat', 'yyyyMMdd')
t = t - days(1:7)
datestr(t, 'yyyymmdd')
Edit.
As excaza pointed out, datetime and datestr use different input format. Hence, 'MM' in the first function, and 'mm' in the second one.
I would go with something like:
lastSevenDays = arrayfun(#(offset) datestr(now-offset, 'yyyymmdd'), 1:7, 'UniformOutput', false)
or more matlaby:
datestr(now - days(1:7), 'yyyymmdd')

vim search for epoch time strings, pipe to date -d and return the date into the file

I have a file with a lot of data in it, one being a last-modified="1231231231"
where 1231231231 is epoch time in milliseconds
<Translation
author_id="25"
id="02f18edd-ef7a-48e2-b614-b5888936017e"
language="de_DE"
last_modified="1325669156960"
phase="1"
target="[ phase="1" language="de_DE" ]"
translation_text="Funktionen"/>
Note the: last_modified="1325669156960"
I can run this:
:%s/\([0-9]\{10\}\)\([0-9]\{3\}\)/\1/g
to find all these occurrences and replace them with a "seconds" string:
last_modified="1325669156"
I can then pattern match on those 10 digits, and what I'd like to do is pipe them to the unix data -d command to return a formatted data stamp:
:%s/[0-9]\{10\}/&/g
In this example, instead of replacing with the same value as I found (I.e, the &),
I'd like to somehow pipe that value to what would be essentially:
date -d &
and return that as a formatted time stamp in the
last_modified="Wed Jan 4 07:13:32 MST 2012"
Any ideas on how to do this? I have to do this about every other week on various files.
You can use strftime() in vim. Find one proper format string to meet your needs.
I'm using %c here:
:%s/last_modified="\zs\(\d\{10}\)\d\{3}/\=strftime('%c', str2nr(submatch(1)))/g
result:
<Translation
author_id="25"
id="02f18edd-ef7a-48e2-b614-b5888936017e"
language="de_DE"
last_modified="2012-1-4 17:25:56"
phase="1"
target="[ phase="1" language="de_DE" ]"
translation_text="Funktionen"/>

Resources