I have created an API endpoint that accepts a parameter called time_zone. I use this parameter to determine what time zone the requesting user is in. time_zone is a utc offset value that should be an integer. Example: MST has a UTC offset of -7.
This allows me to insert the passed in time_zone in the following line of code:
start_time = Time.now.in_time_zone(time_zone).beginning_of_day
The above works fine when the time_zone value is something simple like -7. However, if the time zone offset includes minutes, I run into trouble. Example: Venezuela is UTC-04:30. If I pass in '-430', I get ArgumentError: Invalid Timezone.
What value should I be passing into `Time.now.in_time_zone()' to get my example to work?
Thanks.
After much trial and error, I determined that I need to pass a decimal for time zone offsets that include minutes.
So, to address my previous example using Venezuela (UTC-04:30), I can simply pass in the value -4.5 and that works perfectly.
Similarly, for Nepal (UTC+05:45) I would pass in the value 5.75.
Related
I have a bash script that I use to call a java class and I pass two arguments to this java class. The first argument ($1) is the string that I pass and it contains someone's name. The second argument ($2) is the previous month as a two digit number (also passed in by the user).
So the java class is called like this:
java -DCONFIG_DIR=... com.example.myapp.grades.gradingProcess $1 $2
However, now, I don't want the user to pass in the second argument and instead, I want the script to determine the month.
Can I do something like this?
month=`date +'%m' -d 'last month'`
java -DCONFIG_DIR=... com.example.myapp.grades.gradingProcess $1 $month
And when I run my script, it'll be something like this: ./myscript.sh 'John'
and not pass in a two-digit month since I'm already doing it inside the script?
Or is that not the correct way to go about it?
Sorry if this seems like an elementary question, I'm still trying to get used to bash scripts.
Thank you?
If you are looking for how to supply a default value in the shell, there is an operator for that.
month=${2-$(date -d 'last month' +%m)}
java -stuff "$1" "$month"
Now, if there was a value in $2, month will be set to that; otherwise, the default will be used. The notation ${variable-value} supplies the value of variable or, if it is unset, the text value. (There is also ${variable:-value} which produces value if variable is set but empty as well.)
(This could be inlined into the java command line, even, though using a variable to break it up is probably better for legibility.
java -stuff "$1" "${2-$(date -d 'last month' +%m)}"
Notice also how you basically always put user-supplied variables in double quotes.)
A few thoughts: time zone, leading zero, and leveraging Java rather than bash.
Time zone
Determining the current month requires a current date. Determining the current date requires a time zone.
For any given moment the date varies around the globe. For example, a few minutes after midnight in Paris is a new day while still “yesterday” in Montréal.
When you do not specify a time zone, a current default time zone is implicitly applied. Be aware that the default can change at any moment. And depending on the default means your code is relying on an externality outside your direct control. And it means results will vary.
So around the ending/beginning of the month you could be getting the wrong month number determining on the time zone in play.
Leading zero can mean octal in Java
The %m you are using produces two digits. Single digit month numbers will have a leading zero. Ex: 09
Be aware that in some situations in Java a number with a leading zero is interpreted as a octal number (base 8) rather than a decimal number (base 10).
Let Java do the work
I suggest it makes more sense to let the Java class do the work of determining the previous month. The bash line should be passing the desired/expected time zone rather than the month, if anything.
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ;
int previousMonthNumber = LocalDate.now( zoneId ).minusMonths( 1 ).getMonthValue() ;
Tip: In Java, even better to use an object from the Month enum rather than a mere integer. Makes your code more self-documenting, type-safe, and guarantees valid values.
Month month = LocalDate.now( zoneId ).minusMonths( 1 ).getMonth() ;
I need a hand here
The default OutSystems timezone is UTC
There's an Action called ConvertFromTimeZone(1,2,3)
1- You write the Date and Time, no problem here.
2 & 3 - You have to write the SourceTimeZone and the DestinationTimeZone, these must be written in Text data type.
My question is: How exactly am I supposed to write it?
Thanks.
you can use GetSystemTimeZones to get the list of available timezones in the servers.
This post http://www.outsystems.com/forums/discussion/16005/convertfromtimezone/#Post67485 shows the Identifier that uniquely identifiers a timezone.
Just one correction: the default timezone isn't UTC, but the timezone set in your application server.
The value you should use in the timezone code must be one of Microsoft Time Zone Index Values (column 'Name Of Time Zone')
You can find more information about timezone conversion here:
http://www.outsystems.com/forge/component-discussions/500/Time+Zone
you can use this action ConvertFromUTC(UTC_Datetime, "GMT Standard Time") it is from the Timezone extension.
I need to get time and date from database date like "2015-08-27T12:09:36Z". I tried but not get any solutions where I get date and time in different variable.
I need to get it in Ruby. No rails in my application.
I used below code but not getting.
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone
Any one have a experience in it?
Thanks
I don't have enough reputations to comment so am posting comment as answer, are you looking for this
Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
Which will give the pattern you asked for. Z represent the time zone if you use
%z - Time zone as hour and minute offset from UTC (e.g. +0900)
%:z - hour and minute offset from UTC with a colon (e.g. +09:00)
%::z - hour, minute and second offset from UTC (e.g. +09:00:00)
%Z - Time zone abbreviation name
Check for more
http://apidock.com/ruby/Time/strftime
My Updated answer after your comment
require 'date'
DateTime.parse("2015-08-27T12:09:36Z").strftime('%Y-%m-%dT%H:%M:%SZ')
In your code change Time.strptime('') to DateTime.strptime('')
First, you need to require 'date'. Ruby has built-in Date and Time classes without that require, but the library provides more functionality.
If you have a string retrieved from the database in ISO-8601 format, and you want to turn it into a date and time, just use DateTime.iso8601(string) to get a DateTime object. You can extract the date and time components however you like after that.
irb(main):001:0> require 'date' #=> true
irb(main):002:0> dt = DateTime.iso8601("2015-08-27T12:09:36Z") # DateTime object
irb(main):003:0> d = dt.to_date # Date object
irb(main):004:0> t = dt.to_time # Time object
Is there an object that's part of standard Windows 7 that allows us to, within VBScript, get the local time in a given city/time zone?
One approach would be to use something like SWbemDateTime to convert a given time zone to UTC then re-convert back to another zone. Only the methods necessary to effect the latter step are not present on this particular object.
Check out WMI class 'Win32_ComputerSystem' (http://msdn.microsoft.com/en-us/library/aa394102(v=vs.85).aspx) property 'CurrentTimeZone' which returns the current
time in UTC format.
Also the following classes could be referenced for UTC format:
Win32_Timezone - http://msdn.microsoft.com/en-us/library/aa394498(v=vs.85).aspx
Win32_UTCTime - http://msdn.microsoft.com/en-us/library/aa394510(v=vs.85).aspx
Win32_LocalTime - http://msdn.microsoft.com/en-us/library/aa394171(v=vs.85).aspx
The ActionView::Helpers::FormOptionsHelper provides the time_zone_options_for_select to get a list of options for a select control that includes all of the timezones with their UTC offset. The problem I'm having is how to get it to display the correct offset for when daylight savings time is in effect?
For instance U.S. Mountain time is usually -7 UTC but during the summer it's effectively -6 UTC. Is there a way to have that list correctly reflect that?
TL;DR
The time zone data you are receiving is "correct" because ActiveSupport::TimeZone and TZInfo::Timezone instances do not make assumptions about the current date, and therefore applying DST to them doesn't make "sense" in the context of the responsibility of those objects.
You notice the problem because the default model for time_zone_options_for_select, ActiveSupport::TimeZone, unfortunately returns the offset string when calling #to_s, which, if the location is currently is observing DST, will be incorrect. There is no way to fix the string values generated in the options, or even remove the offset from them.
If this isn't acceptable you'll need to skip on using time_zone_options_for_select, and use options_for_select instead, and generate the options yourself.
Some investigation
time_zone_options_for_select uses ::ActiveSupport::TimeZone as the default model parameter, so passing it in manually will not change your results. In order to construct options for a select box, that method will construct an array of tuples in the format of [time_zone.to_s, time_zone.name], for the purpose of passing it to the more generic options_for_select method. time_zone, in this case, is an instance of ::ActiveSupport::TimeZone.
The important factor here is that this time zone instance object is, conceptually, completely unrelated/divorced from the idea of "the current date". The definition of a time zone (strictly speaking) has nothing to do with the current date. We can confirm this "not using DST" issue like so:
::ActiveSupport::TimeZone.all.find { |tz| tz.name == "Adelaide" }.utc_offset
=> 34200 # 9 hours and 30 minutes, in seconds
Adelaide's non-DST time zone is ACST (Australian Central Standard Time) which is GMT+9.5. Currently (as in the time of writing), Adelaide is in DST which means they are on ACDT (Australian Central Daylight Time), which is GMT+10.5.
::ActiveSupport::TimeZone.all.find { |tz| tz.name == "Adelaide" }.now.utc_offset
=> 37800 # 10 hours and 30 minutes, in seconds
The crucial difference here is essentially what I've outlined above - the ActiveSupport::TimeZone instance is just not concerned with the current date. The class itself is a convenience wrapper around a TZInfo::DataTimezone instance, which has similar opinions on the current date - none.
If you noticed, in the second code snippet above, we called #now on the time zone object before calling #utc_offset. This returns an ActiveSupport::TimeWithZone instance, which includes information about the time zone, as well as the current date - and therefore we get an offset which reflects the fact that the current date should include the DST offset.
So, the only problem here is that including the UTC offset string in the return value of #to_s on ActiveSupport::TimeZone instances is sort of misleading in this instance. It's included because it's the "base" UTC offset for that time zone.
Resources:
Rails 6.1 time_zone_options_for_select implementation
Related GitHub issue, rails/rails#7297
Related GitHub pull request, rails/rails#22243
I had similar problem but ended up using this
time_zone_select('time_zone', TZInfo::Timezone.us_zones,
:default => "America/Los_Angeles",
:model => TZInfo::Timezone
Did you find a better solution?