When converting strptime to as.POSIXct, the millisecond changes - strptime

I have a strptime objective as follows:
a<-strptime(time_second, "%Y-%m-%d %H:%M:%OS")
head(a)
[1] "2016-07-05 20:53:47.166 CDT" "2016-07-05 21:45:00.485 GMT"
[3] "2016-07-05 21:45:30.031 CDT" "2016-07-05 21:45:52.106 GMT"
[5] "2016-07-05 21:45:52.106 CDT" "2016-07-05 21:45:52.106 GMT"
When I convert it by using as.POSIT I got follows:
b<-as.POSIXct(a)
head(b)
[1] "2016-07-05 20:53:47.165 GMT" "2016-07-05 21:45:00.484 GMT"
[3] "2016-07-05 21:45:30.030 GMT" "2016-07-05 21:45:52.105 GMT"
[5] "2016-07-05 21:45:52.105 GMT" "2016-07-05 21:45:52.106 GMT"
You find some millisecond reduced by 1 ms, some does not change (e.g. the last one). Can anyone figure out what is the problem?
Thanks in advance.

Related

postgresql convert 'Thu Jul 02 13:36:17 UTC 2020' to timestamp

I have a string that I need to insert into a table as a timestamp.
'Thu Jul 02 13:36:17 UTC 2020'
Without UTC the following conversion works
SELECT to_timestamp('Thu Jul 02 13:36:17 2020', 'Dy Mon dd HH24:MI:SS yyyy');
How can I convert the timestamp with the UTC portion?
Any help would be greatly appreciated!
Database Type: PostgreSQL
Table Data Type: timestamp (this can change if needed)
If it's always going to be UTC, then you can use:
> select to_timestamp(
'Thu Jul 02 13:36:17 UTC 2020',
'Dy Mon DD HH24:MI:SS UTC YYYY'
);

Error while using time.Parse when timezone and offset are together

I have the following code :
package main
import (
"fmt"
"time"
"log"
)
func main() {
date, err := time.Parse("Monday, 2 January 2006 15:04:05 PM MST-07:00" ,"Thursday, 17 August 2020 13:20:00 PM GMT+08:00")
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(date)
}
And it fails with the following error :
2009/11/10 23:00:00 parsing time "Thursday, 17 August 2020 13:20:00 PM
GMT+08:00" as "Monday, 2 January 2006 15:04:05 PM MST-07:00": cannot
parse ":00" as "-07:00"
But it succeeds if I separate MST-07:00 with a space as : "MST -07:00" in both layout sample and actual string.
What am I doing wrong?
GMT times undergo special handling by time.Parse. The signed offset for GMT in the value must be in the range -23 through +23 excluding zero, and may not include a colon. The layout should just specify MST without an offset.
For example:
package main
import (
"fmt"
"log"
"time"
)
func main() {
for _, ts := range []string{
"Thursday, 17 August 2020 13:20:00 PM GMT",
"Thursday, 17 August 2020 13:20:00 PM GMT+2",
"Thursday, 17 August 2020 13:20:00 PM GMT-2",
} {
date, err := time.Parse("Monday, 2 January 2006 15:04:05 PM MST", ts)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(date)
}
}
yields the output:
crow#mac:tp$ ./example
2020-08-17 13:20:00 +0000 GMT
2020-08-17 15:20:00 +0200 GMT+2
2020-08-17 11:20:00 -0200 GMT-2
An issue was raised for this a while back, and the outcome was (with reference to an example time string containing GMT+10:00):
The special handling of GMT, which is needed for other things, makes
it very difficult to know whether the +10:00 should be considered part
of the time zone or left alone to match the layout.
and so the issue was closed without proposed changes.

Compare dates from given array and two date ranges swift

Hi I have a problem scenario with date comparison with given date range the scenario is as follows:
I have an array containing data:
var filter1Date = [String]()
filter1Date[Wed, 06 May 2015 03:44:19 GMT, Wed, 06 May 2015 03:36:27 GMT, Wed, 06 May 2015 02:56:51 GMT, Wed, 06 May 2015 01:54:25 GMT, Tue, 05 May 2015 19:17:18 GMT, Wed, 06 May 2015 02:57:59 GMT, Wed, 06 May 2015 02:07:38 GMT, Wed, 06 May 2015 01:53:14 GMT, Tue, 05 May 2015 14:30:10 GMT, Tue, 05 May 2015 14:04:34 GMT]
Now I have two dates which gives from and to two dates
example:
var fromDate = "dd-MM-yyyy"
var toDate = "dd-MM-yyyy"
now I want to compare array of filter1Date variable with range fromDate and toDate variable
from this I have to get data from filter1Date which ranges in between these dates can anybody help me in this?
To compare two NSDates you can use NSDate.compare() method. I wrote an extension for the NSDate to handle this:
extension NSDate
{
func isInRange(from: NSDate, to:NSDate) -> Bool
{
if(self.compare(from) == NSComparisonResult.OrderedDescending || self.compare(from) == NSComparisonResult.OrderedSame)
{
if(self.compare(to) == NSComparisonResult.OrderedAscending || self.compare(to) == NSComparisonResult.OrderedSame)
{
// date is in range
return true
}
}
// date is not in range
return false
}
}
You can then use it like this:
var dates:[NSDate] = ... // your dates array
var startDate:NSDate // your start range date
var endDate:NSDate // your end range date
for date in dates
{
if(date.isInRange(startDate, to: endDate))
{
//date is in range, do something
}
else
{
//date is not in range
}
}

Go language time.Parse() for timestamps with no timezone

In Go I'm trying to use the time.Parse() function from the time package to convert a string timestamp into a Time object. I know Go has an uncommon way of representing the time format your timestamps are in by providing it with an example of how their reference time (Mon Jan 2 15:04:05 -0700 MST 2006) would be displayed in your format. I'm still having issues with errors however. Here is an example of one of my timestamps:
Tue Nov 27 09:09:29 UTC 2012
Here is what the call I'm making looks like:
t, err := time.Parse("Mon Jan 02 22:04:05 UTC 2006", "Tue Nov 27 09:09:29 UTC 2012")
So basically what I've done here is try and match the formatting for day name/month name/day number, the hour/minute/second format, the string literal "UTC" and the year format. Note that I've increased the hours field of the Go reference format by 7 (from 15 to 22) to account for the fact that their timestamp is in a negative 7 timezone and all my timestamps are in a UTC timezone.
The error I get is:
parsing time "Tue Nov 27 09:09:29 UTC 2012" as "Mon Jan 02 22:04:05 UTC 2006": cannot parse ":09:29 UTC 2012" as "2"
What am I doing wrong here? Am I misinterpreting how to use time.Parse() or is my use case not supported for some reason?
Your format string should be:
Mon Jan 02 15:04:05 MST 2006
playground
That is, use MST for the timezone and 15 for the hour, as documented in your linked Parse function.
In this case, you can use time.UnixDate:
package main
import (
"fmt"
"time"
)
func main() {
t, e := time.Parse(time.UnixDate, "Tue Nov 27 09:09:29 UTC 2012")
if e != nil {
panic(e)
}
fmt.Println(t)
}
https://golang.org/pkg/time#UnixDate

From mongoshell how to insert utc datetime and epoch values as utc datetimes into mongodb

Trying to do a proof of concept of loading sample tick data into mongo db and the tick archive data is generally in utc and epoch and need to load it as is and output as utc only not local datetimes.
How to insert utc datetime and epoch in mongodb without being cast into utc again and retrieve the
same as utc only.
db.equity_ticks.insert(
{
"SNAPSHOT_DTTM" : Date("2013-07-09 19:00:00.000000"),
"CLOSE_DATE" : Date(15895) ,
"PREV_DAY_CLOSE_DATE" : Date(15895)
}
instead it inserted the local CST and output as CST too !
db.equity_ticks.find().pretty()
{
"_id" : ObjectId("52040942e5171b792b258ae8"),
"SNAPSHOT_DTTM" : "Thu Aug 08 2013 16:10:26 GMT-0500 (CDT)",
"CLOSE_DATE" : "Thu Aug 08 2013 16:10:26 GMT-0500 (CDT)",
"PREV_DAY_CLOSE_DATE" : "Thu Aug 08 2013 16:10:26 GMT-0500 (CDT)"
}
any help is appreciated.