Getting wrong conversion when converting timestamp to utc. Scala Spark - scala

I am facing an issue when trying to convert a timestamp with the following format 2022-10-25 06:35:12.244 -0700 (this is supposed to be in PST) to an UTC timestamp.
I have tried with this code
data = data.withColumn(utc_convert_column,unix_timestamp(col(utc_convert_column), "yyyy-MM-dd HH:mm:ss.SSS Z").cast("Timestamp"))
data = data.withColumn(utc_convert_column, to_utc_timestamp(data(utc_convert_column), "America/Los_Angeles")
but for the example above 2022-10-25 06:35:12.244 -0700 I am getting the following timestamp 2022-10-25 20:35:12 UTC which is not the right UTC timestamp for that PST.
Looking forward for your help. Thx
The result should be the actual UTC timestamp for that sample I gave.

Related

hive timezone conversion with short Timezone IDs

I am trying to convert UTC time to Local Timezone. I only require the date to be extracted. When I try the below it works and gives correct results.
select to_date(FROM_UTC_TIMESTAMP(current_timestamp(), 'Pacific/Fiji'));
But if I try, short ID of the respective timezone it is giving wrong results.
select to_date(FROM_UTC_TIMESTAMP(current_timestamp(), 'FJT'));
The issue was with the difference in GMT and UTC short IDs. In the above example 'Pacific/Fiji' is a UTC based short ID where are 'FJT' is GMT equivalent. Since we are using hive function FROM_UTC_TIMESTAMP we have to use UTC based short IDs. Here is a list of UTC based short IDs

In SQL Server 2014 , how to convert given UTC date time to PST date time, when the server is in UTC

How to convert given UTC date time to PST date time, by keeping the daylight stuff in time calculations?
Note that the server I am hitting is in utc. I mean, GETDATE() = GETUTCDATE().
Also, we can't use AT TIME ZONE, as DB is on older SQL Server.
Thanks in advance for the help.
Search the site, example: Convert Datetime column from UTC to local time in select statement
Read the link above, also include daylight stuff in there for some of the responses too.

Is date +%s result in local time zone or in UTC?

When I type date +%s into a terminal, is the resulting timestamp given in UTC or is it dependent on my system locale setting? Is there a way to check?
Is there a standard for this across OS's?
From man date:
%s seconds since 1970-01-01 00:00:00 UTC
So it is UTC.

MongoDB java-driver insert date

I'm using MongoDB 2.2 with java-driver 2.10.1
I'm inserting a date field into a document from a java.util.Date instance. My instance has the following value:
Wed Oct 10 00:00:00 CEST 2012
but once in mongo, I have this value:
ISODate("2012-10-09T22:00:00Z")
My insertion code:
BasicDBObject doc = new BasicDBObject("key", event.getKey())
.append("title", event.getTitle())
.append("description", event.getDescription())
.append("date", event.getDate());
db.getCollection("events").insert(doc);
You can have a look to the date instance referenced from my event object on this debug screenshot:
Is there something to do with the timezone ? Or a bug from the driver ?
Dates in MongoDB are always stored as UTC datetimes, so what you're seeing is correct.
The CEST time zone is two hours ahead of UTC (GMT) so your time's correct UTC representation is two hours earlier than your CEST time, which is exactly what you're seeing.

JPA Date and Timezone confusion

I'm having a problem reading dates from a database using JPA. I'm using EclipseLink and PostgreSQL
I've populated my database from a CSV file, witch had dates as strings (in this format: 6/30/2009-23:59:56). I used the following snipet to convert it to a Date object:
public static Date parseDate(String s){
DateFormat formatter = new SimpleDateFormat("d/M/yyyy-k:m:s");
try {
return new Date( ((java.util.Date)formatter.parse(s)).getTime() );
} catch (ParseException ex) {
Logger.getLogger(Type.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
The date is correctly converted and stored in the database as expected. Here is how i map the Date object to the database:
#Column(name="data_ts", nullable=false)
#Temporal(TemporalType.TIMESTAMP)
private Date dataTs;
The problem seems to happen when i try to read the Date from the database to use it in a chart(Highcharts). The record with that same timestamp above get read as:
Mon Jun 06 23:59:56 BRT 2011 and it's timestamp as 1307415596000
Note that it is in Brazilian Time(+3h), so the timestamp (that is calculated from GMT) is 3 hours shifted. Once ploted, the timestamp turns to point to 07/06/2011 02:59:56
Here's an example:
List<TimedataEnt> timeData = currentWellsite.getTimeData();
String debug = timeData.get(timeData.size()-1).getDataTs().toString() + ">>>" + timeData.get(timeData.size()-1).getDataTs().getTime();
where currentWellsite is and JPA Entity, and getDataTs() returns a java.util.Date object.
The string turns out to be "Tue Jun 30 23:59:56 BRT 2009>>>1246417196000"
How do I tell JPA not to convert the timestamp read from the database?
As said, Date and Timestamps have no timezone consideration. It seems that the issue is caused because Java considers that the time it reads from the database is the current default timezone.
So, if the database contais 2011-04-04 14:00:00 and my current timezone is +3, assigning that to java Date will generate 2011-04-04 14:00:00 BRT time(+3), with a timestamp shifted 3 hours (since timestamps are caclulated from UTC).
Solved the issue by getting an calculated timestamp:
long ts = myDate().getTime() + TimeZone.getDefault().getRawOffset();
It's important to say that getRawOffset() does not take Daylight Saving periods in consideration. For that, use getOffset()
Your date is 6/30/2009-23:59:56. I read that as 30 june 2009, 23:59:56. So the format to parse it should be M/d/yyyy-HH:mm:ss or M/d/yyyy-kk:mm:ss (depending on if your hours go from 1 to 24 or from 0 to 23). But definitely not d/M/yyyy-k:m:s: the month comes before the day.
Also, a Timestamp doesn't have any time zone. It's a universal instant in time. It's only when you display its value that the timezone is important, because then you have to choose which time zone to use to display the time. Use a DateFormat with the appropriate timezone set to display your timestamp.
Your issue seems to be that you are storing your Timestamp (which does not have a timezone) into a java.util.Date (which has a timezone offset).
If you want control over how the timezone is set, then store your Timestamp as a java.sql.Timestamp, or use your own #Converter.
In general Calendar should be used in Java instead of java.util.Date, which is for the most part deprecated. Calendar also has a Timezone, so you may have similar issues.