Does Ruby's strftime have a format for the month without a leading zero?
I found %e for getting the day without the leading zero, but not having any luck with the month.
Ultimately wanting a date formatted like: 9/1/2010
Some versions of strftime do allow prefixing with minus to format out leading zeros, for eg:
strftime "%-d/%-m/%y"
However this will depend on strftime on your system. So for consistency I would do something like this instead:
dt = Time.local(2010, 'Sep', 1)
printf "%d/%d/%d", dt.day, dt.month, dt.year
Here's the formatting list I go off of. This is from the docs for 2.1.3. According to this you would want %-m:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%C - year / 100 (rounded down such as 20 in 2009)
%y - year % 100 (00..99)
%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%B - The full month name (``January'')
%^B uppercased (``JANUARY'')
%b - The abbreviated month name (``Jan'')
%^b uppercased (``JAN'')
%h - Equivalent to %b
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
%e - Day of the month, blank-padded ( 1..31)
%j - Day of the year (001..366)
Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')
%M - Minute of the hour (00..59)
%S - Second of the minute (00..60)
%L - Millisecond of the second (000..999)
The digits under millisecond are truncated to not produce 1000.
%N - Fractional seconds digits, default is 9 digits (nanosecond)
%3N millisecond (3 digits)
%6N microsecond (6 digits)
%9N nanosecond (9 digits)
%12N picosecond (12 digits)
%15N femtosecond (15 digits)
%18N attosecond (18 digits)
%21N zeptosecond (21 digits)
%24N yoctosecond (24 digits)
The digits under the specified length are truncated to avoid
carry up.
Time zone:
%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 - Abbreviated time zone name or similar information.
Weekday:
%A - The full weekday name (``Sunday'')
%^A uppercased (``SUNDAY'')
%a - The abbreviated name (``Sun'')
%^a uppercased (``SUN'')
%u - Day of the week (Monday is 1, 1..7)
%w - Day of the week (Sunday is 0, 0..6)
ISO 8601 week-based year and week number:
The first week of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
%G - The week-based year
%g - The last 2 digits of the week-based year (00..99)
%V - Week number of the week-based year (01..53)
Week number:
The first week of YYYY that starts with a Sunday or Monday (according to %U
or %W). The days in the year before the first week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
%W - Week number of the year. The week starts with Monday. (00..53)
Seconds since the Epoch:
%s - Number of seconds since 1970-01-01 00:00:00 UTC.
Literal string:
%n - Newline character (\n)
%t - Tab character (\t)
%% - Literal ``%'' character
Combination:
%c - date and time (%a %b %e %T %Y)
%D - Date (%m/%d/%y)
%F - The ISO 8601 date format (%Y-%m-%d)
%v - VMS date (%e-%^b-%4Y)
%x - Same as %D
%X - Same as %T
%r - 12-hour time (%I:%M:%S %p)
%R - 24-hour time (%H:%M)
%T - 24-hour time (%H:%M:%S)
Updated to latest 2.1.3 docs on 10/24/14
Docs show a number of different options for configuring number format. Adding to the %-d format, you can also use these flags in place of "-":
Flags:
- don't pad a numerical output.
_ use spaces for padding.
0 use zeros for padding.
^ upcase the result string.
# change case.
: use colons for %z.
I had a similar problem and fixed it by converting strftime("%m") into an integer.
For example:
strftime("%m")+0 give the current month as integer 'without leading zero'
Simple, though not elegant.
Related
Converting a DateTime to unix convention using %s%L (%s means seconds since Jan 1st 1970, %L means to append 3 digits to represent milliseconds) give this:
DateTime.now.strftime '%s%L'
=> "1656279075654"
How can I get a DateTime back from this?
DateTime.strptime('1656279075654', '%s%L') gives an error, as strptime doesn't seem to know %L strangely.
secs = '1656279075654'.to_f / 1000
DateTime.strptime(secs.to_s, '%s')
or
DateTime.strptime('1656279075654', '%Q')
(%Q - number of milliseconds since 1970-01-01 00:00:00 UTC)
How should I interpret all aspects of the following timestamps? Where is the time based and how do timezones apply?
2015-11-15T14:45:28Z
2015-11-15T14:45:28.9694Z
2015-11-15T14:45:28.969412345Z
Below is my thoughts...
Date: 2015-11-15
???: T
Hours: 14
Minutes: 45
Seconds: 28 OR 28.9694 OR 28.969412345
???: Z
Most of your values are attributed correctly. The date portion (2015-11-15) is in the order YYYY-MM-DD, time in HH:MM:SS.ffff.
T indicates the start of the time portion of the date time.
Z indicates the time zone is UTC. Next to Z, you could have a format like Z+02:00, which indicates the time zone is UTC + 2 hours.
In particular, to add time (e.g. 11:40 + 00:30 = 12:10) and check whether a time belongs to a range (e.g. (11:00..12:00).include?(11:30)).
I understand that I can write a class, but maybe a solution already exists.
The built-in Time class is not entirely what I want, because I am not interested in date-related features, which are built-in.
You can use strftime to format the time any way you want. If you want only the hour and minutes you can use this:
Time.now.strftime("%H:%M")
=> "08:57"
time.strftime gives you all of these options to format
%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (“AM” or “PM”)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal “%” character
Here is a link to the
strftime docs
To check if time belongs to a certain range you can use the cover
method
2.1.5 :003 > (Time.now..Time.now+10).cover?(Time.now)
=> true
2.1.5 :004 > (Time.now..Time.now+10).cover?(Time.now+20)
=> false
ActiveSupport's Numeric will help you.
You can do:
require 'date'
require 'active_support/all'
DateTime.parse("11:40") + 30.minutes
You will need the ActiveSupport gem in your Gemfile.
As for checking if a time is in a range you can use #cover:
irb(main):001:0> (DateTime.parse("11:40")..DateTime.parse("11:50")).cover?(DateTime.parse("11:45"))
=> true
irb(main):002:0> (DateTime.parse("11:40")..DateTime.parse("11:50")).cover?(DateTime.parse("12:00"))
=> false
When I hit a meetup group on Meetup.com to find upcoming events I end up with this fragment of JSON that I believe is when the next meetup is scheduled.
"utc_offset": -21600000,
"time": 1415752200000,
"waitlist_count": 0,
"updated": 1382991416000,
I'm trying to convert this to a humanized Date/Time in Ruby, but I'm not sure how to do this?
It seems that the time stamps here were in milliseconds. Divide them by 1000 and ruby can convert the seconds to Time.
You can use Time.at(i) in Ruby to convert integer to Time.
Time.at(1415752200000/1000) # => 2014-11-12 08:30:00 +0800
Time.at(1382991416000/1000) # => 2013-10-29 04:16:56 +0800
For more info, see http://www.ruby-doc.org/core-2.1.3/Time.html#method-c-at
As I understood from your time object its return only time(in miliseconds) not date.
For that you can try this using strftime method with Time class
seconds = 1415752200000/1000
Time.at(seconds).strftime("%H:%M:%S")
=> "06:00:00"
As per your requirement you can modify formatting of strftime() method like
Time.at(seconds).strftime("%Y-%B-%d %H:%M:%S")
=> "2014-November-12 06:00:00"
Available Formatting Options:
%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (“AM” or “PM”)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal “%” character
I am trying to parse a string 26/03/2012, in dd/mm/yyyy format to Ruby's Date using Date.strptime, as follows:
#!/usr/bin/ruby
require 'date'
puts 'Ruby Version: ' + RUBY_VERSION
date_str = '26/03/2012'
date = Date.strptime(date_str, "%d/%m/%y")
puts 'Parsed Date: ' + date.to_s
The output is:
Ruby Version: 1.8.7
Parsed Date: 2020-03-26
The year part has become 2020, instead of 2012!
That should be %Y upper case, rather than %y:
date = Date.strptime(date_str, "%d/%m/%Y")
puts 'Parsed Date: ' + date.to_s
# Parsed Date: 2012-03-26
From the docs:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%C - year / 100 (round down. 20 in 2009)
%y - year % 100 (00..99)
Since %y expects two digits only, it takes the first two 20 and assumes that to be a 2 digit representation of 2020, since 2020 % 100 = 20.
If you change your strptime function to
date = Date.striptime(date_str, "%d/%m/%Y")
it will output correctly.