I would like to either get the datetime of when the message was sent in user's local timezone or his/her local timezone.
How can I get that from in my bot? facebook gives the timestamp but I doubt if that timestamp is calculated based on the local timezone of the user.
The documentation gives this example:
{
"sender":{
"id":"USER_ID"
},
"recipient":{
"id":"PAGE_ID"
},
"timestamp":1458692752478,
"message":{
"mid":"mid.1457764197618:41d102a3e1ae206a38",
"text":"hello, world!",
"quick_reply": {
"payload": "DEVELOPER_DEFINED_PAYLOAD"
}
}
}
The timestamp field in this example is a Unix timestamp in terms of milliseconds. Unix timestamps are always in terms of Coordinated Universal Time (UTC). In other words, the epoch reference point is January 1st, 1970, 00:00:00.000 UTC.
Therefore, time zones are irrelevant to the message body. You should simply convert from UTC to the desired time zone (probably the user's local time zone) when the message is displayed.
Related
I have a use-case in which I need a user to enter an availability date on the browser (which will be saved in the database in UTC format) for example
July 2nd at 6:30PM,
i am getting IANA location string using momentjs
moment.tz.guess(); //'America/New_York'
now when I do the server side rendering, I would like to render the availability date as
July 2nd at 6:30 EST
or
July 2nd at 6:30 PMT
Is there a javascript library to do this for me? how can I convert 'America/New_York' to EST?
worth mentioning it is a reactjs application
we can achieve this by momentjs. get the IANA timezone with guess() function as explained above to set the timezone and then format it with as documented in moment documents
const date = user input date saved in the database
const z = value of moment.tz.guess() which saved in db
moment(date).tz(z).format('MM/DD/YYYY hh:mm A z');
The ability to use date formatting in the Slack API to display a date/time in the local timezone of the person seeing the message is great, but I'd like people to know that the date/time is in their local timezone.
Using <!date^1392734382^{date_short_pretty} {time}|2014-02-18 6:39 AM PST> will display as "Posted Feb 18, 2014 6:39 AM" if the receiver is in California, and "Posted Feb 18, 2014 8:39 AM" if the receiver is in Chicago (see here in Block Kit Builder) but there is no indication to the receivers that the date/time is in their own timezone.
Is there any way to include the receiver's time zone as part of the token string?
This is not a feature that is provided by the API. The only solution I can think of is to add some info text like "(local time)" to the datetime output in your message.
I am using Esper & I need to filter events by their timestamp. The events come from an external source.
The challenge is that the cutoff instant is at a different timezone than the events` timestamp, e.g. the cutoff instant is at 3:30 CET (e.g. Prague time) while the timestamp field of the event is at UTC.
This poses a problem when the timezone shifts to Daylight Savings Time, because the cutoff instant needs to be modified in the query. E.g. in this case, if the cutoff instant is 3:30 CET, during winter time it would be on 2:30 UTC and during DST it would be on 1:30 UTC. It means that I have to change the query when the time shifts into and out of DST.
This is the current query:
SELECT *
FROM my_table
WHERE timestamp_field.after( timestamp.withtime(2,30,0,0) )
I would like to have a robust solution that will save me the hassle of changing the cutoff timestamp queries every few months. Can I add the timezone to the query statement itself? Is there any other solution?
It may help to add an event property to the event that represents UTC time i.e. normalize the event timestamp to UTC and use the normalized property instead.
The query could also use a variable instead of the hardcoded numbers. Another option would perhaps be changing Esper source to take in a timezone for some func.s
After struggling unsuccessfully with trying ot do it in the WHERE caluse or using a Pattern, I managed to solve the issue using a [Single-Row Function plugin][1].
I pass the plugin function the cutoff hour, timezone & event timezone and compute the cutoff hour in the event's timezone.
My query changed to:
SELECT *
FROM my_table
WHERE timestamp_field.after( timestamp.withtime(
eventTZHour(2, 'UTC', 'Europe/Prague'), 30, 0, 0) )
I added the Java implementation in a class:
public class EsperPlugins {
public int eventTZHour(int hour, String eventTZ, String cutoffTZ) {
// return tz calculations
}
}
and finally registered the plugin in esper.cfg.xml:
<esper-configuration>
<plugin-singlerow-function name="eventTZHour"
function-class="EsperPlugins"
function-method="eventTZHour"/>
</esper-configuration>
[1]: http://www.espertech.com/esper/release-5.2.0/esper-reference/html/extension.html#custom-singlerow-function from esper's docs
In my client application I have the Latitude & longitude information from skyhook API based on its I.P.
Now based on the latitude and longitude information I need to find out the timezone information of the client. But in the google timezone API documentation https://developers.google.com/maps/documentation/timezone/ I see that timestamp is a mandatory field. In which case what should I need to do.
Also can you kindly help me understand what does the timestamp corresponds to? For e.g :- If my Application server is in U.S.A (say PST timezone) and it makes the google API call passing the server timestamp.
If user logs into client application from India passing lat / long information, to the app server to get the timezone information what will the API provide as dstOffset and rawOffset? i.e If I add the server timestamp with dstOffset and rawOffset will I be getting the client machine timezone information?
I've been scratching my head on Google's Timezone API for a few minutes, specifically on the timestamp parameter. Maybe this will lay out the need:
In San Diego, we (now, in August) have a GMT offset of -8 because of daylight savings time. However, in November we'll have a GMT offset of -7.
So which gmt offset should Google return? -7 or -8? They're both valid, but it depends on what day you take the measurement.
Enter the Timestamp argument. Running the service now, and using a timestamp value of August 2015, I get this response:
{
"dstOffset" : 3600,
"rawOffset" : -28800,
"status" : "OK",
"timeZoneId" : "America/Los_Angeles",
"timeZoneName" : "Pacific Daylight Time"
}
But if I bump the timestamp to November 2015 (once San Diego is out of daylight savings, I end up with this):
{
"dstOffset" : 0,
"rawOffset" : -28800,
"status" : "OK",
"timeZoneId" : "America/Los_Angeles",
"timeZoneName" : "Pacific Standard Time"
}
In both cases the rawOffset is the same, but the DST changed because of the timestamp I provided. If you just want to know the raw timezone, the timestamp doesn't matter.
But if you want an application to reliably do something at 8:00am in San Diego in August and 8:00am in November in San Diego, you'll need to engage the timestamp.
Putting it another way, what's the value of knowing that San Diego is normally -7 hours offset from GMT. If you're working with timezones, you're likely trying to ensure that your UTC time is matched up with what a real person in that location is experiencing. As such, the DST offset is critical.
The documentation link you provided clearly states that the timestamp should be in UTC and that it is used to show the correct DST offset value. It will also control if the timeZoneName field is shown with "Standard" or "Daylight" in the name.
If you don't care about that and just want the timeZoneId field, then it doesn't matter what value you pass.
Try this
DateDiff("s", "1/1/1970", DateTime.Now)
I am trying to get the time of other GMT value by using
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone(gmt));
but how can i get the DST time at that time zone.
The TimeZone class provides a getDSTSavings() method for a specific TimeZone Object. (JavaDoc: "Returns the amount of time to be added to local standard time to get local wall clock time.")
The Calendar interface provides two getOffset() methods, which let you find out the offset from UTC. (JavaDoc: "Returns the offset of this time zone from UTC at the specified date. If Daylight Saving Time is in effect at the specified date, the offset value is adjusted with the amount of daylight saving. ")
please see this piece of code to grok the complicated ways of java time:
#Test
public void testDST() {
final TimeZone met = TimeZone.getTimeZone("MET");
Calendar gc = new GregorianCalendar(met);
final long timeInMillis = gc.getTimeInMillis();
final long gmtTime= timeInMillis-(gc.getTimeZone().getOffset(timeInMillis));
final Date gmtDate = new Date(gmtTime);
System.out.printf("%-40s: %tc\n%-40s: %tc\n%-40s: %tc\n%-40s: %d\n%-40s: %d",
"new Date() (local timezone)",new Date(),
"UTC", gmtDate ,
"now from Calendar with TC GMT+02:00",gc,
"zoneoffset",gc.get(Calendar.ZONE_OFFSET),
"dst savings",met.getDSTSavings());
}
You can also define your own SimpleTimeZone and provide custom DST rules, however, i have not found out how to get this information from the predefined TimeZones.
You should also be aware, that if TimeZone.getTimeZone(TZName) does not find the specified timezone, it does not throw an exception, but it just uses GMT, which can cause major misunderstandings.
You can find all this information (and a lot more) in javadoc for Calendar, TimeZone, Date, etc.
There are few methods available in java.util.TimeZone to get Daylight Saving Time. Please check out the BlackBerry Java Docs page.