Differences between revisions 6 and 7
Revision 6 as of 2018-10-10 11:55:00
Size: 4688
Comment:
Revision 7 as of 2018-10-10 11:55:14
Size: 4642
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
 ||<|1> [[Python/Prog/Example/02/Ex_005a|Ex_02_005a_date_and_time_builtin.py]]||                                            ||  ||<|1> [[Python/Prog/Example/02/Ex_005a|Ex_02_005a_date_and_time_builtin.py]]||

例題(5)

  • この例題のポイント: datetimeモジュール

    例題(5)

    【組込関数】

    現在の[日付]を返す関数と,現在の[時刻]を返す関数

    datetimeモジュールを利用する

    Ex_02_005a_date_and_time_builtin.py

  • プログラムの本質的な部分は,下記の 4つの関数 です.
    • datetimeモジュール
      • dateクラス/datetimeクラス
        • today() クラスメソッド
        • now() クラスメソッド
      • dateオブジェクトの属性
        • year
        • month
        • day
      • datetimeオブジェクトの属性
        • hour
        • minute
        • second
        • microsecond

   1 import datetime
   2 
   3 def today():
   4     return( datetime.date.today() )

   1 import datetime
   2 
   3 def now():
   4     return( datetime.datetime.now() )

   1 import datetime
   2 
   3 def today_str():
   4     date_of_today = today()
   5     return( str( date_of_today.year  ) + "" + 
   6             str( date_of_today.month ) + "" +
   7             str( date_of_today.day   ) + "" )

   1 import datetime
   2 
   3 def now_str():
   4     current_time = now()
   5     return( str( current_time.hour   ) + "" + 
   6             str( current_time.minute ) + "" + 
   7             str( current_time.second ) + "" + 
   8             str( current_time.microsecond ) + "μ秒" )
  • 例題ファイルとしては,コメントを付け加えた,下記を配布します.
    • コメントはもう少し増えるかもしれません.

   1 #!/usr/bin/env python
   2 # -*- coding: utf-8 -*-
   3 # ==============================================================================
   4 # * Copyright (c) 2018 IIJIMA, Tadashi
   5 # *       (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
   6 # ==============================================================================
   7 # ソフトウェア工学[02] 例題[02]-(005a)  Ex_02_005a_date_and_time_builtin.py
   8 # Ex(Example) 02-005a: 【組込関数】 [日付]と[時刻]にかかわる組込み関数の使い方.
   9 #                               今日の[日付]を返す関数と,現在の[時刻]を返す関数を定義する. 
  10 #        2018-10-03 飯島 正 (iijima@ae.keio.ac.jp)
  11 # ==============================================================================
  12 # ----- 日付時刻を扱うためのdatetimeモジュールをインポートする -----
  13 import datetime
  14 # ==============================================================================
  15 # ===== 【関数定義】 ===== 
  16 def today():
  17     return( datetime.date.today() )
  18 # ==============================================================================
  19 # ===== 【関数定義】 ===== 
  20 def now():
  21     return( datetime.datetime.now() )
  22 # ==============================================================================
  23 # ===== 【関数定義】 ===== 
  24 def today_str():
  25     date_of_today = today()
  26     return( str( date_of_today.year  ) + "" + 
  27             str( date_of_today.month ) + "" +
  28             str( date_of_today.day   ) + "" )
  29 # ==============================================================================
  30 # ===== 
  31 def now_str():
  32     current_time = now()
  33     return( str( current_time.hour   ) + "" + 
  34             str( current_time.minute ) + "" + 
  35             str( current_time.second ) + "" + 
  36             str( current_time.microsecond ) + "μ秒" )
  37 # ==============================================================================
  38 # ===== 【メイン・プログラム】  =====
  39 # ----- オープニングメッセージ -----
  40 print( "標準モジュールdatetimeを使って,年月日からその日の曜日を求めます: " )
  41 
  42 # ----- 実行結果の表示 -----
  43 # ----- 今日の日付 -----
  44 print( "今日の日付は", today() )
  45 print( "今日の日付は", today_str() )
  46 
  47 # ----- 現在の日時 -----
  48 print( "今日の日時は", today() )
  49 
  50 # ----- 現在の時刻 -----
  51 print( "現在の時刻は", now_str() )
  52 # ==============================================================================


SoftEng: Python/Prog/Example/02/Ex_005a (last edited 2018-10-25 13:00:42 by TadashiIijima)