How to get DateCompare() to behave in ColdFusion 10? - coldfusion

I'm using CF10 with latest update level on Windows in Pacific Standard Time. I need a datecompare() combination that returns 0 but I cannot get it to behave every since Adobe decided to change the behavior of DateConvert() and DateCompare()
<cfset filePath = getBaseTemplatePath()>
<cfset fileinfo = getFileInfo(filePath)>
<cfset lastModified = fileinfo.lastModified>
<cfset lastModifiedUTC = dateConvert("local2utc", lastModified)>
<cfset lastModifiedUTC2 = dateAdd("s", getTimezoneInfo().UtcTotalOffset, lastModified)>
<cfset lastModifiedHttpTime = getHttpTimeString(lastModified)>
<cfset parseLastModifiedHttpTimeSTD = parseDateTime(lastModifiedHttpTime)>
<cfset parseLastModifiedHttpTimePOP = parseDateTime(lastModifiedHttpTime, "pop")>
<cfoutput>
<pre>
lastModified (local) : #datetimeformat(lastModified, 'long')#
lastModifiedUTC : #datetimeformat(lastModifiedUTC, 'long')#
lastModifiedUTC2 : #datetimeformat(lastModifiedUTC2, 'long')#
datecompareLmUTC : #dateCompare(lastModifiedUTC, lastModifiedUTC2)# //wtf
lastModifiedHttpTime : #lastModifiedHttpTime#
parseLastModifiedHttpTimeSTD : #datetimeformat(parseLastModifiedHttpTimeSTD, 'long')#
parseLastModifiedHttpTimePOP : #datetimeformat(parseLastModifiedHttpTimePOP, 'long')#
I need a datecompare() combination that returns 0
------------------------------------------------
DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP) : #DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP)#
DateCompare(lastModifiedUTC2, parseLastModifiedHttpTimePOP) : #DateCompare(lastModifiedUTC2, parseLastModifiedHttpTimePOP)#
CF Version : #server.coldfusion.productVersion#, update level: #server.coldfusion.updatelevel#
</pre>
</cfoutput>
OUTPUT:
lastModified (local) : September 11, 2015 7:10:23 PM PDT
lastModifiedUTC : September 12, 2015 2:10:23 AM UTC
lastModifiedUTC2 : September 15, 2015 4:58:22 PM PDT
datecompareLmUTC : -1 //wtf
lastModifiedHttpTime : Sat, 12 Sep 2015 02:10:23 GMT
parseLastModifiedHttpTimeSTD : September 12, 2015 2:10:23 AM PDT
parseLastModifiedHttpTimePOP : September 12, 2015 2:10:23 AM UTC
I need a datecompare() combination that returns 0
------------------------------------------------
DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP) : 1
DateCompare(lastModifiedUTC2, parseLastModifiedHttpTimePOP) : 1
CF Version : 10,0,17,295085, update level: 17
I'm pulling my hair out.

(Too long for comments)
I did some digging with CF11, based on the blog comments. From what I could tell, the reason the initial comparison fails is that although the first two dates look similar:
// code
lastModifiedUTC : #DateTimeFormat(lastModifiedUTC, "yyyy-mm-dd HH:nn:ss.L zzz")#
lastModifiedUTC2 : #DateTimeFormat(lastModifiedUTC2, "yyyy-mm-dd HH:nn:ss.L zzz")#
// output
lastModifiedUTC : 2015-09-13 19:51:46.219 UTC
lastModifiedUTC2 : 2015-09-13 19:51:46.219 PDT
... due to time zone differences, internally the objects represent a different point in time. That is why dateCompare() fails to return 0. (The third comparison fails for the same reason.)
// code
lastModifiedUTC : #lastModifiedUTC.getTime()#
lastModifiedUTC2 : #lastModifiedUTC2.getTime()#
// output
lastModifiedUTC : 1442173906219
lastModifiedUTC2 : 1442199106219
Notice if you compare lastModifiedUTC to the original (local) date, it works as expected? Despite the different time zones, both objects still represent the same point in time internally:
// code
dateCompare : #dateCompare(lastModifiedUTC, lastModified)#
lastModifiedUTC : #lastModifiedUTC.getTime()#
lastModified : #lastModified.getTime()#
lastModifiedUTC : #DateTimeFormat(lastModifiedUTC, "yyyy-mm-dd HH:nn:ss.L zzz")#
lastModified : #DateTimeFormat(lastModified, "yyyy-mm-dd HH:nn:ss.L zzz")#
// output
dateCompare : 0
lastModifiedUTC : 1442173906219
lastModified : 1442173906219
lastModifiedUTC : 2015-09-13 19:51:46.219 UTC
lastModified : 2015-09-13 12:51:46.219 PDT
Curiously, the second comparison also fails to return 0, despite the fact that both objects seem to have the same time and time zone. However, if you look at the internal time values the milliseconds differ. The milliseconds of the POP value are always zero. DatePart reports the same result. That sort of makes sense, since the POP date was created by parsing a string which does not contain milliseconds. Yet that does not explain why DateTimeFormat shows the milliseconds as non-zero.
The second comparison fails to return 0 because the two dates have different millisecond values. Unlike the file date, the POP date was created by parsing a string that does not contain milliseconds, so that date part is always zero. Since dateCompare() performs a full comparison (including milliseconds) the two dates are not equal.
// code
lastModifiedUTC : #DateTimeFormat(lastModifiedUTC, "yyyy-mm-dd HH:nn:ss.L zzz")#
parseLastModifiedHttpTimePOP : #DateTimeFormat(parseLastModifiedHttpTimePOP, "yyyy-mm-dd HH:nn:ss.L zzz")#
lastModifiedUTC : #lastModifiedUTC.getTime()#
parseLastModifiedHttpTimePOP : #parseLastModifiedHttpTimePOP.getTime()#
datePart(lastModifiedUTC) : #datePart("l", lastModifiedUTC)#
datePart(parseLastModifiedHttpTimePOP) : #datePart("l", parseLastModifiedHttpTimePOP)#
// output
lastModifiedUTC : 2015-09-13 19:51:46.219 UTC
parseLastModifiedHttpTimePOP : 2015-09-13 19:51:46.0 UTC
lastModifiedUTC : 1442173906219
parseLastModifiedHttpTimePOP : 1442173906000
datePart(lastModifiedUTC) : 219
datePart(parseLastModifiedHttpTimePOP) : 0
However, on a good note, that means the comparison works if you skip the milliseconds and only compare down to the "second" ie dateCompare(date1, date2, "s"):
// code
DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP, "s") : #DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP, "s")#
// output
DateCompare(lastModifiedUTC, parseLastModifiedHttpTimePOP, "s") : 0
As an aside, I am not sure why Adobe chose to change the behavior of something as critical as UTC dates .. Unfortunately, I do not know that there is much you can do about it other than the options mentioned in the blog comments a) Change the jvm time zone or b) create your own version of dateConvert and use that instead.
Boy what a mess....

Related

Trouble using utcoffset with Chart.js

I'm trying to use Chart.js with a datetime x axis, and I need to adjust all my values by subtracting 5 hours. Here's some of my code:
var timeFormat = 'MM/DD HH:mm';
time: {
format: timeFormat,
tooltipFormat: 'll',
parser: function(utcMoment) {
return moment(utcMoment).utcOffset(5, true);
}
},
Without the parser function, my values are normal (10:00, January 10, 2021), but with the parser function, for some reason my values are set back all the way to 2001. Yes two-thousand-and-one.(10:00, January 10, 2001) Note that the time is not actually changed (So two errors: 1.time not adjusted when it should be. 2:years adjusted when it shouldn't be). Why could this be?
I will assume that the reason you want to roll it back by 5 hours is because of a timezone difference. If that's the case, you should use moment-timezone instead of moment.
With that said, subtracting 5 hours from the current date is actually simpler than what you're doing.
Before feeding a date into moment, you need to convert it to the js Date object like so: new Date('2021-01-10 00:00:00'). Since your parser function accepts the date in m/d H:M format, you would need to append the year to it first.
So here is how your code should look:
parser: function(utcMoment) {
const new_date = utcMoment.split(' ')[0] + '/' + (new Date().getFullYear()) + ' ' + utcMoment.split(' ')[1];
return moment(new Date(new_date)).subtract({hours: 5})
}

Quantlib; how to use PiecewiseFlatForward

I am using the function PiecewiseFlatForward to build a curve and I am using it by setting settlementDate as an "absolute" starting date, like PiecewiseFlatForward(settlementDate, swapHelpers, Actual360()), so that the curve should be unaffected by a resetting of the Settings.instance().evaluationDate. If I change Settings.instance().evaluationDate to a date in the past with respect to the original settlementDate, the reference date of the curve stays the same, but I get an error when trying to retrieve the forward rates (unlike when using the ForwardCurve function). Here the code, where after resetting the evaluation to a past date the same forwardRate function does not work (RuntimeError: 1st iteration: failed at 1st alive instrument, pillar March 16th, 2020, maturity March 16th, 2020, reference date November 27th, 2019: 2nd leg: negative time (-0.2) given):
calendar = TARGET()
todaysDate = Date(25, November, 2019)
Settings.instance().evaluationDate = todaysDate
settlementDate = Date(27, November, 2019)
calendar = TARGET()
swaps = {
(6,Months): 0.028067,
(12,Months): 0.030768,
(18,Months): 0.029352,
(24,Months): 0.028648,
(30,Months): 0.028532,
(36,Months): 0.028480,
(42,Months): 0.028420,
(48,Months): 0.028394,
(54,Months): 0.028382,
(60,Months): 0.028387}
fixedLegFrequency = Semiannual
fixedLegDayCounter = Thirty360()
fixedLegAdjustment = Unadjusted
swapHelpers = [ SwapRateHelper(QuoteHandle(SimpleQuote(swaps[(n,unit)])),
Period(n,unit), calendar,
fixedLegFrequency, fixedLegAdjustment,
fixedLegDayCounter, USDLibor(Period(6,Months)))
for n, unit in swaps.keys() ]
depoSwapCurve = PiecewiseFlatForward(settlementDate, swapHelpers, Actual360())
dates = [ spot+Period(i*6,Months) for i in range(0, len(swaps)) ]
rates = [ depoSwapCurve.forwardRate(d, USDLibor(Period(6,Months)).maturityDate(d), Actual360(), Simple).rate()
for d in dates ]
print rates
Settings.instance().evaluationDate = Date(12, September, 2019)
rates2 = [ depoSwapCurve.forwardRate(d, USDLibor(Period(6,Months)).maturityDate(d), Actual360(), Simple).rate()
for d in dates ]
print rates2
Is there a way to use the .forwardRate function without having to care about the evaluation date when the function used to build the curve is the PiecewiseFlatForward?
Are you sure there's nothing missing in the code you posted?
After correcting for the error NameError: name 'spot' is not defined (because spot is not defined in your code) with:
spot = calendar.advance(todaysDate, 2, Days)
it seems to work fine...

gqlQuerry comparing datetime objects

Here is the relevant code copied from my application on GAE.
today = datetime.datetime.strptime(date_variable, "%d/%m/%Y")
yesterday = ref_today - datetime.timedelta(days=1)
tomorrow = ref_today + datetime.timedelta(days=1)
logging.info('%s : %s : %s', yesterday, today, tomorrow)
#2016-02-19 00:00:00 : 2016-02-20 00:00:00 : 2016-02-21 00:00:00
records = db.GqlQuery("SELECT * FROM ProgrammeQueue"
" WHERE scheduledFrom < :1 AND scheduledFrom > :2 "
" ORDER BY scheduledFrom DESC",
tomorrow, yesterday)
Problem Statement :
Output: all records of 19/02/2016 and 20/02/2016
Expected: records = all records of 20/02/2016
What am I doing wrong ?
You query states:
WHERE scheduledFrom < :tomorrow AND scheduledFrom > :yesterday
where tomorrow and yesterday are datetimes. the time is set to 00:00:00, so the results will include dates of 19/02/2016 where the time is greater than
00:00:00.
maybe your query should be rewritten to use date objects not datetime objects (depending on your model definition). or maybe you need to rewrite it to something like this:
records = db.GqlQuery("SELECT * FROM ProgrammeQueue"
" WHERE scheduledFrom < :1 AND scheduledFrom >= :2 "
" ORDER BY scheduledFrom DESC",
tomorrow, today)

Basic pyephem code

I have a very simple code to get the longitude of the sun but when I compare the output to Astrolog and Astrodienst its incorrect, there is a 13 minute difference. I have not added Observer as I think default is midnight GMT (which is what I want). What am I doing wrong?
import ephem
start = ephem.date('2015/01/01')
end = ephem.date('2015/12/31')
f2 = open("Sun", 'w')
while start <= end:
sun = ephem.Sun(start)
ecl = ephem.Ecliptic(sun)
f2.write(str(ephem.date(start))+' '+ str(ecl.lon) +'\n')
start+=1
f2.close()
Example of results for 2015/12/30:
code - 2015/12/30 00:00:00 277:43:36.6
Astrodienst - 7°56'39 Cap
Thanks
The reason why the 13 minute difference is because of the epoch setting, when I added
sun = ephem.Sun(start, epoch = start)
the results were the same as swiss ephemeris.

Regex to find specific pattern in R

I have a dataset like below:
dput(d1)
structure(list(FNUM = structure(1L, .Label = "20140824-0227", class = "factor"),
DESCRIPTION = "From : J LTo : feedback#lsd.goe.sfcc : Bcc : Sent On : Mon Apr 13 08:59:18 S 2015Subject : RE:Re: Suspect illegally modified vehiclesBody : Our Ref: BS-CT-1408-0665Date : 2-Apr-2015Our Ref: 2015/Jan/3224Date : 2-Apr-2015Thank you very much! Please conduct a thorough check on the vehicle other than the exhaust system. Warm regards,J L--------------------------------------------On Mon, 4/13/15, feedback#lsd.goe.sf <feedback#lsd.goe.sf> wrote: Subject: RE:Re: Suspect illegally modified vehicles To: jl1229#yahoo.ca Received: Monday, April 13, 2015, 8:56 AM Our Ref: GCE/VS/VS/VE/F20.000.000/38104 Date : 8-Apr-2015 Tel : 1800 2255 582 Fax : 6553 5329 -------------------------------------------- On Mon, 4/6/15, feedback#lsd.goe.sf <feedback#lsd.goe.sf> wrote: Subject: Suspect illegally modified vehicles To: joa#dccs.ca Received: Monday, April 6, 2015, 11:06 AM Our Ref: GCE/VS/VS/VE/F20.000.000/37661 Date : 2-Apr-2015 Tel : 1812 2235 582 Fax : 6553 5329 Dear Ms L Our records show that the vehicle bearing registration"), .Names = c("FNUM",
"DESCRIPTION"), row.names = "1", class = "data.frame")
I use the below regex to identfiy values Our Ref:
> gsub(" *(Our Ref|Date) *:? *","",regmatches(d1[1,2],gregexpr("Our Ref *:[^:]+",d1[1,2]))[[1]])
[1] "BS-CT-1408-0665" "2015/Jan/3224"
[3] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/VS/VS/VE/F20.000.000/37661"
But i only wanted values of Our Ref: which starts with GCE , how do i limit my output to those values which begins with GCE.
Desired Result:
[1] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/VS/VS/VE/F20.000.000/37661"
Updated For Second part of the problem:
dput(d1)
structure(list(FNUM = structure(1L, .Label = "20140824-0227", class = "factor"),
DESCRIPTION = "From : J LTo : feedback#lsd.goe.sfcc : Bcc : Sent On : Mon Apr 13 08:59:18 S 2015Subject : RE:Re: Suspect illegally modified vehiclesBody : Our Ref: BS-CT-1408-0665Date : 2-Apr-2015Our Ref: 2015/Jan/3224Date : 2-Apr-2015Thank you very much! Please conduct a thorough check on the vehicle other than the exhaust system. Warm regards,J L--------------------------------------------On Mon, 4/13/15, feedback#lsd.goe.sf <feedback#lsd.goe.sf> wrote: Subject: RE:Re: Suspect illegally modified vehicles To: jl1229#yahoo.ca Received: Monday, April 13, 2015, 8:56 AM Our Ref: GCE/VS/VS/VE/F20.000.000/38104 Date : 8-Apr-2015 Tel : 1800 2255 582 Fax : 6553 5329 -------------------------------------------- On Mon, 4/6/15, feedback#lsd.goe.sf <feedback#lsd.goe.sf> wrote: Subject: Suspect illegally modified vehicles To: joa#dccs.ca Received: Monday, April 6, 2015, 11:06 AM Our Ref: GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc Date : 2-Apr-2015 Tel : 1812 2235 582 Fax : 6553 5329 Our Ref: GCE/CC/PCF/FB/F20.000.000/233546/SK/PW Date : 2-Apr-2015 Dear Ms L Our records show that the vehicle bearing registration "), .Names = c("FNUM",
"DESCRIPTION"), row.names = "1", class = "data.frame")
> gsub(" *(Our Ref|Date) *:? *","",regmatches(d1[1,2],gregexpr("Our Ref *:\\s+GCE[^:]+",d1[1,2]))[[1]])
[1] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc"
[3] "GCE/CC/PCF/FB/F20.000.000/233546/SK/PW"
However i want to limit my result to
[1] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/QSMO/SQSS/SQ/F20.000.000/503533"
[3] "GCE/CC/PCF/FB/F20.000.000/233546"
which is i wanted only v1/v2/v3/v4/v5/v6 anything after 6 values should be removed or ends with number after 5 /(slashes).GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc should change to GCE/QSMO/SQSS/SQ/F20.000.000/503533 and GCE/CC/PCF/FB/F20.000.000/233546/SK/PW should change to GCE/CC/PCF/FB/F20.000.000/233546
You can add in a requirement that "GCE" (with space before it) occurs before your [^:]
regmatches(d1[1,2],gregexpr("Our Ref *:\\s+GCE[^:]+",d1[1,2]))
EDIT: try this, you can match groups n numbers of times with {n},
gsub(" *(Our Ref|Date) *:? *", "",
regmatches(d1[1,2],
gregexpr("Our Ref *:\\s+GCE(/[^/-]+){5}",
d1[1,2], perl=T))[[1]])
Here is a different approach using strpslit to split on any non-digit character one or more times: \\D+ followed by a space:
splts <- strsplit(d1$DESCRIPTION, "\\D+ ")[[1]]
splts[grep("GCE", splts)]
# [1] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/QSMO/SQSS/SQ/F20.000.000/503533"
# [3] "GCE/CC/PCF/FB/F20.000.000/233546"