using ruby to correct timezone based on Day Light Savings Time [duplicate] - ruby

How do you convert a timezone from int to string (etc: Europe/Stockholm)?
I'm using Facebooks PHP api and that returns ["timezone"] => int(2). How am I suppose to parse that?

How do you convert a timezone from int to string (etc: Europe/Stockholm)?
You can't. Read "Time Zone != Offset" in the timezone tag wiki.
Facebook's documentation explains that the timezone field is the user's timezone offset from UTC.
What it doesn't make clear, but I have found through experimentation, is that it is not necessarily the user's current offset, but it instead it is the offset as of the user's last log in. If the user changes time zones, or daylight saving time begins or ends, that will not be reflected in the Facebook data until the user's next log in.
See also these posts:
Facebook API, timezone and country
FaceBook Time zone and Event Times

You can get the timezone name by using timezone_name_from_abbr("", $value*3600, false);. $value is the timezone offset you get from Facebook.
Thanks to minaz.

Related

Get Last modified date on Blob Storage

I'm uploading a file into Azure Storage Explorer, the blob check every 12 hours for a file, if there is a file an email is sent. When an email is sent I need to get the time and date when the file is uploaded not when the email is sent. I use the List of Files LastModified and the output is like this 2021-09-22T15:15:55Z, I don't need the time in UTC. Is there any way to change convert the time using the List of Files LastModified?
The 'List of Files LastModified' gives the exact thing that you required while in order to change the utc to local/Any time zone that we required, we need to add a function 'convertFromUtc'.
For CST :
formatDateTime(convertTimeZone(utcNow(), 'UTC', 'Central Standard Time'),'HH:mm:ss')
For IST:
formatDateTime(convertTimeZone(utcNow(), 'UTC', 'India Standard Time'),'HH:mm:ss')
else you can perform hardcoded operation such as to SubtractFromTime or AddToTime considering the timezone that we wanted.
Here is the flow of my logic app
REFERENCES :
Reference guide for functions in expressions - Azure Logic Apps | Microsoft Docs

DRF datetime field serializer 'Invalid datetime for the timezone' twice a year

I'm importing data from csv to InfluxDB through a Django Rest Framework API endpoint.
The relevant part of the viewset:
if request.method == "PUT":
measurements = InputMeasurementSerializer(data=request.data, many=True)
measurements.is_valid(raise_exception=True)
The serializer:
class InputMeasurementSerializer(serializers.Serializer):
value = serializers.FloatField()
time = serializers.DateTimeField(input_formats=[
"%Y-%m-%d_%H:%M:%S", ...])
The input data is in the form of time value paires for every 15 minutes:
time,value
2021.04.11 00:00:00,0.172
2021.04.11 00:15:00,0.76
2021.04.11 00:30:00,0.678
2021.04.11 00:45:00,1.211
It works fine for all dates, except for the time values on the 28.03.2021 between 02:00-03:00 and on the 25.10.2020 between 02:00-03:00 it throws the exception: Invalid datetime for the timezone "Europe/Budapest".
Could be related to the time setting because of the daylight saving - but I don't see how exactly. Has anyone any clue what could be the problem here?
In my settings.py:
TIME_ZONE = 'Europe/Budapest'
USE_TZ = True
It indeed is because of the daylight savings, explained in the documentation
Even if your website is available in only one time zone, it’s still good practice to store data in UTC in your database. The main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen. (The pytz documentation discusses these issues in greater detail.) This probably doesn’t matter for your blog, but it’s a problem if you over-bill or under-bill your customers by one hour, twice a year, every year. The solution to this problem is to use UTC in the code and use local time only when interacting with end users.

YouTube Live Streaming API Snippet.scheduledStartTime not Working while Creating Live Broadcast

I am creating YouTube live broadcast using API explorer everything is working fine but Snippet.scheduledStartTime is not working fine.
My time zone is +05:00 and country is Pakistan.
I want to schedule event at 2019-01-18 04:50 PM. I have set the Snippet.scheduledStartTime to 2019-01-18T11:50:00.000Z but on event page it shows Starts January 18, 2019 at 3:50 AM (PST) when I click edit it shows wrong country and time zone like that United States (GMT -08:00) Pacific
I want to use javascript or php client libraries.
Please let me know how I can fix it?
Here is my php code
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
$broadcastSnippet->setTitle('New Test Broadcast');
$broadcastSnippet->setScheduledStartTime('2019-01-18T11:50:00.000Z');
According to the YouTube Live Streaming API Docs, the format for scheduledStartTime must be in ISO 8601 format:
datetime
The date and time that the broadcast is scheduled to start.
The value is specified in ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) format.
I would suggest that you check the time zone designator you have set.
Create date with timezone
I am not a PHP dev but the smartest thing to do IMO would be to create a date in the correct timezone and then out put it in the correct format. After about five minutes of googleing i found this.
$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('c');

How to get latest updated record from sys_user table which is modified at or after certain time stamp in ServiceNow

I want to fetch record from the sys_user table which is updated at or after certain time stamp.
for that I have created rest request as
https:/service-now.com/api/now/v1//table/sys_user?sysparm_query=sys_updated_on>=javascript:gs.dateGenerate('2017-10-30','01:25:00')
I had converted current time which is in IST format into GMT and pass it to dateGenerate() function.
Problem statement -
I don't want to convert the IST to GMT, is there any way by which i can identify ServiceNow instance time zone at runtime and convert given time into that time stamp and get the users.
If i can pass this date and time in UTC format.
Ahoy!
This is a great question, and something that's quite difficult in ServiceNow (dealing with time-zones).
As such, I've written a tool to manage this for you. It's totally free!
The tool is called TimeZoneUtil, and can be found here:
https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil
You simply need to initialize a GlideDateTime object, set its' time-zone to IST, use setDisplayValue() to set its' time based on IST current time, then use .getValue() to get that same time in system time.
This is because getDisplayValue()/setDisplayValue() work based on time-zone, whereas setValue()/getValue() always return the corresponding system time.
EDIT: In order to make this a little more clear, I'll provide some example usage below.
var tz = new TimeZoneUtils(); //initialize with current time
gs.print(tz.getOffsetHours()); //prints out "-7" in my instance, as the system time is in Pacific.
tz.setTimeZone('Asia/Kolkata'); //sets the time-zone to IST/UTC+5.5
gs.print(tz.getOffsetHours()); //prints "5.5".
gs.print(tz.getGDT().getDisplayValue()); //Prints the current time in IST (2017-11-01 20:52:31 at the moment).
gs.print(tz.getGDT().getValue()); //Prints the current time in system time (2017-11-01 15:23:12 at present).
gs.print(new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()); //Single line, also prints current time in IST: 2017-11-01 20:52:31
The first 6 lines there, demonstrate basic usage and explain how it works.
The eighth line however, demonstrates usage on a single line, which you can use inside a query string. For example:
sysparm_query=sys_updated_on>=javascript:new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()
Hope this helps!
Tim Woodruff
Author, Learning ServiceNow & Building Powerful Workflows
Owner/Founder, SN Pro Tips

Why Google Calendar API V3 didn't accept all timezone IDs?

We are adding event with start time and end time like below snippet,
EventDateTime startEventDateTime = new EventDateTime().setDateTime(startDateTime).setTimeZone(timeZone);
EventDateTime endEventDateTime = new EventDateTime().setDateTime(endDateTime).setTimeZone(timeZone);
If the timeZone is either "Asia/Calcutta" or "GMT +05:30", it will be added in Google Calendar successfully, but some of our users' timezone is like "IST","PST","CST". If we give it in timeZone variable, Google Calendar throws
"Invalid time zone definition for start time."
Is it possible to accept even if the timezone is like "IST","PST","CST","EST"?
Time Zone should be in same format as shown in Google Calendar time Zone link.
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them. So just because of this Google might not accept the Three-letter time zone IDs.

Resources