I'm looking to compare the dates of some AWS resources in an external script so I need to match AWS's date/time format.
AWS' own documentation states that the date must be the complete date plus hours, minutes, and seconds, however date formats end up looking like 2017-07-27T15:47:59.373Z, where my hardcoding of %Y-%m-%dT%H:%M.%SZ gets me only 2017-07-15T11:39.29Z.
Side by side, that's:
AWS: ??? - 2017-07-27T15:47:59.373Z
Me: %Y-%m-%dT%H:%M.%SZ - 2017-07-15T11:39.29Z
There's something on the end that's adding a few extra digits. What am I missing to get the formatting identical?
The "extra digits" is the milliseconds.
I'm assuming you're using bash's date command. If that's the case, try with:
date -u +"%Y-%m-%dT%H:%M:%S.%3NZ"
The -u option gets the date in UTC (so it's compliant with the Z, which is the UTC designator).
If you don't use -u, it'll print the date and time in the system's timezone (which can't necessarily be UTC).
Related
There are several ways to get time in Tarantool:
Using the clock module
Using fiber.time function
Using os.date
But what is the correct way to work with dates?
For the first, there are several routines for Unix epoch:
os.time() — classic Lua time function. Kinda slow and not very efficient. Not recommended to use inside tarantool for getting current epoch, but of course will work. May be used for getting epoch of arbitrary date (within local timezone). ex:
os.time({ year = 2020, month = 6, day = 4 })
will produce 1591261200, which is 12:00:00 in my GMT+3 timezone
clock.time() (and clock.time64()) — High resolution timer, almost raw binding to clock_gettime. More information may be taken from doc
fiber.time() (and also fiber.time64()) — Cached version of clock.time. Updated every event loop iteration. Recommended for use if absolute precision of clock is not required.
For converting epoch into different formats and timezones there are variants:
os.date("<format>" [, epoch ]) — Convert epoch into local timezone.
os.date("!<format>" [, epoch ]) (note ! prefix) — Convert epoch into GMT timezone.
For getting components of a date as a table we may use os.date('*t') for local and os.date('!*t') for UTC
icu-date may be considered it you need to work with different timezones and/or formats.
For example, if you need UTC time, it's ok to use cached fiber.time with os.date:
local fiber = require 'fiber'
os.date("!%Y-%m-%dT%H:%M:%SZ", fiber.time())
will return something like 2020-06-04T11:48:54Z independently on timezone
It depends on your task.
If it's important for you to manipulate with timezones/formats etc.
I suggest to use icu-data library (https://github.com/tarantool/icu-date)
I have file strings that contain dates in their heading, stuff such as 2017-03-06 092328 - iPhone - Music Show - street performance, while walking the dog.m4a
I would like to parse the date our of this string, because I need to print it in a podcast friendly format.
I'm able to parse out the date, but for some reason the time component refuses to be parsed out :)
Date.strptime("2017-03-06 092328", "%Y-%m-%d %H%M%S").strftime(%Y-%m-%d %H%M%S)
Expected output:
"2017-03-06 092328"
Actual output
"2017-03-06 000000"
Your problem is that Date is (what a surprise!) a date, as in, "Not a time, just a date".
require 'time'
Time.strptime("2017-03-06 092328", "%Y-%m-%d %H%M%S").strftime("%Y-%m-%d %H:%M:%S")
#=> "2017-03-06 09:23:28"
To be fair :
the documentation of Time#strptime mentions that it's based on Date#strptime
Date._parse("2017-03-06 09:23:28") will happily return {:hour=>9, :min=>23, :sec=>28, :year=>2017, :mon=>3, :mday=>6}
If you find it confusing that a Time object has a date and a time but that Date just has a date, you could use DateTime.
What's the difference between strptime and strftime? I see that strptime is a method in the DateTime class, and strftime is a method in the Time class.
What's the difference between Time and DateTime, other than that they have different core methods? The explanation for the Time class in the Ruby docs is helpful, but the one for DateTime just says "datetime". There's also the Date class, which says it provides Date and DateTime. Help me make sense of this.
I see strptime and I want to pronounce it "strip time", but that doesn't make sense. Is there a good mnemonic-device for it?
What do strptime and strftime mean, anyway?
How do you remember which does what?
The difference between Time and DateTime has to do with implementation. A large amount of the DateTime functionality comes from the Rails world and is an arbitrary date with time of day. It's more of a calendar-based system. Time is measured as seconds since January 1, 1970 UTC and is time-zone agnostic. On some systems it is limited to values between 1901 and 2038, a limitation of how traditionally this value is stored as a signed 32-bit integer, but newer versions of Ruby can handle a much wider range, using a 64-bit value or BigNum as required.
In short, DateTime is what you get from a database in Rails where Time is what Ruby has traditionally used. If you're working with values where dates are important and you want to know things like the end of the month or what day it'll be six weeks ahead, use DateTime. If you're just measuring elapsed time and don't care about that, use Time. They're easy to convert between if necessary.
Date on the other hand is just a calendar date and doesn't have any associated times. You might want to use these where times are irrelevant.
strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification. I've rarely seen strptime used since DateTime.parse is usually good at picking up on what's going on, but if you really need to spell it out, by all means use the legacy parser.
strptime means string parser, this will convert a string format to datetime.
Example:-
datetime.strptime('2019-08-09 01:01:01', "%Y-%m-%d %H:%M:%S")
datetime.datetime(2019, 8, 9, 1, 1, 1)//Result
strftime means string formatter, this will format a datetime object to string format.
Example:-
sample_date=datetime.strptime('2019-08-09 01:01:01', "%Y-%m-%d %H:%M:%S")
datetime.strftime(sample_date, "%Y-%d-%m %H:%M:%S")
'2019-09-08 01:01:01'//Result
I read the above answer and it is clear in its delineation of Time, DateTime and Date in Ruby.
Time is packaged with Ruby. It is measured as seconds since January 1, 1970 UTC and is time-zone agnostic. More specifically, the Time class stores integer numbers, which presents the seconds intervals since the Epoch. We can think of this as Unix Time. It has some limitations. I read somewhere if stored as a 64-bit signed integer, it can represent dates between 1823-11-12 to 2116-02-20, but on my system it can represent dates outside this range. If you do not specify the timezone to use in the enviroment variable ENV['TZ'], then it will default to your system time found in /etc/localtime on Unix-like systems. When to use Time? It is useful for measuring time elapse or interpolating a timestamp into a string value.
Rails actually extends the Time class. It accomplishes this through ActiveSupport::TimeWithZone. It provides support for configurable time zones. Note Rails will always convert time zone to UTC before it writes to or reads from the database, no matter what time zone you set in the configuration file. In other words, it is the default behaviour of Rails that all your time will get saved into database in UTC format.
# Get current time using the time zone of current local system or ENV['TZ'] if the latter is set.
Time.now
# Get current time using the time zone of UTC
Time.now.utc
# Get the unix timestamp of current time => 1524855779
Time.now.to_i
# Convert from unix timestamp back to time form
Time.at(1524855779)
# USE Rails implementation of Time! Notice we say Time.current rather than Time.now. This will allow you to use the timezone defined in Rails configuration and get access to all the timezone goodies provided by ActiveSupport::TimeWithZone.
Time.current
TimeWithZone provides a lot of very useful helper methods:
# Get the time of n day, week, month, year ago
1.day.ago
1.week.ago
3.months.ago
1.year.ago
# Get the beginning of or end of the day, week, month ...
Time.now.beginning_of_day
30.days.ago.end_of_day
1.week.ago.end_of_month
# Convert time to unix timestamp
1.week.ago.beginning_of_day.to_i
# Convert time instance to date instance
1.month.ago.to_date
For most cases, the Time with the time zone class from Rails’ ActiveSupport is sufficient. But sometimes you just need a date.
Just as with the Time class, Ruby is packaged with the Date class. Simply require the time library:
require "time"
Time.parse("Dec 8 2015 10:19")
#=> 2015-12-08 10:19:00 -0200
Date.parse("Dec 8 2015")
#=> #<Date: 2015-12-08>
Time.new(2015, 12, 8, 10, 19)
#=> 2015-12-08 10:19:00 -0200
Date.new(2015, 12, 8)
Since Date is part of Ruby, it by default uses the timezone defined in /etc/localtime on Unix-like systems, unless you modify the TZ environmental variable. Just as with the Time class, Rails extends the Date class. Use Date.current instead of Date.today to take advantage of ActiveSupport::TimeWithZone and use Rails-based timezone configurations.
Now there is one more class available with regards to dates and times. DateTime is a subclass of Date and can easily handles date, hour, minute, second and offset. It is both available in Ruby (via require 'time') and in Rails (via require 'date'). Rails extends it with TimeZone capabilities just like with the Time class.
require 'date'
DateTime.new(2001,2,3,4,5,6)
I personally do not see a need for using DateTime in your applications, for you can use Time itself to represent dates and times, and you can use Date to represent dates.
The second part of the question was regarding strptime and strftime. Time, Date and DateTime all have the strptime and strftime methods. strptime parses the given string representation and creates an object. Here is an example:
> result = Time.strptime "04/27/2018", "%m/%d/%Y"
=> 2018-04-27 00:00:00 -0400
> result.class
=> Time
This is useful if you have an application and a user submits a form and you are given a date and/or represented as a string. You will want to parse it into a Time or Date before you save it to the database.
strftime formats a date or time. So you call it on a Date or Time object:
> Date.current.strftime("%Y-%m-%d")
=> "2018-04-27"
And you can use them together to first parse user input and then format it in a certain way, perhaps to output into a csv file:
value = Date.strptime(val, '%m/%d/%Y').strftime('%Y-%m-%d')
I have the following times stored in an XML document, which correspond to the time when the document was created and then updated:
<create-time>2010-11-04T03:13:35.212Z</create-time>
<update-time>2010-11-03T20:18:26.331-07:00</update-time>
The document was created at 8:13 pm, and then updated 5 minutes later, at 8:18 pm, but when I show the creation dates with format-dateTime(xs:dateTime(.), '[M]/[D]/[Y]'), I get 11/4/2010 and 11/3/2010, as if the document was updated one day before it was had been created, which is obviously not the case. How can I fix this?
The create-time and update-time in your XML document are correct, but they use different timezones:
create-time is in UTC (also called Zulu time, hence the Z).
update-time is in Pacific time.
This can happen if different pieces of code set this the time, or even from the same code using different libraries or functions. For instance, if you are using XPath from XForms:
Using current-dateTime() uses a timezone from the dynamic context, which is often the current timezone for the machine on which the code is running.
Using now() always returns a UTC time.
The solution in XPath is to use the adjust-dateTime-to-timezone() function. This will normalize your dateTimes so they are in the same timezones. As an example, in an XForms output, to show just the date part of create-time you would use:
<xforms:output value="format-dateTime(adjust-dateTime-to-timezone(xs:dateTime(create-time)), '[M]/[D]/[Y]')">
<xforms:label>Creation date</xforms:label>
</xforms:output>
I am trying to create a rss feed from a web page. I am able to get the data to create the title, but I am trying to get the date updated correctly. There is a string on the page, that tells what time of day the news was reported, such as "Time of Report: 1pm". So how can I get the "1pm" and convert that to a date string that contains the current date, with this time?
Thanks,
CP
The Pipes "Date Builder" module is surprisingly powerful.
You can actually feed it a string like "1pm" and it will return:
hour21
timezoneUTC
second0
month1
minute0
utime1264021200
day20
day_of_week3
year2010
The date is relative to when you run it - in this case it assumes "1pm Today". The result is "hour21" in UTC because it parsed in Yahoo's time-zone. So if you need something specific, use the "Sting Builder" to append the time-zone. For example "1pm GMT" gives:
hour13
timezoneUTC
second0
month1
minute0
utime1263992400
day20
day_of_week3
year2010
Obviously you may run into issues if your pipe runs after a date change - an "11pm" entry could easily be read the following morning, leading to inaccurate results.