How to convert UDate from one timezone to another using ICU - c++

After spending some significant amount of time, I could not figure out how to convert UDate from one timezone to another. Here is the problem I am trying to solve :
I have a timestamp which is number of milliseconds since epoch. This is a timestamp in UTC. I want to convert this timestamp to a timestamp at some local time zone (e.g. US/Eastern). I want to extract number of days since epoch and number of milliseconds since from converted timestamp. I want to use icu library to do this.
I tried to create a UDate from number of milliseconds since epoch in UTC. I can create a timezone instance for the given timezone.
TimeZone *tz = TimeZone::createTimeZone("US/Eastern");
How do I convert UDate from UTC to the given timezone and extract the answers I want? Could it be done using icu?
Any help will be greatly appreciated.

I want … number of days since epoch and number of milliseconds since from converted timestamp.
I think these will be the same regardless of time zone, won't they?
Or can you give an example of the result you would like to see (before vs. after)

Related

Storing unix timestamp as an IntegerField [duplicate]

Which one is best to use, DateTime or INT (Unix Timestamp) or anything else to store the time value?
I think INT will be better at performance and also more universal, since it can be easily converted to many timezones. (my web visitors from all around the world can see the time without confusion)
But, I'm still doubt about it.
Any suggestions?
I wouldn't use INT or TIMESTAMP to save your datetime values. There is the "Year-2038-Problem"! You can use DATETIME and save your datetimes for a long time.
With TIMESTAMP or numeric column types you can only store a range of years from 1970 to 2038. With the DATETIME type you can save dates with years from 1000 to 9999.
It is not recommended to use a numeric column type (INT) to store datetime information. MySQL (and other sytems too) provides many functions to handle datetime information. These functions are faster and more optimized than custom functions or calculations: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
To convert the timezone of your stored value to the client timezone you can use CONVERT_TZ. In this case you need to know the timezone of the server and the timezone of your client. To get the timezone of the server you can see some possibilites on this question.
Changing the client time zone The server interprets TIMESTAMP values
in the client’s current time zone, not its own. Clients in different
time zones should set their zone so that the server can properly
interpret TIMESTAMP values for them.
And if you want to get the time zone that a certain one you can do this:
CONVERT_TZ(#dt,'US/Central','Europe/Berlin') AS Berlin,
I wouldn't store it in int, you should check out MySQL Cookbook by Paul DuBois he covers lot's of things in it.Also there is a big portion about your quetion.

How to convert seconds from Epoch time to UTC time in Pyspark?

Say I have a dataframe with a column name epoch that is seconds from epoch time and I wanted to convert it to UTC time. How to do it in Pyspark?
I dont want to harcode "GMT-7" and I want to make sure I use the right offset for the time, GMT-7 or GMT-8 based on daylight savings. I'm running Spark 2.1
myDF = myDF.withColumn("epoch_tmst",(myDF.epoch.cast(dataType=TimestampType()))
myDF = myDF.withColumn("epoch_tmst_utc",to_utc_timestamp(myDF.epoch_tmst,"GMT-7"))
There is no way to convert unixtime into UTC timestamp directly by pyspark method.
(You can do unix->GMT-7 & GMT-7->UTC separately by pyspark, but looks like that's not what you want)
How about take them into literal string once, and use Python's time module?
https://docs.python.org/3/library/time.html#time.gmtime

Is there an API to convert time in FILETIME format from UTC to local time?

I know that SystemTimeToTzSpecificLocalTime API can convert from UTC time to local time, but it takes time in SYSTEMTIME format. I'm curious if there is an API that accepts FILETIME format instead?
PS. I know that I can achieve this by using FileTimeToSystemTime() and then SystemTimeToFileTime(). I was just trying to save two steps for converting to SYSTEMTIME and back.
Have a look at FileTimeToLocalFileTime()
Converts a (UTC-based) file time to a local file time.
And LocalFileTimeToFileTime():
Converts a local file time to a file time based on the Coordinated Universal Time (UTC).
However, you cannot convert between a UTC FILETIME and a local FILETIME in a specified timezone. The current timezone of the local machine is used for the conversion. The documentation states:
To account for daylight saving time when converting a file time to a local time, use the following sequence of functions in place of using FileTimeToLocalFileTime:
1. FileTimeToSystemTime
2. SystemTimeToTzSpecificLocalTime
3. SystemTimeToFileTime
And
LocalFileTimeToFileTime uses the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, this function will take daylight saving time into account, even if the time you are converting is in standard time.

Why we substract the unix epoch from boost::posix_time::ptime to obtain time_t

could someone please explain why we need to subtract boost::gregorian::date(1970,1,1) from a boost::posix_time::ptime to obtain a POSIX notation of time (i.e. microseconds since 1970,1,1 midnight UTC time zone)
For example this link provides clear instructions, but not the reason:
How do I convert boost::posix_time::ptime to time_t?
Thanks
Because you are converting a date to a time.
From the Boost documentation :
Posix Time
Defines a non-adjusted time system with nano-second/micro-second
resolution and stable calculation properties [...] This time system uses the Gregorian calendar to implement the date portion of the time representation.
So boost::posix_time::p_time is not just the epoch time (as return by time(), for example)), but rather the date and the time, expressed (obviously) since the beginning of the Gregorian calendar.
So if you want a Posix Epoch Time (from since 1970,1,1 midnight UTC time zone), you will need to do the subtraction of all those years.

FILETIME information retrieval on changing timezones

Suppose i have a FILETIME which has some value (which we can see by converting it to systemtime) when I am in timezone1. Suppose I changed my timezone to timezone2.
Now I want to get the time in hours and minutes (format) which was in timezone1.
A simple way would be to convert it to system time then use SystemTimeToTzSpecificLocalTime to convert the time to a timezone specific time.